How to add title in Navigation drawer layout? - android

[UPDATE]
I solve the problem by adding addHeaderView :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
LayoutInflater inflater = getLayoutInflater();
ViewGroup mTop = (ViewGroup)inflater.inflate(R.layout.header_listview_menu, mDrawerList, false);
mDrawerList.addHeaderView(mTop, null, false);
================================
My question is so simple !
I would like to how to add a title in a navigation drawer ?
I already created my navigation drawer with listview (icon+text) for each item.
Thanks a lot,

You would do that the same way as you would add headings in any other ListView, by teaching your ListAdapter to return heading rows as well as detail rows. At the low level, this involves overriding methods like getViewTypeCount() and getItemViewType() in your ListAdapter, plus having getView() know the difference between the row types. Or, use an existing high-level implementation like https://github.com/emilsjolander/StickyListHeaders or http://code.google.com/p/android-amazing-listview/ or any of the others found when searching for android listview headers.

Put a TextView above a ListView, and wrap it inside a vertical LinearLayout . Give to your ListView android:layout_weight="1" and android:layout_height="0dip"

Maybe it's a bit late but I think I have a simpler solution. In your Activity's layout, instead of adding a listView inside the DrawerLayout, you can add for example a LinearLayout, and you can easily add separators and rows. For example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.astuetz.viewpager.extensions.PagerSlidingTabStrip
android:id="#+id/indicator"
android:layout_height="48dip"
android:layout_width="fill_parent"/>
<ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Separator 1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First button"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Separator 2"/>
</LinearLayout>
And in the Activity, you can add the listeners to the buttons.
Hope that helps!

Related

Unable to show hidden RelativeLayout

I have a xml with one NavigationView and two different RelativeLayouts.
One of the RelativeLayouts is hidden at first (I change its visibility in the onCreate() to GONE).
When I click on a button inside the NavigationView I want the hidden layout to become visible.
This is my Java code:
RelativeLayout imagenes,registro;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invitado);
registro = (RelativeLayout) findViewById(R.id.registro);
imagenes = (RelativeLayout) findViewById(R.id.imagenes);
registro.setVisibility(View.INVISIBLE);
imagenes.setVisibility(View.VISIBLE);
mDrawerlayout= (DrawerLayout) findViewById(R.id.drawer_layout);
mToggle= new ActionBarDrawerToggle(this,mDrawerlayout,R.string.open,R.string.close);
mDrawerlayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void registrarse(View v){
mDrawerlayout.closeDrawers();
registro.setVisibility(View.VISIBLE);
}
And here the xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fff">
<android.support.design.widget.NavigationView
app:headerLayout="#layout/popup_header"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/popup_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
<RelativeLayout
android:id="#+id/imagenes"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_marginTop="50dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:src="#drawable/cloud"
android:layout_gravity="center"
android:contentDescription="" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/registro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80FF4081">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/registrate"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="26sp"
android:textStyle="bold"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
I could fix your code.
1 - Fix your DrawerLayout
DrawerLayout layout file must have an specific format:
<DrawerLayout>
<Content Layout /> <!-- It can be linearlayout, framelayout etc -->
<NavigationView />
</DrawerLayout>
You have 2 RelativeLayouts and one NavigationView. You have more items than allowed and in wrong order. So, for tests, I moved both imagenes and registro into a LinearLayout` (you could improve and keep all items inside only one relative layout.. but lets leave that for another time).
I did not change anything... I just move NavigationView to the bottom of the file and move both RelativeLayout into a LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/imagenes"
... >
....
</RelativeLayout>
<RelativeLayout
...>
.....
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
.... />
</android.support.v4.widget.DrawerLayout>
2 - Fix your code
Since both RelativeLayout were placed in a LinearLayout and it is vertical aligned, View.INVISBLE does not work anynore for us. So, you should use View.Gone and change your code as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
....
imagenes.setVisibility(View.VISIBLE);
registro.setVisibility(View.GONE);
....
}
public void registrarse(View v){
mDrawerlayout.closeDrawers();
imagenes.setVisibility(View.GONE);
registro.setVisibility(View.VISIBLE);
}
3 - Another possibilites
Since your DrawerLayout was not properly configured, I wonder if your navigations items were being called. So, maybe, your code is not even calling registrarse(View v).. However, but that is hard to cofirm with current code attached.
For any case, with the modifications I mentioned above and if your code is calling registrarse properly, it should work normally.
You are not seeing the RelativeLayout after changing it's visibility to View.VISIBLE because you have set the height and width of both RelativeLayout to match_parent , so the imagenes occupies whole screen and it is on top of registro.
Try putting them inside a ScrollView or adjust their heights or call registro.bringToFront() after making it visible.
height and width of imagenes RelativeLayout is match parent so it takes whole screen..u need to set appropriate height to see registro RelativeLayout or put it in ScrollView

Android Navigational Drawer with Textviews, Buttons

I am trying to create a personal details page where the user can view and change his/her personal details. I followed a tutorial and created a Navigational Drawer which loads a ListView. The thing is that i need to add a few TextViews and 1 button but no matter where i put them in the xml, they keep showing outside the drawer, messing up the whole layout. Any help? Thanks for your time everyone!!!
GuestMain.java
public class GuestMain extends Activity {
/////////NAVIGATIONA LDRAWER///////
String[] info;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
///////////////////////////////////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guest_main);
addListenerOnButton();
////////////////////////////////////////NAVIGATIONAL DRAWER//////////////////////////////////////////////
info = new String[]{"Full Name","Phone Number","Email","Address","Country","City"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,info);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.darker_gray);
/////////////////////////////////////////////////////////////////////
}
activity_guest_main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="com.example.domotel.GuestMain" >
<ImageButton
android:id="#+id/iconback"
android:contentDescription="#string/btn_back"
android:src="#drawable/icon_back"
android:layout_width="40dp"
android:layout_height="25dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="11dp"
android:scaleType="fitStart"
android:clickable="true"
android:background="#e5b53a" />
<ImageButton
android:id="#+id/iconuser"
android:contentDescription="#string/btn_back"
android:src="#drawable/icon_user"
android:layout_width="40dp"
android:layout_height="25dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="12dp"
android:scaleType="fitStart"
android:clickable="true"
android:background="#e5b53a" />
</RelativeLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#d9ffffff"/>
<!-- 85% Opacity -->
</android.support.v4.widget.DrawerLayout>
Not sure if I completely understand what you are trying to do, but here is the way that I understand what your saying. You want to have the ability to edit items inside the drawer and then have the changes reflected outside. Don't really think that this is what the drawer was designed for. But in order to have the text boxes inside the drawer with the strings you could create another xml layout file with the list view containing a string and a text box. Then in place of android.R.layout.simple_list_item_1 you would use your drawer_layout from your xml layout file
adapter = new ArrayAdapter(this, R.layout.drawer_layout, R.id.text_field, info)
Then you need to handle the text and layout from within the drawer

How to add sections in a ListView (android)?

I read this tutorial to create a Navigation Drawer with a ListView.
Link: http://developer.android.com/training/implementing-navigation/nav-drawer.html
How can I divide my ListView into sections and add a title for each section? (Like facebook application). I read many examples and tutorials, but no one is very clear.
Must I do override the getView method of an Adapter? Or..
I hope someone knows how to answer clearly. Thanks!!
This is my code:
public class NoLeague extends Activity{
private ListView listView;
private DrawerLayout drawerLayout;
private String[] items = {"Lega_1","Lega_2", "Option", "Logout"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setBackgroundDrawable(getResources().
getDrawable(R.drawable.banner));
getActionBar().setTitle("TITOLOdiPROVA");
setContentView(R.layout.noleague_layout);
listView = (ListView) findViewById(R.id.left_drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
listView.setAdapter(new ArrayAdapter<String>(this,
R.layout.item_layout, items));
}
}
This is the item_layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
This is the layout of my Navigation Drawer:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/noleague_image" >
</LinearLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#575555"
android:dividerHeight="1dp"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
If you search for Sectioned Listview! you'll find many examples,
You check out Amazing Listview! and Expandable Listview!
Hope this helps!!
This is pretty slick Using multiple layouts in Listview. It's for those who want to build something of their own. the Amazing Listview and Expandable Listview are ready to be used

Android Layout with ListView overlaps Buttons

I can't seem to find a way to make a layout that contains a ListView that show the entire list of my contacts (its a long list), with 2 buttons one next to the other below the list.
its freaking me out, nothing i do seems to work. i searched some related quiestions and found this one: Android Layout with ListView and Buttons
but none of the alternative solutions over there is working for me.
when i look at the graphical presntation (of eclipse) it looks fine, but when i run it on my device i can only see the list of contacts. the listView overlaps the buttons.
I am using a simpleList that render on another layout that represent a row. i'm starting to suspect something i do programtically ruins it, because i ran over my java code and found no mention of the activity_contact_list that i was talking about. only connection to the row layout.
so i got this activity_contact_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_alignParentTop="true" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1.0"
android:orientation="horizontal" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
</LinearLayout>
this is after many tries such as: using RelativeLayout instead of the LinearLayout, puting the ListView's code below the LinearLayout of the buttons, and many more..
and here is the code of the activity: ContactList.java:
public class ContactList extends ListActivity{
//some global variables for late use.
public Model model;
SimpleAdapter adapter;
List<Map<String, String>> data; /* data is a list of Map */
String[] from = {"name","phone"}; /* names of the columns */ //TODO - add date!
int[] to = {R.id.name, R.id.number};
List<Contact> contacts; /* list of contacts */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_contact_list);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayHomeAsUpEnabled(true);
model = Model.getInstance(this);
data = new ArrayList<Map<String, String>>();
contacts = fillContactsList();
fill_data();
adapter = new SimpleAdapter(this, data, R.layout.contact_list_row, from, to);
setListAdapter(adapter);
}
i commented the setContentView line cause it doesnt compile otherway...
As is said. the list works perfactly. but it overlaps the buttons at the bottom of the activity. any idea? i'm sure its something silly i'm missing..
thanks in advance!
Did you try to use Relative Layout ?
Change Linear Layout to Relative Layout that's all.
As Linear Layout Doesn't support align parent..
Edited
set List Height to mach parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_below="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
How long is your ListView. Maybe it is longer than your screen and is the LinearLayout with buttons shown outside your screen.
What happens when you rearrange with the buttons above the ListView.
EDIT:
I think you have to set a Max-size in your ListView. Maybe you can try something like this in your java-code, but I don't know if it is scrollable then...
ListView listView = (ListView) findViewById(R.id.listView):
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listView .getLayoutParams();
params.height = 200; // or something bigger / smaller
listView .setLayoutParams(params);

Force EmptyView to display?

I have a ListView that I am creating. The content is set from a separate adapter class. When the adapter returns zero items I want the empty view to display from a separate xml. This is happening inside a SherlockFragment. However for some reason I cannot get the emptyView to display.
Help Please?
Code:
RelativeLayout fragView = new RelativeLayout(getActivity());
ListView listView = new ListView(getActivity());
listView.setEmptyView(getActivity().findViewById(R.id.emptyView));//This is having no effect
scoreViewAdapter = new ScoreViewAdapter(getActivity(),
databaseHelper.getAllTimeRecords());
listView.setAdapter(scoreViewAdapter);
if (scoreViewAdapter.getCount() < 1)
listView.//can I set something here to force or change my view? From the methods available to ListView I don't see any potential solutions..
fragView.addView(listView);
Here is the xml....pretty standard.:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/emptyText"
android:src="#drawable/ic_android_logo" />
<TextView
android:id="#+id/emptyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:text="No Items Found"
android:textAppearance="?android:attr/textAppearanceLarge" />
Thanks to Faizan's feedback to lead me here. Here is the solution:
View empty_view = inflater.inflate(R.layout.empty_view, null);
...
if (scoreViewAdapter.getCount() < 1)
fragView.addView(empty_view);
fragView.addView(listView);
You have to inflate that xml using LayoutInflater and then call
listView.setEmptyView(inflated_view);
You can get get the inflater from the onCreateView overrided method anc do
View inflated_view = inflater.inflate(R.layout.my_empty_layout,null);
I hope it will resolve the problem.
I believe you extends FragmentList and not ListActivity
so you actually can use setEmptyView

Categories

Resources