How would I do this? Or is it even possible get a button within a fragment from the activity?
I've tried many things e.g. trying to get the view or container of the fragment but no luck.
I am doing this as i have 27 fragments in an activity which all have a same button. Instead of doing a button listener on each fragment, i want to reduce my code greatly but just doing a loop in my activity going through each fragment and setting there button to activate the button listener (in the activity) if pressed.
Once i know how to get a button and set it to the button listener, I can easily do the loop bit my self.
Just finishing my first year for computer science and have been coding in java for a couple of years now.
Thanks in advance!
Don't loop or get reference to the button.
Simply make your activity implement OnClickListener and set it as click listeners for the buttons in all your fragments ..
In the fragment
yourBtn.setOnClickListener(((YourActivityType) getActivity()));
another solution without the implements keyword
is to have a public method in your activity, for example, setButtonAction(Button btn)
public void setButtonAction(Button btn) {
btn.setOnClickListener(myClickListener);
}
and declare myClickListener as variable in youractivity
Same idea without having your activity to implement clickLitener
Related
I am using Java and Android Studio. I have one activity with a few options to select from, and once one of them is selected, they click the button that takes them to the next activity. They do the same thing again except this time when the user clicks the button it takes them to a new activity where it displays the results based on the two radio buttons clicked previously. How can I do this?
You can send some variable through intent and onCreate() method of the second activity, you can use that variable to differentiate your functionality.
This video demonstrates the functionality of the radio button: https://youtu.be/zJG9Prn3vZ0
In this video you can know how to pass arguments to another activity: https://youtu.be/OMCctaE66b8
Hope that helps you to solve your problem.
You cannot declare a method inside another method. Put a closing brace after onCreate.
When you are opening a new activity, you are doing it with an Intent. Put the values you want to send (bool clicked) the new activity inside the intent
I am working on an android application. I have cardviews for multiple fragemnts. I have put these cardviews in the main activity. So when I pressed the cardview a fragment will open. So i will make these cardviews visibility as gone. But on back press the onResume() is not called. Hence the layout is not getting visible. I tried many things but did not work. What should I do?
From what I understand, you have an activity with a fragment. Then you load a new fragment and then you press back. This interaction will not fire the activity's onResume() because the activity has never been stopped. If there is logic you need to perform between fragments, it needs to be somewhere else. I cannot provide any more details without some more information.
I think first of all use the navigation on Android Jetpak
Help me solve my problem. I'm doing the transition between the activity using the startActivity method.
If I press n-times on the button that initiates startActivity, there will be n-transitions to activity. How to make the transition was only one?
You can set the button unclickable when you press it the first time.
The code is:
button.setEnabled(false);
I have two questions and your help is really appreciated
First:
is there a way to make a button in the main activity and a menu item open the same fragment from back stack without creating a new one.
Second:
how can I retain what is typed in editText in the fragment without any buttons clicked, should I use outState.putString() in the onSaveInstantState() method or somewhere else and then do I check if it existed should I do it in the onCreate() or onCreateView()?
thank you!
First: yes, you can, just use similar code in button onClick and onOptionsItemSelected
pop the fragment by name example
Second: Save fragment state example
In my application i am using tabhost. And to control different activities i am using activity group. I have Activity A from which i am going to activity B. Activity B contains Edit Text, Spinner, Button etc. Now when i scroll the Activity B and press the device Back Button than it does not go to the previous Activity. It goes out of application.
Kindly suggest the answer.
This is the way the Activity lifecycle works for Activity Group. Try using fragments instead of activity group with your tabhost to get your movements. Also, please edit your question title to more accurately reflect the actual question.