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...
Related
I have a Navigation Drawer like this
Now I want to access the Test Button inside the ListItem 1
I tried it inside the DrawerLayout.onItemClickListener() but it is not working.
I have my drawerListener() as a ActionBarToggle object.
How can I get access to the button?
Help Needed. Thanks :)
I have found my solution.
I made a class implementing OnClickListener and set the onclick method of the button inside the getView of the listview.
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
I have the following button-in-Fragment problem:
My custom adapter throws out a row in my listview. That row has a (remove) button.
When the user clicks on the button in the row, the row is removed (and he button as well, duh). I have this working perfectly in an activity. But, how does this work in a Fragment?
If I use findViewbyId in onCreateView it crashes, because the view simply does not exist yet.
onClick in xml is also not an option, that does not work for fragments.
Somewhere I should be able to place a onClick listener and be able to remove it with adapter.remove. But where to place it and what does it look like?
If I understand what you're saying correctly you should add the onCLickListener to the button from within your adapter.
Use onViewCreated() ..it is called after onCreateView, when the view returned has been added.
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.
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.