create stylish buttons using code

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

Currently I am not using the visual designer at all.
Instead I am reading my layout from a comma delimited file that I create. This approach provide me lots of flexibility to modify my layouts quickly and easily.

Whoever, I am facing some major limitation and in particular creating nice looking buttons with different colors.

Let me clarify. If I don’t change the button colors programmatically at all, the buttons by default inherit the OS theme and look stylish ( with color tones etcc. ). On the other hand, if execute this line of code
tbtnButton.Color = Colors.RGB( strBackColorList(0), strBackColorList(1), strBackColorList(2))

The button doesn’t look nice any more.

What I am trying to get at, is allowing the user to change colors based on light conditions, but still keep the elegant look.

Any ideas?

BTW I am still using version 1.7
 

iCAB

Well-Known Member
Licensed User
Longtime User
Ok let me to explain in a different way.

I have a nexus 7, by default when you create buttons in code, the buttons are created with the default OS theme and look kinda shiny silver ( grey shades). Say I want to create the same type of button in code with a different set of color shades ( example a mix of lime green tones ), but I want the button looks to stay exactly the same as the default one with only one difference ( color tones ).



Thank you
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Ok let me to explain in a different way.

I have a nexus 7, by default when you create buttons in code, the buttons are created with the default OS theme and look kinda shiny silver ( grey shades). Say I want to create the same type of button in code with a different set of color shades ( example a mix of lime green tones ), but I want the button looks to stay exactly the same as the default one with only one difference ( color tones ).
This is definitely possible using StateListDrawables but I do not know how predictable this is.
You want to get the current drawable of the button (for all states), and modify the color or the bitmaps, and create a new statelistdrawable.
The hard thing will be to change the color shades in my opinion.

This code will draw a bitmap on the current command button style:

B4X:
Sub CreateBitmapButton(cmd As Button, bmp As Bitmap, act As Activity, Padding As Int )
   Dim C As Canvas 
   Dim r1 As Rect 
   Dim r2 As Rect 
   Dim sld As StateListDrawable 

   'Create a temporary button and draw on it
   Dim tmpbutton As Button 
   tmpbutton.Initialize("")
   act.AddView(tmpbutton,0,0,cmd.Width,cmd.Height)
   
   SetPressed(cmd, True)
   DoEvents

   C.Initialize(tmpbutton)
   r1.Initialize(0,0,cmd.Width,cmd.Height)
   r2.Initialize(Padding,Padding,cmd.Width-Padding,cmd.height-Padding)
   sld.Initialize 
   
   C.DrawDrawable(cmd.Background,r1)
   C.DrawBitmap(bmp,Null,r2)
   tmpbutton.Invalidate 
   sld.AddState(sld.State_Pressed,tmpbutton.Background)
   
   tmpbutton.RemoveView 
   
   'Force to pressed state
   SetPressed(cmd, False)
   DoEvents
   tmpbutton.Initialize("")
   act.AddView(tmpbutton,0,0,cmd.Width,cmd.Height)
   C.Initialize(tmpbutton)
   C.DrawDrawable(cmd.Background ,r1)
   C.DrawBitmap(bmp,Null,r2)
   tmpbutton.Invalidate 
   sld.AddState(sld.State_Enabled,tmpbutton.Background)
   
   tmpbutton.RemoveView 
   SetPressed(cmd, False)
   cmd.Background = sld

End Sub
 
Upvote 0

iTzCorky

Member
Licensed User
Longtime User
Judging by your title you want to create stylish buttons apart from the ones that are provided. This is very easy to do. First create a button with photoshop and or go on google and find you some button templates and then make a button and then next all you add is an imageview and set that image as your button. If this is not what you were looking for please let me know and I will help you further.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

I was hoping to achieve this without having to add bitmaps as this may get too complicated to handle.

What I am actually trying to achieve is some similar to a "Skin" or a "Theme".
The application is for in vehicle use ( similar to a navigation system ). During the day, you need a different set of colors (something that is more readable in direct sun light ) than at night time ( not too bright ).

I think I may have to experiment a little bit with what "Klaus' suggested "Gradient Drawable".

Thanks for all your inputs.
greatly appreciated

Any other suggestions would be appreciated as well.
 
Upvote 0
Top