Thursday, March 19, 2015

Set Custom Drawer Title Bar and Drawer Layout

XML:

main_activity.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000" >

  // put your other elements

    <ListView
        android:id="@+id/drawerList"
        android:layout_width="@dimen/sidebarWidth"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="@drawable/back_slider"
        android:divider="#C1C1C1"
        android:dividerHeight="1dp" >
    </ListView>

</android.support.v4.widget.DrawerLayout>



sample_titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#186CB7" >

    <TextView
        android:id="@+id/textSlider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:gravity="center"
        android:textColor="#fff"
        android:textSize="27sp" />

</RelativeLayout>

CODE:

public class MainActivity extends Activity implements OnItemClickListener {



DrawerLayout drawerLayout;
ListView listView;
ActionBarDrawerToggle drawerListner;

// Baseadapter Class for slider options
CustomSliderList slideAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_USE_LOGO
| ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP);

drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
listView = (ListView) findViewById(R.id.drawerList);
listView.setOnItemClickListener(this);

LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// inflate the view that we created before
View v = inflater.inflate(R.layout.sample_titlebar, null);

TextView tv = (TextView) v.findViewById(R.id.textSlider);

                // set application name to title text
tv.setText(getResources().getString(R.string.app_name));


                // Here you set menu icon, and R.string (for disabled(you speak, it open close))
drawerListner = new ActionBarDrawerToggle(MainActivity.this,
drawerLayout, R.drawable.menu_icon, R.string.open_the_drawer,
R.string.close_the_drawer) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};

                // adapter according to your need
slideAdapter = new CustomSliderList(MainActivity.this,
CustomSliderList.name);

listView.setAdapter(slideAdapter);

drawerLayout.setDrawerListener(drawerListner);
getActionBar().setDisplayHomeAsUpEnabled(true);

                // set background of menu icon by color or
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.rgb(24, 108, 183)));

                // or set background of menu icon by image
                getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.back_strip));

getActionBar().setCustomView(v);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerListner.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerListner.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerListner.onConfigurationChanged(newConfig);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
               // you can set your methods using position
listView.setItemChecked(position, true);
}

}


No comments:

Post a Comment