How to close navigation drawer through touch on the Screen in android - android

I have success fully open 4 different navigation drawer from left side when I click 4 different background button. Now when I click 1st button the 1st navigation drawer opening and closing as well. But problem is that when I click 2nd button drawer is opening but not closing onclick of list item and touch outside of the drawer. See below image want.
see code public class MainActivity extends Activity {
private ListView mDrawerList;
private CharSequence mDrawerTitle;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private RelativeLayout leftRL;
private RelativeLayout rightRL;
private RelativeLayout byleft;
private RelativeLayout toleft;
private DrawerLayout drawerLayout;
final String[] data ={"one","two","three"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// I'm removing the ActionBar.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
final ListView navList = (ListView) findViewById(R.id.drawer);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
// Recycle the typed array
navMenuIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
leftRL = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
rightRL = (RelativeLayout)findViewById(R.id.YouWantInRightDrawer);
byleft = (RelativeLayout)findViewById(R.id.WantInRightDrawer);
toleft = (RelativeLayout)findViewById(R.id.InRightDrawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
}
public void onLeft(View view)
{
drawerLayout.openDrawer(leftRL);
}
public void ToLeft(View view)
{
drawerLayout.openDrawer(rightRL);
drawerLayout.setFocusableInTouchMode(true);
}
public void byLeft(View view)
{
drawerLayout.openDrawer(byleft);
}
public void throughLeft(View view)
{
drawerLayout.openDrawer(toleft);
}
}
<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
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#0F0FF0"
>
<Button
android:id="#+id/home_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#drawable/ic_home"
android:onClick="onLeft" />
<Button
android:id="#+id/profile_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/home_btn"
android:background="#drawable/ic_people"
android:onClick="ToLeft"
android:clickable="true"
/>
<Button
android:id="#+id/chat_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/profile_btn"
android:background="#drawable/ic_photos"
android:onClick="byLeft"
android:clickable="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/chat_btn"
android:background="#drawable/ic_whats_hot"
android:onClick="throughLeft"
android:clickable="true"
/>
</RelativeLayout>
<!-- Left Drawrer -->
<RelativeLayout
android:id="#+id/YouWantInRightDrawer"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="left" >
<ListView
android:id="#+id/firsr_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/WantInRightDrawer"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="left" >
<ListView
android:id="#+id/second_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/InRightDrawer"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="left" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_dark" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/whatYouWantInLeftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/list_background"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

Hi friends i got the solution see code below
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Drawer1" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Drawer2" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Drawer3" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Drawer4" />
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#android:color/black" >
</ListView>
</android.support.v4.widget.DrawerLayout>
Java:
public class MainActivity extends Activity {
private Button btn1, btn2, btn3, btn4;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
String[] string = { "one", "two", "three", "four", "five" };
String[] string2 = { "one", "two", "three" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_drawer_layout);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
string);
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
mDrawerList.setVisibility(View.INVISIBLE);
} else {
mDrawerList.setAdapter(arrayAdapter);
mDrawerLayout.openDrawer(mDrawerList);
mDrawerList.invalidate();
}
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1,
string2);
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
mDrawerList.setVisibility(View.INVISIBLE);
} else {
mDrawerList.setAdapter(arrayAdapter1);
mDrawerLayout.openDrawer(mDrawerList);
mDrawerList.invalidate();
}
}
});
}
}
Happy coding!!!!

Related

I have added slider menu in android app. But unable to open after clicking side menu

I have added slider menu in android app. But unable to open after clicking side menu. after clicking on side menu it is not opening list. When i debugged i found that adapter =NULL & mdrawertoggle=NULL.
I am adding code please provide me solution if anyone have.
Thanks.
Attached image is showing side menu but unable to get list after clicking .
MainActivity
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adater ;
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// data of json url
private static final String url = "http://milagro.in/wip/apps/n/THDC2.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
//mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
//actionBar.setDisplayHomeAsUpEnabled(true);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Profile
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// About
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Emi Calculator
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Currency Converter
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// PayInstallments/EMI
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
// Social Feed
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
// Feedback
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
//Settings
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adater= new NavDrawerListAdapter(getApplicationContext(),navDrawerItems);
mDrawerList.setAdapter(adater);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.menu, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
//displayView(0);
}
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("tata_project_name"));
movie.setThumbnailUrl(obj.getString("project_logo_url"));
movie.setParkingUrl(obj.getString("parking"));
movie.setPowerbackupUrl(obj.getString("powerbackup"));
movie.setFitnessUrl(obj.getString("fitness"));
movie.setLiftUrl(obj.getString("lift"));
movie.setParkUrl(obj.getString("park"));
movie.setSecurityUrl(obj.getString("security"));
movie.setSwimmingUrl(obj.getString("swimming"));
movie.setTypology(obj.getString("project_Typology"));
movie.setPrice(obj.getString("price"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Profile();
break;
case 1:
fragment = new About();
break;
case 2:
fragment = new EMICalculator();
break;
case 3:
fragment = new CurrencyConverter();
break;
case 4:
fragment = new PayInstallment();
break;
case 5:
fragment = new SocialFeed();
break;
case 6 :
fragment =new Feedback();
break;
case 7:
fragment =new Settings();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void bottomMenuClick(View v)
{
int pos = Integer.parseInt(v.getTag().toString());
switch (pos)
{
case 1: // enquiry screen
startActivity(new Intent(MainActivity.this,Enquiry.class));
break;
case 2: // contact screen
startActivity(new Intent(MainActivity.this, Contact.class));
break;
case 3: // Instant Call Back screen
startActivity(new Intent(MainActivity.this, CallBack.class));
break;
}
}
#Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
activity_main.xml
<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"
tools:context=".MainActivity"
android:id="#+id/app_name"
>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/container_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector"
android:background="#color/list_background"/>
<!-- <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/content_frame"
>-->
<!-- <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="#+id/MyButton"
android:layout_width="370dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#01458e"
android:textColor="#FFFFFF"
android:text="#string/button_text"></Button>
</LinearLayout>
</LinearLayout>-->
<!-- </LinearLayout>-->
<!--
<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">
<!– Framelayout to display Fragments –>
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!– Listview to display slider menu –>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>-->
<!--<RelativeLayout
android:id="#+id/topLay"
android:layout_width="match_parent"
android:layout_height="#dimen/topLayHt"
android:layout_alignParentTop="true"
android:background="#drawable/header_bg">
<!–<ImageView
android:id="#+id/settingBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/setting_btn_highlight"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/setting_btn_mg_rt"
android:clickable="true"
android:onClick="settingClick"
android:visibility="visible"/>
<ImageView
android:id="#+id/menu_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/menu_btn_highlight"
android:layout_marginLeft="#dimen/menu_btn_mg_lft"
android:clickable="true"
android:onClick="menuClick"/>
<ImageView
android:id="#+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home_btn_highlight"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/home_btn_mg_rt"
android:clickable="true"
android:onClick="homeClick"
android:visibility="gone"/>
<ImageView
android:id="#+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back_btn_highlight"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/back_btn_mg_lft"
android:clickable="true"
android:onClick="backClick"
android:visibility="gone"/>–>
</RelativeLayout>-->
<include
android:id="#+id/menu_lay"
layout="#layout/bottom_layout" />
</RelativeLayout>
NavdrawerlistAdapter.java
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
}
#Override
public int getCount() {
return navDrawerItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
TextView txtCount = (TextView) convertView.findViewById(R.id.counter);
imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
txtTitle.setText(navDrawerItems.get(position).getTitle());
// displaying count
// check whether it set visible or not
if(navDrawerItems.get(position).getCounterVisibility()){
txtCount.setText(navDrawerItems.get(position).getCount());
}else{
// hide the counter view
txtCount.setVisibility(View.GONE);
}
return convertView;
}
}
drawerlist_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#drawable/list_selector">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<TextView android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/counter_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="#color/counter_text_color"/>
</RelativeLayout>
Navdraweritem.java
public class NavDrawerItem {
private String title;
private int icon;
private String count = "0";
// boolean to set visiblity of the counter
private boolean isCounterVisible = false;
public NavDrawerItem(){}
public NavDrawerItem(String title, int icon){
this.title = title;
this.icon = icon;
}
public NavDrawerItem(String title, int icon, boolean isCounterVisible, String count){
this.title = title;
this.icon = icon;
this.isCounterVisible = isCounterVisible;
this.count = count;
}
public String getTitle(){
return this.title;
}
public int getIcon(){
return this.icon;
}
public String getCount(){
return this.count;
}
public boolean getCounterVisibility(){
return this.isCounterVisible;
}
public void setTitle(String title){
this.title = title;
}
public void setIcon(int icon){
this.icon = icon;
}
public void setCount(String count){
this.count = count;
}
public void setCounterVisibility(boolean isCounterVisible){
this.isCounterVisible = isCounterVisible;
}
}
first you have to put your whole view inside a drawerlayout like this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical">
<include
layout="#layout/activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
here activity layout is the main layout with all the views
activity_main.xml should be like this:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="#+id/container_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_row_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>

how to add a buttons at the bottom of slide drawer menu in android?

I need to add buttons at the bottom of slide drawer menu but if i am adding not getting error on xml but runtime it show errors
please help me to find out where i am wrong
i want this :-
this my 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
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/cscart"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/cscart" />
<TextView
android:id="#+id/border"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="#+id/cscart"
android:background="#ffffff"/>
<Button
android:id="#+id/magento"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_below="#+id/border"
android:layout_centerHorizontal="true"
android:background="#drawable/magento" />
<TextView
android:id="#+id/border1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="#+id/magento"
android:background="#ffffff"/>
<Button
android:id="#+id/opencart"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_below="#+id/border1"
android:layout_centerHorizontal="true"
android:background="#drawable/opencart" />
<TextView
android:id="#+id/border2"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_below="#+id/opencart"
android:background="#ffffff"/>
<TableLayout
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:stretchColumns="1"
android:background="#ffffff"
android:layout_below="#+id/border2"
>
<TableRow>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:paddingRight="2dp">
<Button
android:id="#+id/abantecart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/abantecart" />
</FrameLayout>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:paddingLeft="2dp">
<Button
android:id="#+id/prestashop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/prestashop" />
</FrameLayout>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_below="#+id/ll"
>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OUR NEWSLETTER"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="14sp"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_below="#+id/text1"
android:text="Subscribe To Our Newsletter And Be The First To Get Our Latest"
android:textColor="#000000"
android:textSize="10sp"/>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/text2"
android:layout_marginTop="10dp"
android:background="#drawable/email_entry">
<EditText
android:id="#+id/mail_id"
android:layout_width="220dp"
android:layout_height="match_parent"
android:hint="Enter Your Email Address"
android:textSize="10sp"
android:inputType="textEmailAddress"/>
<Button
android:id="#+id/subscribe_mailid"
android:layout_width="32dp"
android:layout_height="match_parent"
android:background="#00000000"
/>
</LinearLayout>
<Button
android:id="#+id/guarenteed"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/ll1"
android:text="100% GUARENTEED"
android:textColor="#ababab"
android:background="#drawable/gurenteed_color"
android:textSize="12sp"
android:layout_marginTop="20dp"/>
<TextView
android:id="#+id/border3"
android:layout_width="match_parent"
android:layout_height="0.1sp"
android:layout_below="#+id/guarenteed"
android:background="#ababab"/>
<Button
android:id="#+id/membership"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/border3"
android:text="OUR MEMBERSHIP"
android:textColor="#ababab"
android:background="#drawable/gurenteed_color"
android:textSize="12sp"
/>
<TextView
android:id="#+id/border4"
android:layout_width="match_parent"
android:layout_height="0.1sp"
android:layout_below="#+id/membership"
android:background="#ababab"/>
<Button
android:id="#+id/secure_payment"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/border4"
android:text="100% SECURE PAYMENT"
android:textColor="#ababab"
android:background="#drawable/gurenteed_color"
android:textSize="12sp"
/>
<TextView
android:id="#+id/border5"
android:layout_width="match_parent"
android:layout_height="0.1sp"
android:layout_below="#+id/secure_payment"
android:background="#ababab"/>
<Button
android:id="#+id/forum_access"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#+id/border5"
android:text="24X7 FORUM ACCESS"
android:textColor="#ababab"
android:background="#drawable/gurenteed_color"
android:textSize="12sp"
/>
<TextView
android:id="#+id/border6"
android:layout_width="match_parent"
android:layout_height="0.1sp"
android:layout_below="#+id/forum_access"
android:background="#ababab"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3a3439"
android:layout_below="#+id/border6"
android:layout_centerHorizontal="true">
<LinearLayout
android:id="#+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
>
<Button
android:id="#+id/facebook"
android:layout_width="50dp"
android:layout_height="20dp"
android:background="#drawable/facebook"
android:layout_marginRight="5dp"/>
<Button
android:id="#+id/twitter"
android:layout_width="50dp"
android:layout_height="20dp"
android:background="#drawable/twitter"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<TextView
android:id="#+id/link"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="#drawable/link_add"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_below="#+id/ll2"/>
<TextView
android:layout_width="310dp"
android:layout_height="wrap_content"
android:background="#drawable/copyright"
android:layout_centerHorizontal="true"
android:layout_below="#+id/link"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Listview to display slider menu -->
<ExpandableListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:listSelector="#drawable/list_selector"
android:background="#drawable/slidemenu_back"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="b1"/>
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="b1"/>
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:text="b1"/>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
main activity :-
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
//private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
public static int cart;
static boolean read=true;
InputStream is=null;
String result=null;
String line=null;
String mail_id;
int code;
static boolean changebackground=true;
//expandable listview
ExpandableListAdapter listAdapter;
ExpandableListView mDrawerList;;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
//expandable listview
TextView totalitems;
DatabaseHandler data;
ImageView gotocart;
List<String> OurTemplates;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
changebackground=false;
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.actionbar,
null);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
actionBarLayout.setLayoutParams(lp);
Button menu=(Button) actionBarLayout.findViewById(R.id.menu);
totalitems=(TextView) actionBarLayout.findViewById(R.id.detail_totalcartitem);
data=new DatabaseHandler(getApplicationContext());
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
mDrawerList = (ExpandableListView) findViewById(R.id.list_slidermenu);
mDrawerList.setGroupIndicator(null);
prepareListData();
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1), true, ""));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
// Recycle the typed array
navMenuIcons.recycle();
//expandable listview
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
mDrawerList.setAdapter(listAdapter);
// Listview Group click listener
mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
/*Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview Group collasped listener
mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
/* Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();*/
}
});
// Listview on child click listener
mDrawerList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
/
int pos=childPosition;
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
changebackground=true;
if(pos==0)
{
// Adding child data
listDataHeader.add("E-commerce Templates");
// Adding child data
List<String> E_commerce = new ArrayList<String>();
E_commerce.add("Cs-Cart");
E_commerce.add("Magento");
E_commerce.add("Opencart");
E_commerce.add("Prestashop");
E_commerce.add("Abantecart");
listDataChild.put(listDataHeader.get(0), E_commerce);
listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
// setting list adapter
mDrawerList.setAdapter(listAdapter);
mDrawerList.expandGroup(0, true);
}
else if(pos==1)
{
listDataHeader.add("CMS Templates");
List<String> CMS = new ArrayList<String>();
CMS.add("Wordpress");
CMS.add("Drupal");
listDataChild.put(listDataHeader.get(0), CMS);
listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
mDrawerList.setAdapter(listAdapter);
mDrawerList.expandGroup(0, true);
}
else if(pos==2)
{
listDataHeader.add("Video Sharing Templates");
List<String> video_sharing = new ArrayList<String>();
video_sharing.add("Clipshare");
video_sharing.add("Phpmotion");
listDataChild.put(listDataHeader.get(0), video_sharing);
listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
mDrawerList.setAdapter(listAdapter);
mDrawerList.expandGroup(0, true);
}
else if(pos==3)
{
listDataHeader.add("Social Networking Templates");
List<String> social_networking = new ArrayList<String>();
social_networking.add("Dolphin");
social_networking.add("Socialengine");
listDataChild.put(listDataHeader.get(0), social_networking);
listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
mDrawerList.setAdapter(listAdapter);
mDrawerList.expandGroup(0, true);
}
else if(pos==4)
{
listDataHeader.add("Free Templates");
List<String> free_templates = new ArrayList<String>();
free_templates.add("Cs-Cart");
free_templates.add("Magento");
free_templates.add("Opencart");
free_templates.add("Prestashop");
free_templates.add("Dolphine");
free_templates.add("Drupal");
listDataChild.put(listDataHeader.get(0), free_templates);
listAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listDataChild);
mDrawerList.setAdapter(listAdapter);
mDrawerList.expandGroup(0, true);
}
return false;
}
});
;
menu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.openDrawer(mDrawerList);
}
else if (mDrawerLayout.isDrawerOpen(mDrawerList)){
mDrawerLayout.closeDrawer(mDrawerList);
}
}
});
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("E-commerce Templates");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
switch (position) {
case 0:
//fragment = new Home();
// click();
///mDrawerLayout.closeDrawer(mDrawerList);
break;
case 1:
Intent i=new Intent(MainActivity.this, CsCart.class);
startActivity(i);
break;
case 2:
click();
break;
case 3:
click();
break;
case 4:
click();
break;
case 5:
click();
break;
default:
break;
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
void click()
{
navDrawerItems.clear();
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Home");
listDataHeader.add("Our Templates");
listDataHeader.add("Our Services");
listDataHeader.add("Portfolio");
listDataHeader.add("Add-ons");
listDataHeader.add("Blog");
listDataHeader.add("Forum");
listDataHeader.add("Contact us");
// Adding child data
OurTemplates = new ArrayList<String>();
OurTemplates.add("E-commerce Templates");
OurTemplates.add("CMS Templates");
OurTemplates.add("Video Sharing Templates");
OurTemplates.add("Social Networking Templates");
OurTemplates.add("Free Templates");
listDataChild.put(listDataHeader.get(0),new ArrayList<String>() );
listDataChild.put(listDataHeader.get(1), OurTemplates);
listDataChild.put(listDataHeader.get(2), new ArrayList<String>());
listDataChild.put(listDataHeader.get(3), new ArrayList<String>());
listDataChild.put(listDataHeader.get(4), new ArrayList<String>());
listDataChild.put(listDataHeader.get(5), new ArrayList<String>());
listDataChild.put(listDataHeader.get(6), new ArrayList<String>());
listDataChild.put(listDataHeader.get(7), new ArrayList<String>());// Header, Child data
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
}
at the run time i am getting error like this
04-16 02:59:47.594: E/AndroidRuntime(1086): FATAL EXCEPTION: main
04-16 02:59:47.594: E/AndroidRuntime(1086): Process: com.bugtreat.shopping, PID: 1086
04-16 02:59:47.594: E/AndroidRuntime(1086): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:844)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1096)
04-16 02:59:47.594: E/AndroidRuntime(1086): at com.bugtreat.shopping.MainActivity.onPrepareOptionsMenu(MainActivity.java:586)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.app.Activity.onPreparePanel(Activity.java:2556)
04-16 02:59:47.594: E/AndroidRuntime(1086): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:464)
04-16 02:59:47.594: E/AndroidRuntime(1086): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
04-16 02:59:47.594: E/AndroidRuntime(1086): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.view.Choreographer.doFrame(Choreographer.java:543)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.os.Handler.handleCallback(Handler.java:733)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.os.Handler.dispatchMessage(Handler.java:95)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.os.Looper.loop(Looper.java:136)
04-16 02:59:47.594: E/AndroidRuntime(1086): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-16 02:59:47.594: E/AndroidRuntime(1086): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 02:59:47.594: E/AndroidRuntime(1086): at java.lang.reflect.Method.invoke(Method.java:515)
04-16 02:59:47.594: E/AndroidRuntime(1086): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-16 02:59:47.594: E/AndroidRuntime(1086): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-16 02:59:47.594: E/AndroidRuntime(1086): at dalvik.system.NativeStart.main(Native Method)

Center aligning of title in action bar of activity and fragments

My HomeActivity consists of Sliding Menu where each option in Sliding Menu opens a new Fragment with different title. I want that title in action bar of fragments and activity appear in center.I tried using custom layout for fragment-
final ActionBar ab = getActivity().getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
// actionBar.setDisplayShowHomeEnabled(false);
ab.setCustomView(R.layout.action_bar);
ab.setDisplayShowCustomEnabled(true);
actionbar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fontFamily="sans-serif"
android:maxLines="1"
android:text="title"
android:textSize="20sp"
android:gravity="center"/>
</RelativeLayout>
This aligns the title in center but the sliding menu disappears.I want that all my fragments display title in center of action bar and sliding menu should not vanish.Here is my code for manifest,styles.xml,activity and one of the fragment and also xml file for Home Activity..
Manifest
<activity
android:name=".ui.slidingmenu.Home"
android:label="#string/app_name"
android:theme="#style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Styles.xml
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarTabStyle">#style/MyTheme.ActionBar.TabView</item>
<item name="android:actionBarTabTextStyle">#style/MyTheme.ActionBar.TabText</item>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:background">#12ABEE</item>
<item name="android:gravity">center</item>
<item name="android:actionMenuTextColor">#ffffff</item>
<item name="android:titleTextStyle">#style/myTheme.ActionBar.Text</item>
</style>
<style name="myTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textColor">#ffffff</item>
<item name="android:textStyle">bold</item>
<item name="android:gravity">center</item>
</style>
HomeActivity
public class Home extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle, mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
private String mName, mProfileimage;
private LinearLayout linearLayout;
private TextView nameTextView;
private ImageView profileImageView;
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 40;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slidermenu);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
linearLayout = (LinearLayout) findViewById(R.id.left_drawer_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
nameTextView = (TextView) findViewById(R.id.username);
profileImageView = (ImageView) findViewById(R.id.drawer_profile_image);
navDrawerItems = new ArrayList<NavDrawerItem>();
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(Home.this);
mName = app_preferences.getString("name", "null");
mProfileimage = app_preferences.getString("profileimage", "null");
nameTextView.setText(mName);
try {
URL url = new URL(mProfileimage);
Bitmap lProfileBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
lProfileBitmap = getRoundedCornerBitmap(lProfileBitmap);
BitmapDrawable lProfileDrawable = new BitmapDrawable(lProfileBitmap);
profileImageView.setBackgroundDrawable(lProfileDrawable);
} catch (Exception e) {
Log.d("error in image", e.toString());
}
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[9], navMenuIcons.getResourceId(9, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[10], navMenuIcons.getResourceId(10, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[11], navMenuIcons.getResourceId(11, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(this.linearLayout);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HF();
break;
case 1:
fragment = new NF();
break;
case 2:
fragment = new FD();
break;
case 3:
fragment = new FDFD();
break;
case 4:
fragment = new VideosFragment();
break;
case 5:
fragment = new DG();
break;
case 6:
fragment = new DSF();
break;
case 7:
fragment = new FDSF();
break;
case 8:
fragment = new DFSD();
break;
case 9:
fragment = new DFSF();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(this.linearLayout);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
}
VideosFragment
public class VideosFragment extends Fragment {
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
ActionBar actionBar;
private int countClick;
private FragmentActivity myContext;
private ViewPager viewPager;
private String[] tabs = {"All Teams Videos", "Player Videos", "My Team Videos"};
public VideosFragment() {
}
#Override
public void onAttach(Activity activity) {
myContext = (FragmentActivity) activity;
super.onAttach(activity);
}
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.videos, container, false);
actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
return rootView;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_search2:
Intent i = new Intent();
i.setClass(myContext, LS.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
slidermenu.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">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/left_drawer_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#drawable/bgmenu"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0">
<ImageView
android:id="#+id/drawer_profile_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#drawable/profileblock">
</ImageView>
<ImageView
android:id="#+id/drawer_profile_image"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignLeft="#+id/drawer_profile_background"
android:layout_alignRight="#+id/drawer_profile_background"
android:layout_centerHorizontal="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:scaleType="centerCrop"></ImageView>
<ImageView
android:id="#+id/settingicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/drawer_profile_background"
android:layout_marginLeft="-15dp"
android:layout_toRightOf="#+id/drawer_profile_background"
android:background="#drawable/settings" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/drawer_profile_background"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/white"></TextView>
</RelativeLayout>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="start"
android:layout_marginTop="2dp"
android:layout_weight="4"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="2dp"
android:listSelector="#drawable/menuselector" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Try adding the following to your TextView of your custom ActionBar layout to center the text.
android:layout_gravity="center"
Also, to explain, when you set gravity="center" that just centers the text in the TextView layout.
Next, your icon hides because your custom layout overlaps the icon for the navigation drawer (siding menu). To fix this, the easiest way is to set a margin for your entire layout (in your case, the relative layout of your custom actionbar layout). I think 30dp should be enough to cover all screen sizes.
android:layout_marginRight="30dp"

Android: 2 Listviews in NavigationDrawer

I've done a Navigation Drawer in my app and the works ok. But I'm trying to put another Listview below the above but I can not get the new display on the screen. I have put the Navigation Drawer in BaseActivity class. What can be the error? This is my code:
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" >
<LinearLayout
...
</LinearLayout>
<LinearLayout android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<ListView
android:id="#+id/listview_drawer"
style="#style/scrollbar"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/grisTransparente"
android:choiceMode="singleChoice"
android:divider="#color/negro"
android:dividerHeight="1dp" />
<ListView
android:id="#+id/listview_drawer2"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/negro"
android:dividerHeight="1dp"
android:background="#color/grisTransparente"
style="#style/scrollbar"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
BASEACTIVITY
private void cargarDrawerLayout(Bundle b) {
mTitle = mDrawerTitle = getTitle();
textosMenuLateral = getResources().getStringArray(R.array.titulos_drawer);
textosMenuLateral2 = getResources().getStringArray(R.array.titulos_drawer2);
iconosMenuLateral = getResources()
.obtainTypedArray(R.array.iconos_drawer);
iconosMenuLateral2 = getResources()
.obtainTypedArray(R.array.iconos_drawer2);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.listview_drawer);
mDrawerList2 = (ListView) findViewById(R.id.listview_drawer2);
View header = getLayoutInflater().inflate(R.layout.drawer_header, null);
mDrawerList.addHeaderView(header);
mDrawerList2.addHeaderView(header);
int[] colores = {0, 0xFFFFFFFF, 0};
mDrawerList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colores));
mDrawerList.setDividerHeight(4);
mDrawerList2.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colores));
mDrawerList2.setDividerHeight(4);
navDrawerItems = new ArrayList<DrawerItem>();
navDrawerItems2 = new ArrayList<DrawerItem>();
navDrawerItems.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral.getResourceId(0, -1)));
navDrawerItems.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral.getResourceId(1, -1)));
navDrawerItems.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral.getResourceId(2, -1)));
navDrawerItems2.add(new DrawerItem(textosMenuLateral2[0], iconosMenuLateral2.getResourceId(0, -1)));
iconosMenuLateral.recycle();
iconosMenuLateral2.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
mDrawerList2.setOnItemClickListener(new SlideMenuClickListener2());
adapter = new DrawerListAdapter(getApplicationContext(),
navDrawerItems,
perfilObj.getColor(),
pos);
mDrawerList.setAdapter(adapter);
adapter2 = new DrawerListAdapter(getApplicationContext(),
navDrawerItems2,
perfilObj.getColor(),
pos2);
mDrawerList2.setAdapter(adapter2);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.icono_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private class SlideMenuClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setTypeface(null, Typeface.BOLD);
opcionesPanelLateral(position);
}
}
private class SlideMenuClickListener2 implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setTypeface(null, Typeface.BOLD);
opcionesPanelLateral2(position);
}
}
private void opcionesPanelLateral(int position) {
Intent i;
switch (position - 1) {
case 0:
case 1:
case 2:
case 3:
break;
default:
break;
}
mDrawerList.setItemChecked(pos, true);
mDrawerList.setSelection(pos);
setTitle(textosMenuLateral[pos]);
mDrawerLayout.closeDrawer(mDrawerList);
}
private void opcionesPanelLateral2(int position) {
Intent i;
switch (position) {
case 0:
break;
default:
break;
}
mDrawerList2.setItemChecked(pos, true);
mDrawerList2.setSelection(pos);
mDrawerLayout.closeDrawer(mDrawerList2);
}
Have you tried adding layout weight to your ListViews? A ListView will naturally match it's parent's size.
Layout Weight I hope it helps.
<ListView
android:id="#+id/listview_drawer"
style="#style/scrollbar"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/grisTransparente"
android:choiceMode="singleChoice"
android:divider="#color/negro"
android:dividerHeight="1dp"
android:layout_weight="1"/>
<ListView
android:id="#+id/listview_drawer2"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/negro"
android:dividerHeight="1dp"
android:background="#color/grisTransparente"
style="#style/scrollbar"
android:layout_weight="1"/>

Android Adapter getView

Sorry, this issue has been treated several times but I still can not solve it.
I have a Navigation Drawer with Fragment that works fine, but if I use it only with Activity I get error
You must supply a resource ID for a TextView.
This is my adapter, I do not think anything wrong. Thanks for the help
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
}
#Override
public int getCount() {
return navDrawerItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.text1);
imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
txtTitle.setText(navDrawerItems.get(position).getTitle());
return convertView;
}
}
and XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp" >
<ImageView
android:id="#+id/icon"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:contentDescription="#string/desc_list_item_icon"
android:src="#drawable/img_1" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/icon"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:text="tt"
android:textSize="18sp" />
</RelativeLayout>
in my Activity class
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
//items to array
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons.getResourceId(7, 0)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[8], navMenuIcons.getResourceId(8, 0)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
just create a holder class and use textview inside the getview() like this.
holder.textview = (TextView) convertView.findViewById(R.id.text1);
and below class for Holder class.
public class Holder {
TextView textview;
//CheckBox check;
}

Categories

Resources