Wednesday, August 10, 2011

Android Tutorial-Making a custom Android button using a custom view

Android Tutorial-Making a custom Android button using a custom view



Creating a custom view is as simple as inheriting from the View class and overriding the methods that need to be overridden. In this example, a custom button is implemented in this way. The button shall feature a labelled image (i.e. an image with text underneath).



1 public class CustomImageButton extends View {

2 private final static int WIDTH_PADDING = 8;

3 private final static int HEIGHT_PADDING = 10;

4 private final String label;

5 private final int imageResId;

6 private final Bitmap image;

7 private final InternalListener listenerAdapter = new InternalListener();

8



The constructor can take in the parameters to set the button image and label.





9 /**

10 * Constructor.

11 *

12 * @param context

13 * Activity context in which the button view is being placed for.

14 *

15 * @param resImage

16 * Image to put on the button. This image should have been placed

17 * in the drawable resources directory.

18 *

19 * @param label

20 * The text label to display for the custom button.

21 */

22 public CustomImageButton(Context context, int resImage, String label)

23 {

24 super(context);

25 this.label = label;

26 this.imageResId = resImage;

27 this.image = BitmapFactory.decodeResource(context.getResources(),

28 imageResId);

29

30 setFocusable(true);

31 setBackgroundColor(Color.WHITE);

32

33 setOnClickListener(listenerAdapter);

34 setClickable(true);

35 }

36



With the constructor defined, there are a number of methods in the View class that needs to be overridden to this view behave like a button. Firstly, the onFocusChanged gets triggered when the focus moves onto or off the view. In the case of our custom button, we want the button to be “highlighted” when ever the focus is on the button.



This android tutorial to create and android button has been taken from the android development



tutorial by androidcore click the link to read the rest of the android tutorial



learn android programming

16 comments:

  1. redirected xanax for anxiety and sleep - xanax online no prescription overnight

    ReplyDelete
  2. this site xanax side effects mayo clinic

    ReplyDelete
  3. buy ambien online took 3 ambien cr - ambien drug interactions oxycodone

    ReplyDelete
  4. tramadol online buy tramadol online cash on delivery - need purchase tramadol

    ReplyDelete
  5. Visit Website 2mg klonopin equal - generic form of klonopin

    ReplyDelete
  6. buy retin a retin a cream generic name - retin a cream tube size

    ReplyDelete
  7. more helpful hints xanax side effects sexually - generic xanax thailand

    ReplyDelete
  8. address where to buy ambien cr online - ambien online no prescription mastercard

    ReplyDelete
  9. valium online valium en diazepam - what is valium pill used for

    ReplyDelete
  10. ambien online price of ambien per pill - ambien side effects withdrawal

    ReplyDelete
  11. this site generic xanax pill identification - different types xanax pills

    ReplyDelete
  12. buy valium online purchasing valium online legal - where can i buy real valium online

    ReplyDelete
  13. generic valium valium anxiety symptoms - how long will 10mg valium last

    ReplyDelete
  14. smCSihI streaming free ku 12 years a slave watch online free stkw LsENZlP onli

    ReplyDelete
  15. Find Out More xanax drug forum - much do xanax pills cost street

    ReplyDelete
  16. site internet much does 1mg xanax sell - xanax doses

    ReplyDelete