creating a button inside onclick listener? - android

I want to dynamically create a set of buttons when my program runs.I want these buttons to be created when i create a generate button.Is it possible to call Button but=new Button(this); inside OnClickListener of another Button?

Yes.. say Button b = new Button(yourAct.this);..
and do not forget to add it to your parent layout by saying addView(b)

Yes -- just be sure to change new Button(this) to new Button(mClass.this).

This does work. I have an activity that creates a button in the same way. To set onClickListener to a method in the outer class, you need to use (mClass.clicklistener) to set the listener methods.

Related

How can I make this Custom Control and Android

I want to make a custom control in Android.
Actually, I want to extends a EditText with one Button and one TextView. (Like below picture)
My Custom Control Picture
I have not problem with listener. I want just make a custom control that contains 3 controls. (EditText, Button and TextView)
How can I do it?
Create a Linear layout with orientation = horizontal .
Then create an edittext and a button in it .
In java code ,set onClicklistener on button and then get the value of edittext by [edittext].getText().toString() ;
Control means you want to perform any action (a similar action i understand from your explaination) on click of any of the three?
If so, have a specific method which will be called on click of each of the items and alter them according to your choice.
Is this what you meant by user control?

Android change layout and set onclick event on button in new layout

I have application with two layouts (main and detail). Application starts with main layout. I have button on main layout. When i click on it i call setContentView(R.layout.detail);.
Now i have set new layout with button and i need set onclick event on this new layout button.
I tried set both events in onCreate() method. After that works only first button on main layout. Button on detail layout don't works.
Can you help me?
Its not a recommended way to change the layouts.
Anyways, once you change the layout by setContentView(R.layout.detail) , you need to set the onclickListener of the button again.
After setting the new view, try to findviewbyid(urbuttonid) and setonclicklistener for this.
It should work.
This question has very simple solution:
When you set another content view , Initialize again button of new layout like this:
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
after that your click event goes for second view's button...
or
you can do assign different id's and names to both buttons that you can handle each button click by its id.
or
you can assign click listener for each button on the views.
Good Luck

Android : How to identify button click in a Listview having more than one button

My custom Listview has more than one button, how can I identify which button has been clicked ? I want to do different things on action of different buttons.
In custom adapder of your ListView you must be creating objects of each button in its getView() method.
Just set the onClickListener on the buttons and perform the action on each buttons in its onClick().
Create local variables for buttons inside getview method and set onClickListener for each button in a row.
Also have a look at this -:
2 Buttons in ListView in android

Android ListView: How to use the activity onClickListener from a custom Adapter?

I have a listView using a custom adapter. Each row contains a button and some other Views. I want to be able to click either on the button, or on the row itself (to edit the item of the list that is clicked).
Setting an onItemClickListener in the activity won't work because of this problem
I think I have to set an onClickListener in the getView() method of my adapter for it to work properly.
I would like to use my activity's onClickListener, in order to use a startActivityForResult() when the row is clicked, in order to have something returned to my activity when the item edition activity is over.
How can I do that?
Thanks!
You'll need to add an onclick listener to every button you add to every row. The best way to do this is probably to make your own custom layout in code, and every time you create a new view in your adapter, set the onclick listener in the layout code.

How to access an element present in parent view?

full code
how do i access the parent activity's mail layout elements? In this case a button. I have it declared in main.xml. When a button in the listview is clicked, i want to change the text of the button in main.
So you have a ListView and a button in your layout, and you want to change the button's text, when one of the buttons in your ListView is clicked? There're many ways to achieve this.
You could pass the reference of your button to your custom Adapter class. That way you can accesss that button from the inner event handler calss you defined for the buttons in your list rows - so you can change its text.
You could fire a broadcast intent from the row-buttons' event handlers. You listen for these intents in your Activity, and change the Button's text accordingly. This may be an overkill tho.
Use a static method for it in the Activity.
etc.

Categories

Resources