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.
Related
i have this app that i want to have a specific way of navigation. And so i made a research but i got confused. And i am begginer in android development.
i want to ask what kind of layout or anything i can use to achieve that. I dont search for super specific answer, just what is the thing that is doing the job i want. So there it is:
the blue block("choose a brand" view) has to be on that position at all times and only changing the text if needed. I want when one image button is clicked the whole green block with the image grid change into another xml layout. I want to call multiple layouts in the green block when i interact with the buttons of those layouts. Atm the green block is an <include layout ="layout.xml"/>
I really apreciate any answer. Sorry if is basic but i really tried to find the thing i need but so far i see solutions that prevent me from using simple inflaters. Thanks in advance
You have to use fragments for this scenario.
You will have a LinearLayout where the first element will be your blue block.
The second element will be a FrameLayout that you will change into the Fragment you need (usually it will have the ID container).
Create a Fragment and set the layout your layout.xml file.
Create a second Fragment with the desired layout you would like to change the green block.
As you click on a imageButton you will have to change the current Fragment with the desired one. Here you will see how to send objects to fragments.
You can find here a way to switch between fragments. In the ft.replace method the first id is the FrameLayout you will use as a container(see above).
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
Be careful when you import fragments. If you use the support package you will have to use getSupportFragmentManager();
Read more on the android developer about fragments
I think what you are looking for is Fragment class, which can be used as reusable code that can be linked to a UI/layout.
From android documentation:
To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the Fragment class, which behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle.
Read more here
I would like to create an user interface that always displays a menu panel on the left side, and a content block which changes depending on which menu entry is selected, i.e something similar to many websites.
I am new to creating Android user interfaces so I am wondering how to properly and dynamically change the content.
Is it better to create two different views, each of them including the same panel fragment (meaning 2 instances ?) and a different content fragment, and change that view using Activity#setContentView(), or should I create a single view and replace the fragment of the content dynamically ?
You should definitely use the Fragment approach.
Use a FragmentActivity and use the FragmentManager (getFragmentManager in the Activity) to create a transition and replace the Fragment depending on what item the user clicked.
See: http://developer.android.com/training/basics/fragments/fragment-ui.html
Use this below code with library from github it will answar you perfectly
https://github.com/neokree/MaterialNavigationDrawer
I have an Activity that displays five custom compound views. Within these views there are a couple of EditTexts and a Spinner.
When anything changes within any of the five compound views, I want my Activity to fire a method. So I need a listener in my Activity that listens for any change inside the Compound views. I would prefer not having to check every single control inside the compound views, but just notify my Activity that a view has changed state somehow.
Any ideas on how to do that?
I am new to Android development. I want every activity has a same customized popup menu on the top right. I use a LinearLayout in xxmenu.xml and include it in each activity's layout xml files. The display is OK. As the OnClickListener should be same, I don't want to implement the OnClick functions in every activity's Java file. How can I do this?
I know Android has inherent menu solution via OptionsMenu, but I don't know if it can be customized or how (e.g., I want it on topright corner, with different backgrounds, each item has an image icon followed by texts, one-side shadow, etc.)
Make an abstract BaseActivity class that implements onCreateOptionsMenu and onOptionsItemSelected for your common menu and menu items. Then make all your other activities extend from that one.
First, the solution is to define a base class (e.g., BaseActivity) to extend Activity, and the actual activities using this menu to extend BaseActivity (e.g., MainActivity extends BaseActivity).
Second, use a separate xml to define the menu layout and include it in other layouts. A simple way to bind the listeners is to use android:onClick="toggleMenu".
Third, implement public void toggleMenu(View v) in BaseActivity.
We can also call the views and do the bindings in Java code. But I met a code sequence problem that if I get the clickable view via findViewById in BaseActivity's onCreate(), it returns null, and the binding fails. So the finding of the views and following bindings should be done in the child classes (e.g., MainActivity), after calling super.onCreate().
I'm converting all my Activities to Fragments so that I can use them in a ViewPager.
I've searched for this but I couldn't find a satisfying answer, so that's why I'm asking it here.
In my Activities, I've written some code in the onCreate() method. I for example call some findViewById()s in order to link some xml-buttons to my Activity. I also make some views invisible in the onCreate(), set an OnClickListener(), fill a TextView with text and remove a Notification, all in the onCreate() method.
My question is: Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?
Many thanks in advance!
Although Pragnani's answer is close, there's little educational value in it. Besides, there's a more appropriate option to his 2nd statement.
Where should I put this code in the fragment? In the onCreate()?
onCreateView()? onActivityCreated()? and why?
The short answer is: either onCreateView() or onActivityCreated() will do. The view hierarchy won't be created until onCreateView(), so that's the earliest point in the fragment's life cycle that you could inflate the views and attach click listeners etc. Since onActivityCreated() will always be run after onCreateView(), that's a suitable location too. onCreate() may be skipped in favour of the system temporarily detaching the fragment and reattaching it, e.g. when retaining fragments.
Pragnani is correct by pointing out that inflating the views of a fragment is slightly different from inflating views in an activity. More specifically: a fragment does not define a findViewById() method, so you'll need to call it on some other object.
Rather than using getActivity().findViewById(), you'll want getView().findViewById(). The reason for this is that if you use the activity for the view lookups, then you'll get into trouble when multiple fragments with the same view IDs are attached to it. This will be the case if you reuse view ids in the layouts of your various fragments, or if you show two identical fragments that display different data. In both cases, only the first match would ever be returned, whereas you really want to the view to be looked up in the conext of the fragment. That's exactly what getView() returns, the fragment's root view (that you returned in onCreateView()), and thus limits the scope of the lookup appropriately.
1.Left the onCreate empty and just call super.onCreate()
2.Instead of findViewById() use getActivity().findViewById()
always use getActivity() where you need context of the view.
Do all other operations in onCreateview()