Creating Custom Views - The ToggleButton

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

press button from code

Postby » Thu Feb 04, 2010 12:34 am


Thanks a lot for the tutorial. I'm able to place and display buttons on the screen.

I need to use this as option buttons. ie, there will be 2 buttons. Only one should be pressed at a time. How can I do that. I tried doing, ie, by calling performClick() of the other button, but it does not reflect in the screen.

can you please help me to implement this.
Once Poster
Once Poster
Posts: 1
Joined: Sun Nov 01, 2009 9:42 pm
Location: London


Re: Creating Custom Views - The ToggleButton

Postby Snedd » Sun Aug 29, 2010 4:12 pm

Great stuff, this is exactly what I was looking for to figure out how to do custom views that can be used in XML layouts, many thanks.

To add what I found following on from this tutorial, if you just want to extend the basic View class (instead of Button) then you have to ignore the params, e.g.

Code: Select all
public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

instead of

Code: Select all
public MyView(Context context, AttributeSet attrs, Map params) {
        super(context, attrs, params);

and it all works.
Again many thanks to the poster :)
Posts: 7
Joined: Sat Aug 21, 2010 11:51 am


Postby milesf » Thu Jun 16, 2011 8:35 am

I struggled for a few hours trying to get this outdated example to work. I finally figured it out, and I want to share some suggestions for anyone who is running into the same problems.
After following HBarker's instructions on how to update the depreciated Button members and successfully compile, I was still getting the following runtime error: "android.view.InflateException: Binary XML file line #23: Error inflating class"
The problem is that a third ToggleButton constructor is missing. The solution is to add the following lines of code to
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public ToggleButton(Context context, AttributeSet attrs) {
  2.                 super(context, attrs);
  3.         }
Parsed in 0.032 seconds, using GeSHi

I think HBarker was hinting of this solution when mentioning using the constructor completion menu, but I was primarily focusing on the Map to int conversion, so it wasn't obvious to me. So to be extra specific, you can either copy and paste the code above, or use Right-Click>Source>Generate Constructors from Superclass... to add the missing constructor.
Once Poster
Once Poster
Posts: 1
Joined: Thu Jun 16, 2011 7:38 am


Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests