0

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".

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.

I couldn't find <selector> 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 ?

UPDATIONS : @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.

This is the image WITHOUT applying GradientDrawable : enter image description here

On applying GradientDrawable the output is : enter image description here

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 :

<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" >

   <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" >
         <Button android:text="Accounting" android:id="@+id/accBtn" android:layout_width="80dip" style="@style/TileButtonStyle"  />
        <Button android:text="Data" android:id="@+id/dataBtn" android:layout_width="80dip" android:layout_height="fill_parent"></Button>
        <Button android:text="Information" android:id="@+id/infoBtn" android:layout_width="80dip" android:layout_height="fill_parent" android:ellipsize="end"></Button>
  </LinearLayout>
  ..... Other lineasr layout with same parameters as above child 

And the GradientDrawable that I create is :

    public static GradientDrawable getButtonDrawable(Button btn) {
    int colors[] = {getStartColor(), getEndColor()};
    GradientDrawable grad = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
    grad.setCornerRadius(5f);
    return grad;
}

And finally in my onCreate(), I add :

    GradientDrawable btnGradient = Utility.getButtonDrawable(btn1); 
btn1.setBackgroundDrawable(btnGradient);

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 ?

Any help is appreciative to help me achieve my goal.

Thanks

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • you don't need to update layout parameters unless you are setting any of them in your code when you are changing background, but it seems you are adding some layout parameter on change of background. – jeet Jan 07 '12 at 11:13
  • @jitendrasharma, My next goal is to resize the layout dynamically which should auto resize the buttons. Have a look and try to help - http://stackoverflow.com/questions/8769491/android-retrieve-layout-marginbottom-programmatically – Tvd Jan 07 '12 at 12:00
  • @jitendrasharma, I don't plan to change LAyoutParams of each button. The above was just a point. But actually adding marginRight to buttons only did the job. Wondering their should be something else or I may be going wrong somewhere but can't find it. – Tvd Jan 07 '12 at 12:04

4 Answers4

1

You can Create StateListDrawable Object dynamically in java file and set as background and sources of applcation components.

jeet
  • 29,001
  • 6
  • 52
  • 53
  • I don't need different styles for different states, so instead of using StateListDrawable, I tried workign with GradientDrawable. But the button's size is changed. Have a look at my updated question. – Tvd Jan 07 '12 at 09:03
0

Android has Themes and Styles, but they are a development-time feature and can't be manipulated at runtime. However, different predefined themes can be applied at runtime.

So you can have a set of predefined themes, with fixed view properties (colors, fonts, etc..) and give user an option to choose a theme at runtime.

But, if you need to change every particular view property, then you will need to roll your own "theme" system. Which means you will need to have properties stored somewhere and applied each time a view is built.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • predefined Themes are also not allowed. Every thing to be pure dynamic - whatever user selects - background, 2 gradient colors, text color, etc. Can you focus more on your point reg "roll your own theme system". – Tvd Jan 06 '12 at 12:02
  • @Tvd: did you mean "predefined themes are not allowed" in your app or in android in general? – Peter Knego Jan 06 '12 at 12:30
  • in my application. As said everything got to be dynamic based on user selection, so Predefined can have no space in it. – Tvd Jan 06 '12 at 13:21
0

Tvd! I incline to agree with Peter Knego and Jitender Sharma. Furthermore, I think/believe you can setOnClickListener on those buttons of yours and perform your color changing stuff with the code that rests inside the setOnClickListenermethod assigned to every button. In addition to this, you'll have to configure your color.xml file and custom themes. There's a lot of work to be done. I'm not really sure which is the best way though. I strongly suggest you to go through the android learning stuff I'd provided to you in my previous answer. Only they can give you a detailed insight and a solid idea to go ahead. All the best!

Community
  • 1
  • 1
Ghost
  • 3,966
  • 1
  • 25
  • 36
  • So, can I edit "colors.xml" at run time ? If nothing works, then ofcourse I have to write code in onCreate method (I guess) of each Activity, where I set all properties based on that's saved in Utility object. Like btn1.setTextColor(Utility.getTextColor()). The learning stuff that you have provided in previous answer, all contains links to buy books & I can't afford that. – Tvd Jan 06 '12 at 11:59
  • @Tvd: no you san not edit resource files at runtime (colors.xml, etc..). – Peter Knego Jan 06 '12 at 12:31
  • 1
    @Tvd: Just Google search those books. They're all available for free download. That's how I've downloaded. If you can't find them anywhere, provide me your mail id if you feel it's safe. I'll mail them to you. As far as altering resources is concerned, you can't do it during runtime. It's of no use if you do it. – Ghost Jan 06 '12 at 12:46
  • @Ghost, Thanks for the books info. Though it didn't help me in this question, but definetely good one for other issues. – Tvd Jan 07 '12 at 10:13
0

Oh I got the solution :

I added layout_marginRight attribute to buttons and that did the work.

Though I am still concerned, without GradientDrawable the buttons had margin betweenthem then after applying GradientDrawable why is the default margin lost ? Why is the need of additional layout_marginRight to be added ?

If anyone yet has answer for this, please let me know.

Thanks

Tvd
  • 4,463
  • 18
  • 79
  • 125
  • I hadn't seen your updated question but now I did. Why don't you reduce your padding? That may help. – Ghost Jan 08 '12 at 05:00
  • @Ghost, Yes that what I did adding more padding to root layout and marginRight to buttons. But the wonder is why is size increased after implementing GradientDrawable. AND HAVE NOT USED any paddign in any of the buttons or inner layout then where do I reduce it ??? – Tvd Jan 09 '12 at 05:57
  • Sorry! Hadn't noticed that point. Well, I don't know any better. Guess you'll have to do more research. – Ghost Jan 10 '12 at 10:08
  • Standard buttons uses 9-patch that has defined padding in it. If you use your gradient drawable you lose padding used in 9-patch. At least it is case in Holo theme. – Salw Aug 20 '12 at 07:57