Android: navigation drawer without xml - java

Is it possible to create and add to app Navigation Drawer without using xml?
Every example i saw had an additional xml file with layout.
Nowadays i try to initialize this with:
DrawerLayout sidePanel = new DrawerLayout(this);
and then add this sidePanel using View method addView() but it cause exception.
Is it possible to add Navigation Drawer using only java code?

yes , you can do that without the XML, but still you would require a layout to at least hold that navigation drawer that you need

Related

Android Dev: Navigation View vs App Bar

I'm new to Android development. I get that a NavigationView is an AppBar (i think). What makes these two different? I want to create a custom "appbar/toolbar" with a centered logo and menu/settings button in the top right corner (instead of the standard left) which reveals a drawer.
I was going to ditch the built in appbar/toolbar all together and just create my own somehow and include button overlay which displays a drawer.
What would you do? Navigation view, app bar, or custom toolbar from scratch? I don't know what the standard is or what is acceptable. What is the difference between a navigationview and appbar. Thank you.
edit: I'm slowing realizing that an appbar is one feature within a navigation view, among others like a drawer layout, menu items etc... i think.
1. NavigationView
By using NavigationView, we can bind the menu directly with NavigationView. This is the benefit of the NavigationView. No need to create ListView and adapter with navigation drawer. By default we can get selector of item click. With menu we can change the color of icon of selected menu.
For more details :
1.https://developer.android.com/reference/android/support/design/widget/NavigationView.html
http://www.technotalkative.com/part-4-playing-with-navigationview/
2. AppBar
Appbar is for toolbar with scrolling effect. We can easily give the material design effect.
For more details :
1.https://developer.android.com/reference/android/support/design/widget/AppBarLayout.html

Android navigation through multiple layouts

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

where to get R.id.container from in Android?

I am new to android. I have created a new android project selecting tabs as a template Eclipse implements tabs and pager itself. Each tab view has a fragment. Then I implemented ListView in a fragment.
Now when a list item is tapped, new view (fragment) should be open. To open a new fragment view on clicking list item, I need to give R.id.fragment_container to it. this id tells us on which container fragment view will be shown.
My Question is where should I get R.id.container as there is no container is defined with this id in the Main Activity. I have tried many things came in google like to add a layout (Relative/Linear) as well but it crashes and also I have tried to give pager id as well but nothing happens.
Think i have some understanding problem with fragments. Any explanation/help specific to my case would be highly appreciated. Thanks
R.id.fragment_container or R.id.container are the IDs of a View in your MainActivity's layout file. Open that activity_main.xml (or whatever it is called) and add a container to hold your fragment.
You'll need to add the android:id="#+id/fragment_container" line to the View/Layout you choose.
add andriod:id="#+id/id_name_". in your fragment layout and call IT wherever u want

Navigation Drawer app behaivour when open fragments

Im working with a Android app that uses Navigation Drawer. This app use the template provided with Eclipse ADT (when you select the navigation drawer template).
I dont understand the behaivour of the navigation drawer very well.
My main requeriment is make a "search" option, I have on the Navigation Drawer a EditText to get the query string from the user. I want that I press search button and open my SearchFragment getting the search query.
I know that I can make this:
Bundle args = new Bundle();
args.putString("searchQuery",searchQuery);
fragment.setArguments(args);
And this for getting:
getArguments().getString("searchQuery");
But I have the next:
MainActivity
NavigationDrawerFragment
SearchFragment
OtherFragments.. (I think this arent relevant in the question)
I dont understand where I can make this steps.
More data:
I have in the navigation drawer class the EditText with the setOnEditorActionListener for the search press.
Any info that I can add I will be add. Sorry my english
Thanks for the help
Navigation drawer is basically a component that show 2 pane in the way that one is the main pane (usually fragment) that is used to show content and the other one is usually a listview that is just used to choose which fragment to show in main pane
so if you want to switch between 2 different layouts you have to create 2 different fragments and then you can add or replace these fragments based on listview's selected item.
You may refer to below link for complete code
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Hope it helps!

navigation bar expandable list view android

I've been trying to recreate a navigation drawer looking something like this:
http://developer.android.com/design/media/navigation_drawer_holo_dark_light.png
It appears to just be an expandable list view within a navigation drawer but the trouble is I'm a bit of a newbie so i'm not sure on how to do it, any advice on how I would go about creating something like this?
Try this tutorial, you'll learn as well.
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
As for the Navigation Drawer implementation, use a Menu Drawer library, it will make it much easier.

Categories

Resources