Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid :Customizing Button with dynamic values
    text
    copied!<p>I want to customize Buttons for my application. The application has a color picker where the user will select color and I have to set that particular start/end color to the buttons. These colro values will be stored in an object "Utility". </p> <p>Basically from start only, I want to use "Utility" object to set colors for background, text color, font, etc. And again when the color is changed by the user I got to change it to the buttons and refresh them. And also to save colors in a file, so next time user starts app, it comes up with the last color selected. </p> <p>I couldn't find <code>&lt;selector&gt;</code> to be the best option, as I wont be able to change the color in xml. What can be the best option for such requirement ?</p> <p><strong>UPDATIONS :</strong> @jitendra, from your answer I got somethign helpful. I use GradientDrawable to set colors of my buttons. In my onCreate() of the Activity, I call a method RefreshComponents() that sets the background of root, text color/size of buttons and gradient colors of the buttons. It works properly, but the only problem I see is the on applying GradientDrawable to the button the gap between 2 buttons is lost. </p> <p>This is the image WITHOUT applying GradientDrawable : <img src="https://i.stack.imgur.com/bL7EU.png" alt="enter image description here"></p> <p><strong>On applying GradientDrawable the output is :</strong> <img src="https://i.stack.imgur.com/Byg4n.png" alt="enter image description here"></p> <p>You see the size of button is increased a bit from all the sides. If I apply to next button also, they both touch eachother. My xml for the above is :</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainroot" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingTop="35dip" android:paddingBottom="35dip" android:paddingLeft="35dip" android:paddingRight="35dip"android:gravity="center" &gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainrow1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="15dip" android:gravity="center_horizontal" &gt; &lt;Button android:text="Accounting" android:id="@+id/accBtn" android:layout_width="80dip" style="@style/TileButtonStyle" /&gt; &lt;Button android:text="Data" android:id="@+id/dataBtn" android:layout_width="80dip" android:layout_height="fill_parent"&gt;&lt;/Button&gt; &lt;Button android:text="Information" android:id="@+id/infoBtn" android:layout_width="80dip" android:layout_height="fill_parent" android:ellipsize="end"&gt;&lt;/Button&gt; &lt;/LinearLayout&gt; ..... Other lineasr layout with same parameters as above child </code></pre> <p>And the GradientDrawable that I create is :</p> <pre><code> public static GradientDrawable getButtonDrawable(Button btn) { int colors[] = {getStartColor(), getEndColor()}; GradientDrawable grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors); grad.setCornerRadius(5f); return grad; } </code></pre> <p>And finally in my onCreate(), I add :</p> <pre><code> GradientDrawable btnGradient = Utility.getButtonDrawable(btn1); btn1.setBackgroundDrawable(btnGradient); </code></pre> <p>What is going wrong here ? Is the margin around the button becoming 0 ? Do I have to set bounds for the grad, or again set LayoutParams for the button ? </p> <p>Any help is appreciative to help me achieve my goal.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload