I am inflating multiple same linear layouts from one activity, and I would like to hide all of these from another activity. How can I accomplish this?
what I tried:
LinearLayout test_layout=(LinearLayout)this.findViewById(R.layout.test);
test_layout.setVisibility(View.INVISIBLE);
But test_layout is always "null"
My Activity is "singleTask", so I think if I get hold of one view and close, it will close all similar views opened by this activity.
Related
I'm using a Navigation Drawer Activity. When I made two fragments, I noticed that their layout was the same. The difference is that pressing the button executes a different method for each fragment. But the other buttons do the same thing on the two fragments. How do I reuse the layout and java code for the fragments and be able to specify a method to be executed on each fragment, knowing that it will need View access?
One way to do this is to create two different fragment classes that load the same XML layout and assign the click handler for the button programmatically.
you can use visibility of the button.use same xml for both fragment instead of one button use two, then in fragment check which fragment is in view then change the visibility.Gone of the not needed button for the activity. if you are using databindng then do different task for two button in viewmodel that will do the rest of your button task
You can set a Boolean (fals is the first "fragment", true the second). Then you can use a check in witch "fragment" you are and execute the code.
if(true){
executeFirstButtonUse()
}else{
executeSecondButtonUse()
}
Then U wil have 1 fragmen, 1 view, (1 viewModel).
I u coud provide more info or code I could provide more detail or an other method.
I have a fragment with a textView. I want the textView to change when I scroll across the viewPager.I want to reuse the same fragment rather than creating a new fragment for displaying each text.How do I do this? I hope pro developers can understand what I mean.
You cannot attach a same fragment into activity twice simultaneously. If you want the behavior you mentioned don't use ViewPager. Just use swipe gesture detection in a layout and maybe some animations.
I have a Fragment activity which hosts a fragment list. The fragment activity also has a couple of text views which summarise the data in the fragment list.
FragmentActivity = Layout that contains a fragment container, text views and a button.
ListFragment = goes inside of the FragmentActivity's fragment container and is a list of data
This works fine as an activity, but now I'm trying to put the whole lot into another fragment container to display it in an action bar tab on another activity. I can only get it to work with the ListFragment (obviously this is because the FragmentActivity can't go inside of a fragment container) but this means that the summary text views and the buttons are missing and I really need them there too.
I wondered if anyone had suggestions of the best way to implement this? Some thoughts of solutions I've had:
Change the Layout of the activity holding the tabs if this certain tab is clicked
Programmatically add the text views and button to the fragment container if this tab is clicked
Add another fragment to the fragment activity, below the list fragment
Either way I'm stumped so any help is really appreciated.
Create a Fragment that resembles the layout of you FragmentActivity and add it instead. you can't add the FragmentActivity as a Fragment simply because it's not a Fragment but an Activity that was added a Fragments support for older versions.
So instead of adding you button and TextViews directly to the FragmentActivity Layout. Create a Fragment with the same components and add it to the Activity layout.
That way could could reuse this set of components in another location in your application if you needed to, using the same Fragment.
So I have 2 fragments visible if the user uses a device with a big enough screen (like a Samsung Galaxy Tab).
Right now I am displaying a fragment list twice (same fragment twice). Inside the code of the fragment I use this to hide a progressbar.
ProgressBar pb = (ProgressBar)getActivity().findViewById(R.id.progress);
pb.setVisibility(View.GONE);
Problem is that it only works on one of the fragments. Both have the same id since its the same fragment?
Should I create 2 identical fragments or is it possible to find the "correct" progress-bar in the correct fragment?
In stead of finding the view (the ProgressBar) in the activity's view hierarchy, find it in the fragment's view hierarchy. So inside the fragment, do the following:
ProgressBar pb = (ProgressBar) getView().findViewById(R.id.progress);
Generally, you don't want to do lookups in the parent's view hierarchy, so above basically applies to all views in the fragment's layout.
In my main layout (mainlayout) I am displaying some text and images which are set dynamically based on the actions of the user. For one particular button click I need to display the contents of another layout (secondlayout). I do this using:
setContentView(R.layout.secondlayout);
On the second layout I have another button that I use to return to the main layout, once again using:
setContentView(R.layout.mainlayout);
The problem is on displaying the mainlayout again all the text and images I was displaying have now disappeared.
How can I return to the mainlayout and still display the contents I was displaying?
don't do it that way. setContentView() is meant to be called once in your onCreate() method. however, couple of reasonable ways to do it,
encapsulate each layout in a fragment, then show / hide each fragment as needed.
bundle both layouts into a single layout, and show / hide each section of the layout by calling setVisibility() on the layout's outermost container.