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
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 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'm having a design issue. I'm not sure if this is a good way to do it but here is what I want to do.
I want a way for the user to quickly swipe or click on a button to change the data that is shown in a fragment which contains a list. I thought of using tabs but it seems that's more suited if you have different fragments for each tab and using only one fragment might not be best practices.
The reason I only want to use one fragment is because that code will not change for different tabs only the data that is given to the recyclerview.
Should I go forward with this or is there a better method?
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 am working on an Android project where a group of buttons needs to show on the bottom of every screen (activity) in the application. The group of buttons are basically a navigation bar. I want to know the best way to do this without creating new buttons for every activity. I have been around programming (C++/C#) for many years but am pretty new to Android and Java so if someone can point me in a general direction, it would be appreciated.
Thanks.
I bet you need to use "include" tag for xml layouts. It's the best when you need to reuse some UI components. See http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ for the examples and description.
To elaborate on Konstantin's answer, after you've used include, you'll need to bind actions to these buttons.
If the buttons should have the same action regardless of the activity they are in, use the include tag to create their layout and then create a parent NavigationActivity (or whatever else you want to call it) class from which all your other activites will inherits.
In the parent NavigationActivity class' onCreate method, you can set up the onClickListener (and other needed stuff) for the buttons.