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

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

Related

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: Share sibling Views pressed state

I have a layout that has two children in it. My goal is to have the whole layout function as a button. Each of the children has a different color to show pressed state. When I press them individually they show their pressed state fine. However when one is pressed, I would like them both to show pressed state.
My first thought was to use duplicateParentState, but the parent never seems to be in the pressed state because the Views fill the layout. My next thought was to use AddStatesFromChildren, but all this did was make the parent show pressed, but not the children. Finally I tried using both, but that caused an error.
How can I get sibling Views to share a pressed state?
I'm only 4 years late to this, but you were close:
set them both inside a parent layout
set your click listener on the parent
then set android:duplicateParentState="true" on each of the children.
Works like a charm!
You could possibly do something like:
Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button otherButton = (Button) findViewById(R.id.other_button);
otherButton.setPressed(true);
}
})
But, of course, have the buttons called as you need to.

creating a button inside onclick listener?

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.

Access another view's content from another view

I have an activity with two view.One view has the buttons and the other is main view.I must disable buttons for some circumstances inside main view.I couldn't figure out how to do that.
Button myBtn = (Button) ((MyActivity)getContext()).findViewById(R.id.mybtn);
I was calling this code inside constructor of my view.
That's why it's not working.
Now it's working...

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