Creating Custom Buttons

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Creating Custom Buttons

Postby Katharnavas » Thu Dec 06, 2007 8:03 am

Hi,
Can you please explain the possibilities of creating a custom icon like buttons which should be changed the image on focus and even get clicked and get back to normal while the other button got focus.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby plusminus » Thu Dec 06, 2007 3:35 pm

Hello ,

I haven't done that before, but i would start extending the Button-Class on have a look at the methods/constructors Button(and Parent-Classes provide) provides:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class MyButton extends Button{
  2.  
  3.         public MyButton(Context context) {
  4.  
  5.                 super(context);
  6.  
  7.                 // TODO Auto-generated constructor stub
  8.  
  9.         }
  10.  
  11.  
  12.  
  13.         public MyButton(Context context, AttributeSet attrs, Map inflateParams, int defStyle) {
  14.  
  15.                 super(context, attrs, inflateParams, defStyle);
  16.  
  17.                 // TODO Auto-generated constructor stub
  18.  
  19.         }
  20.  
  21.  
  22.  
  23.         public MyButton(Context context, AttributeSet attrs, Map inflateParams) {
  24.  
  25.                 super(context, attrs, inflateParams);
  26.  
  27.                 // TODO Auto-generated constructor stub
  28.  
  29.         }
  30.  
  31.        
  32.  
  33.         @Override
  34.  
  35.         protected void onFocusChanged(boolean arg0, int arg1) {
  36.  
  37.                 // Kinda switch the background here ???
  38.  
  39.                 super.onFocusChanged(arg0, arg1);
  40.  
  41.         }
  42.  
  43.  
  44.  
  45.         @Override
  46.  
  47.         public int[] createDrawableState() {
  48.  
  49.                 // Perhaps here the Drawables for pressed/enabled/disabled are passed ?
  50.  
  51.                 return super.createDrawableState();
  52.  
  53.         }
  54.  
  55.  
  56.  
  57.         @Override
  58.  
  59.         public boolean onMotionEvent(MotionEvent ev) {
  60.  
  61.                 // TODO Auto-generated method stub
  62.  
  63.                 return super.onMotionEvent(ev);
  64.  
  65.         }
  66.  
  67.  
  68.  
  69.         @Override
  70.  
  71.         public boolean performClick() {
  72.  
  73.                 // TODO Auto-generated method stub
  74.  
  75.                 return super.performClick();
  76.  
  77.         }
  78.  
  79. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Katharnavas » Sat Dec 08, 2007 5:00 am

Hi,
Thank you for ur help . So for doing this we need to create our own button class only.Is not possible to do with whatever we have ? is it so?
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby plusminus » Sat Dec 08, 2007 12:38 pm

Hi Katharnavas,

this can probably be done by altering the styles of the button. We did that with the Christmas Calender, by overriding their background to a simple color. I think there are is also one or two attributes (like android:background , ...) that could manage what you want to do.

Have a look here (click) and search for/scroll down to "Stretchable Image". They are creating a customized button-background there.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Katharnavas » Sat Dec 08, 2007 1:21 pm

Hi,
Thank you again for ur suggestions. Is it possible to use a class that extends BaseAdapter as did in Gallery Example and Image Switcher. But instead of using the BaseAdapter with Gallery i need to use it with Button or ImageButton.
Explore the possibilities of using it and if u would like with an example please?

plusminus wrote:Hi Katharnavas,

this can probably be done by altering the styles of the button. We did that with the Christmas Calender, by overriding their background to a simple color. I think there are is also one or two attributes (like android:background , ...) that could manage what you want to do.

Have a look here (click) and search for/scroll down to "Stretchable Image". They are creating a customized button-background there.

Regards,
plusminus
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby plusminus » Sat Dec 08, 2007 5:22 pm

Hello Katharnavas,

An adapter is not really a very special thing, it just handles 2-3 functions automatically.
I don't think you'll find a proper solution with an Adapter. I would still try using the onFocusChanged()-method. Which gives you exactly the thing you requested in the first post.
Change image on getting/loosing Focus.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Katharnavas » Mon Dec 10, 2007 5:11 am

Hi,
Thanks for ur suggestion.. I will try and let you know the results.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby benderamp » Mon Dec 17, 2007 9:56 pm

Hi,

I had chance to implement my custom toggle button in the following way:

I have extended Button class, and overriden 2 methods:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         public boolean performClick() {        
  5.  
  6.                 isChecked = !isChecked;
  7.  
  8.                 return super.performClick();
  9.  
  10.         }
  11.  
  12. @Override
  13.  
  14.         public int[] createDrawableState() {
  15.  
  16.                 int[] states;
  17.  
  18.                 if (super.hasFocus()) {
  19.  
  20.                         if (isChecked()) {
  21.  
  22.                                 states = Button.PRESSED_STATE_SET;
  23.  
  24.                         } else {
  25.  
  26.                                 states = super.createDrawableState();
  27.  
  28.                         }
  29.  
  30.                 } else {
  31.  
  32.                         if (isChecked()) {
  33.  
  34.                                 states = Button.PRESSED_STATE_SET;
  35.  
  36.                         } else {
  37.  
  38.                                 states = Button.LAST_STATE_SET;
  39.  
  40.                         }
  41.  
  42.                 }
  43.  
  44.                 return states;
  45.  
  46.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


The key method is createDrawableState() - it returns the drawable, that should be drawn on the button depending on its current state - I wanted to use standard Button graphics, so I used Button.LAST_STATE_SET etc - I suppose you can provide any graphics here. In the above case with Button createDrawableState() is called multiple times when super.performClick() is called - first to draw the pressed button, then to draw unpressed button. You can try to play with this code more to understand the details.
benderamp
Freshman
Freshman
 
Posts: 5
Joined: Sun Nov 25, 2007 5:19 am

Postby plusminus » Mon Dec 17, 2007 11:09 pm

Hello benderamp,

I'm transforming this into a tutorial :) (With Loading from XML and stuff)

Thx for posting.

Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Katharnavas » Wed Dec 19, 2007 5:06 am

Hi nice to hear that you are going to do a tutorial of it ...

plusminus wrote:Hello benderamp,

I'm transforming this into a tutorial :) (With Loading from XML and stuff)

Thx for posting.

Best Regards,
plusminus
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby plusminus » Wed Dec 19, 2007 6:36 pm

Hello Katharnavas,

Oops, forgot to post the link: :src: http://www.anddev.org/viewtopic.php?t=310

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to General

Who is online

Users browsing this forum: No registered users and 8 guests