This is what I want to have:
Populate a ListView with data I request from a server
Populate the ListView to a Navigation Drawer
I have populated the ListView with the data I requested and it's not empty. However I can't get it to populate the ListView to the Navigation Drawer.
ListView commentsList = (ListView) findViewById(R.id.right_drawer);
CommentsAdapter adapter = new CommentsAdapter(HomeCategoryActivity.this, R.layout.comment_single, comments, fname, lname);
commentsList.setAdapter(adapter);
The above code represents this:
And it's XML which is in main Layout:
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#2B2B2B"
android:choiceMode="singleChoice" />
That adapter works and I populate the ListView successfully and not null. And then I assign a Navigation Drawer:
DrawerLayout rightDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_cat);
rightDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
XML of that:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout_cat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="activity.classes.HomeCategoryActivity">
And finally I dont know how to attach the ListView to the Drawer..
What am I doing wrong? I think it does not require an adapter to assign the ListView to the Drawer?
You should place the ListView as the second child of the DrawerLayout The first child will be the content when the drawer is off-screen and the second child will be the content visible when the drawer is on-screen (aka the drawer itself).
Also, by doing this:
commentsList.setAdapter(adapter_right);
You are resetting the adapter (the source of the data) on the list view so you will not see the data you originally had set with your CommentsAdapter.
See the example xml file on the Google DrawerLayout Dev Page.
Related
I have a navigation drawer using fragments created from the default "Navigation Drawer Activity" template in Android Studio. Inside a fragment A, I want to open another fragment B. So the fragment B must replace fragment A. I saw on google, that I should use FrameLayout to Dynamically change the current displayed fragment.
<FrameLayout
android:id="#+id/nav_host_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</FrameLayout>
To change fragment, I am using the code below, when I am clicking on a button in the fragment that I want to be replaced
NewCalendarEvent newEventFrag = new NewCalendarEvent();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_frame_layout, newEventFrag)
.addToBackStack(null)
.commit();
Currently, when I click on the button, the two fragments are show. Is because I am using fragment and frameLayout together ? What I am doing wrong ?
If you are using a NavHostFragment and the Navigation Component, then you never use a FragmentTransaction. Instead you navigate to a screen by using the APIs on NavController.
We're making an android app, and there is something we want to add.
Which is the effect the Gmail app has.
You can choose which account you want to view (and the rest of the app will behave accordingly).
EDIT:
I now already have a (working) navigation bar, but the things I want are the round icons in the header. I want someone to be able to choose the user they are viewing.
The effect you want can be achieved by using NavigationView from the com.android.support:design support lib.
You can find a full tutorial on that here. And you can download the full source code from that tutorial here.
And here's another nice tutorial that you could follow.
But long story short, that view is split between two main parts, a header and a menu part, and each one of those you'll have to define on XML.
As from that tutorial:
Header View
This View is basically the top part of the navigation
drawer, which holds the profile picture, name and email etc. You need
to define this in a separate layout file we would look into that in
just a moment.
Menu
This is the menu you want to show below your header, we define
menu in a menus folder, just like you define menu for your overflow
menu. So basically NavigationView is a container for the Header View
and Menu which you are going to use in your sliding drawer. So now
that you understand the NavigationView we can start building our
Navigation Drawer.
With that in mind, build your header as you would do with any other layout. And the Menu is defined somewhat like the Toolbar/ActionBar menu. e.g.:
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:checked="true"
android:icon="#drawable/icon_home"
android:title="#string/title_home"/>
<item
android:id="#+id/drawer_content"
android:icon="#drawable/icon_content"
android:title="#string/title_content"/>
<item
android:id="#+id/drawer_about"
android:icon="#drawable/icon_about"
android:title="#string/title_about"/>
<item
android:id="#+id/drawer_exit"
android:icon="#drawable/icon_exit"
android:title="#string/title_exit"/>
</group>
</menu>
Then, on your Activity you'll just have to make a layout like the one found in the tutorial, using the DrawerLayout along with NavigationView.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"/>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/navigation_menu"/>
</android.support.v4.widget.DrawerLayout>
You'll also have to create some Fragments for each screen you want to display with this NavigationView. After you've done that, on your Activity you can handle the selection events by implementing NavigationView.OnNavigationItemSelectedListener, like this:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
// Your Activity
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Fragment fragment = null;
switch(menuItem.getItemId()) {
case R.id.drawer_home:
fragment = new YourFragment();
break;
case R.id.drawer_content:
fragment = new AnotherFragment();
break;
case R.id.drawer_about:
fragment = new AboutFragment();
break;
case R.id.drawer_exit:
// TODO - Prompt to exit.
finish();
break;
}
if (fragment == null) {
fragment = new YourFragment();
}
drawerLayout.closeDrawers();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame, fragment)
.commit();
return true;
}
}
As for your edit, the icons could be represented by an ImageView. And to navigate between multiple profiles, it depends on how you've implemented that logic on your app, but as a "generic" answer, you could switch those profiles using something like a Spinner.
Those tutorials will help you through that step:
Android spinner (drop down list) example
Android - Spinner
Basic Spinner example (Stackoverflow question)
Spinners (Android dev guide)
Once you've set that up on your header, handle the item selection and change the user profile accordingly. (This last part depends ENTIRELY on how you've implemented user profiles on your app). But just as a head start, you could check the android training site, more specifically, this part.
You should use NavigationView
It provides the framework for easy to implement material navigation
drawer with the help of inflate navigation items through menu
resource. Befor Navigation View, we have hard way to make material
navigation drawer using listview or linearlayout with custom adapter,
but now we just need to add Navigation View in DrawerLayout,
everything else will be handled by Navigation View.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your contents -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
For this requirement You can check sample
MaterialDrawer
How To Make Material Design Navigation Drawer
Playing with NavigationView
Hope this helps .
I think this MaterialDrawer is what you're looking for. This library has a lot of examples. You can either use this library directly or read the source code and implement your own drawer.
You can implement this Material Navigation drawer using MaterialNavigation library.
Article about implementation is here.
You will just have to import that library and you're done. See demo code on below site:.
https://github.com/PatilShreyas/MaterialNavigationView-Android
I currently have some swipe pages and in all of them I want to have list with different data.
I am still pretty new to android so I tried all possible options I could come up with:
First I tried adding some xml layouts to the insides of the page.
Second I tried creating and populating lists pragmatically.
None of the above worked out for me.
For starters I want to check if I understand the concept:
Swipe layout contains pages and data could be passed to them by getItem() Fragment type content.
And now how to achieve what I want:
I have to add LinearLayout add a child ListView and finally add a child for that the TextView element to main page template in my layout/main.xml and create a data population method.
Is there something wrong in my reasoning and that is why I can't find answer or I just have not found the right source?
CODE EXAMPLES:
main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
MainActivity.java
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
...
You can do this by declaring an xml say listview.xml in which define your ListView.
You can either create a new TextView pro grammatically or create the new layout say titleview.xml in which write your title view using TextView.
Pro grammatically you can inflate both the views and pass your main layout parent view as parent to these child views.
LayoutInflater inflater = getLayoutInflater();
ListView listview = inflater.inflate(R.layout.list_view, parent);
TextView textview = inflater.inflate(R.layout.textview, parent);
layout.addView(textview);
layout.addView(listview);
I made a list view by the android.com's tutorial, and its in a tab widget, but the problem is that its spreading all over the tab's area and I want to create 2 lists in one tab, one beside the other... how can I do that?
here is a link to the tutorial:
http://developer.android.com/resources/tutorials/views/hello-listview.html
EDIT: ok that what I have in the onCreate method, there are 2 static final String arrays in this class called CONTACTS and FAVORITES, I copied MH's layout to an xml file called list_who and changed the IDs to lv_cons and lv_vav, and another layout called tv (TextView) that only have a text view because that what the adapter requires
super.onCreate(savedInstanceState);
ListView lv_cons = (ListView) findViewById(R.id.lv_cons);
ListView lv_fav = (ListView) findViewById(R.id.lv_fav);
lv_cons.setAdapter(new ArrayAdapter<String>(this, R.layout.tv, CONTACTS));
lv_fav.setAdapter(new ArrayAdapter<String>(this, R.layout.tv, FAVORITES));
setContentView(findViewById(R.layout.list_who));
If you want two display two lists side-by-side, there are a few things you'll need to change.
First the (minimal) layout for the tab's content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/left_listview"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ListView
android:id="#+id/right_listview"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
If you followed the Hello ListView sample code you linked, you'll have to change your ListActivity back to Activity. The former basically offers a number of convenience methods for working with a single list, but you can do exactly the same with a regular activity, which you'll need to work with multiple lists.
You can inflate your ListViews like any other view by using their ids (there is no more getListView() convenience method, since we're not extending ListActivity anymore)`:
ListView leftList = (ListView) findViewById(R.id.left_listview);
ListView rightList = (ListView) findViewById(R.id.right_listview);
From that point on you can add all stuff to populate/manipulate the lists' contents.
//Edit: If you insist on creating the layout programmatically, simple convert above xml code to it's Java equivalent:
// Create ViewGroup as container for both lists
LayoutParams rootParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LinearLayout root = new LinearLayout(this);
root.setLayoutParams(rootParams);
// Create LayoutParams for the lists - both identical in this example
LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT);
listParams.weight = 1;
ListView leftList = new ListView(this);
ListView rightList = new ListView(this);
leftList.setLayoutParams(listParams);
rightList.setLayoutParams(listParams);
// Add lists to container
root.addView(leftList);
root.addView(rightList);
// Set container as content view
setContentView(contentView);
If you're looking to handle two separate ListViews in separate tabs (i.e. a ListView in each tab), then follow this tutorial: Android TabActivity with two list views.
You simply need to make one composite layout and extract your tabHost from the xml programmatically and then inflate the tabs on the fly. I further suggest using this method if you want two lists to communicate with each other via an Intent. Both tabs would be hosted in one activity. If I misunderstood your question and you want to display two separate, adjacent lists within one activity..then disregard my answer.
I think this is what you're looking for. Good luck.
I have four tabs that hold four listviews, I want to set a background for each list view but whenever I try to add the background it puts the image in each cell of the listview instead of behind the list.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/pre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="21sp">
</TextView>
I've realised that this is beacuse I've tried to add the background in a textview so it adds the image in each cell in the listview, so I have tried to add a linearlayout, listview and a imageview and put the background there but it force closes. I think this is becuse the tabhost uses main.xml to draw the main page and it conflicts, so I even tried to add the listview there still it nforce closes, it will only work if I have a textview only, howe can I add a background to each listview, below is the listview code;
public class prem extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Pre"};
ListView lv = getListView();
lv.setCacheColorHint(00000000);
lv.setAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, names));
}
Okay, your XML layout files are going to be used in the setContentView() method. You haven't posted the code for your activity which includes the TabHost, but I'll assume you're using the default setContentView(R.layout.main);. If you're NOT using setContentView() (in the case of your ListActivity), adding a ListView to an XML file isn't going to change anything, because it's never being used.
You are correct in that you're having the issue because you're setting the background of the TextView. Since you're using a ListActivity, you'll need to set the ListView's background using code. ListView is a subclass of View, so you can use the methods from the View class to set your background resource for the ListView.
For example:
ListView listView = getListView();
//set background to color
listView.setBackgroundColor(#FF888888);
//set background to Drawable
listView.setBackgroundDrawable(myDrawable);
//set background to Resource
listView.setBackgroundResouce(R.id.my_res_id);
As per JonniBravo, adding....
If you have an XML layout file that you are using to set the view by using setContentView() in your activity, you can have a ListView defined inside that.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lists_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/paper_frame" />
</RelativeLayout>
The activity can "find it" using
lv = getListView();
As shown in the sample above, you can set the background for the ListView with the "background" attribute. As usual, this can be a valid drawable (e.g. a color, or an image). In my example it happens to be a 9.patch image that is stretched by Android to enclose the list, providing a frame.
Good luck.