Navigation drawer without actionbar, android - android

I am trying to implement navigation drawer without any actionbar.
I have a small layout at the top of main layout and it looks like below
I want that when i will click the button the navigation drawer will appear under the small(colored) layout.
I have tried with some example but the drawer always appear like this
But i want that the navigation drawer will appear under the small layout instead of "from the top" .
I want something like this:
How can i achieve that??
My tried example's xml file:
<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" >
<!-- The first child in the layout is for the main Activity UI -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/actionBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="#0f9d58" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#id/actionBar"
android:background="#0f9d58" >
<Button
android:id="#+id/actionBarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:gravity="center"
android:text="Holy Operating Systems, Batdroid!"
android:textSize="24sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
<!-- Side navigation drawer UI -->
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_below="#id/actionBar"
android:layout_gravity="left|start"
android:background="#ffeeeeee" />
</android.support.v4.widget.DrawerLayout>
And the main activity was :
public class MainActivity extends Activity {
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
Button actionBarButton;
Boolean buttonStateOpen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStateOpen=false;
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.navList);
addDrawerItems();
actionBarButton=(Button) findViewById(R.id.actionBarButton);
actionBarButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(buttonStateOpen==false)
{
drawer.openDrawer(Gravity.LEFT);
buttonStateOpen=true;
}
else if(buttonStateOpen==true)
{
drawer.closeDrawer(Gravity.LEFT);
buttonStateOpen=false;
}
}
});
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
}

Approach 1
if You have Fixed height of your layout the you can set that much of margin from top to ListView
Approach 2
use this method to calculate the height of your RelativeLayout
then
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
viewWidth = view.getWidth();
viewHeight = view.getHeight();
}
});
}
then use this to set Runtime margin to ListView from top
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);

Yes you can create a drawer layout without action bar, it is very possible, Just do these following steps:
public class NavigationDrawerFragment extends Fragment implements View.OnClickListener {
View drawerView;
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
RelativeLayout mRlUBAccLayout, mRlPaymentLayout, mRlAutoPayLayout;
TextView mTvUBAccount, mTvMakePayment, mTvAutoPay;
DialogClass mDialog;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
drawerView = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mRlUBAccLayout = (RelativeLayout) drawerView.findViewById(R.id.rl_accountlayout);
mRlPaymentLayout = (RelativeLayout) drawerView.findViewById(R.id.rl_mkpaymentlayout);
mRlAutoPayLayout = (RelativeLayout) drawerView.findViewById(R.id.rl_autppaylayout);
mTvUBAccount = (TextView) drawerView.findViewById(R.id.tv_ubaccount);
mTvMakePayment = (TextView) drawerView.findViewById(R.id.tv_mkpayment);
mTvAutoPay = (TextView) drawerView.findViewById(R.id.tv_autopay);
mRlPaymentLayout.setOnClickListener(this);
mRlUBAccLayout.setOnClickListener(this);
mRlAutoPayLayout.setOnClickListener(this);
return drawerView;
}
public void showDrawer(DrawerLayout drawerLayout) {
mDrawerLayout = drawerLayout;
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
getActivity().invalidateOptionsMenu();
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
getActivity().invalidateOptionsMenu();
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
if(HomeFragmentActivity.defaultInstantance().viewingFragment == HomeFragmentActivity.Fragments.ACCOUNT_FRAGMENT){
setNavDrawerColor(0);
}else if(HomeFragmentActivity.defaultInstantance().viewingFragment == HomeFragmentActivity.Fragments.PAYMENT_FRAGMENT){
setNavDrawerColor(1);
}else if(HomeFragmentActivity.defaultInstantance().viewingFragment == HomeFragmentActivity.Fragments.AUTOPAY_FRAGMENT){
setNavDrawerColor(2);
}
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
}
From your activity call this method showDrawer(Drawerlayout). And add this code in the xml for the view which shows the drawer layout:
<fragment
android:id="#+id/fr_navdrawer"
android:name="apjenius.vinton.NavigationDrawerFragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
and find the fragment for navigation drawer in the activity using the below code:
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fr_navdrawer);
mNavigationDrawerFragment.showDrawer((DrawerLayout) findViewById(R.id.dw_drawerLayout));

Try to set the height and the width of the action bar =0dp from layout app_bar_..
so it will not effect on the code but will remove the action bar
like this
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />

Related

Android : Fragment with list-view

The first child in the layout is the listview for the main Activity UI in Navigation Drawer Layout . And I'm trying to display list-view with fragment and navigation drawer layout but list-view is not display in main Activity UI..Can someone help me how to show list.Thanks in advanced.
Here is my code
<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 first child in the layout is for the main Activity UI-->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Side navigation drawer UI -->
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private ListView mDrawerList ;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
Fragment fragment = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView) findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
fragment = new YourListFragment();
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
}
}
private void addDrawerItems() {
String[] osArray = {"Android", "iOS", "Windows", "OS X", "Linux"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#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);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Display listview in fragment
public class YourListFragment extends Fragment {
int DR_CAMERA_REQUEST = 99999;
ListView allPostListView;
MyListAdapter adapter;
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.all_post_list_fragment, container, false);
allPostListView = (ListView)rootView.findViewById(R.id.listview_AllPost);
adapter = new MyListAdapter(getActivity().getBaseContext(),R.layout.all_post_row, bitmapArray);
allPostListView.setAdapter(adapter);
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == DR_CAMERA_REQUEST )
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
Log.e("photo ", " = " + photo);
bitmapArray.add(photo);
//imgProfilePic.setImageBitmap(photo);
}
}
}
Adapter class
class MyListAdapter extends ArrayAdapter {
Context context;
int layoutResourceId;
int DR_CAMERA_REQUEST = 99999;
ArrayList<Bitmap> bmp = new ArrayList<Bitmap>();
ProgressBar pBar;int fixedHeight = 220;
public MyListAdapter(Context context, int layoutResourceId , ArrayList<Bitmap> bitmapArray) {
super(context, layoutResourceId);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.bmp = bitmapArray;
}
#Override
public int getCount() {
return bmp.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View row, ViewGroup parent) {
final Holder holder;
if (row == null)
{
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
row = vi.inflate(R.layout.all_post_row, parent, false);
holder = new Holder();
holder.horizontalScrollView = (HorizontalScrollView) row.findViewById(R.id.hlist);
holder.lLinearLayout = (LinearLayout) row.findViewById(R.id.innerlay);
holder.imgBtn_Camera = (ImageView) row.findViewById(R.id.imgButton_Camera);
row.setTag(holder);
}
else
{
holder = (Holder) row.getTag();
}
LayoutInflater mInflater;
mInflater = LayoutInflater.from(getContext());
View cur_deal = mInflater.inflate(R.layout.horizontalitem, holder.lLinearLayout, false);
RelativeLayout rLayout = (RelativeLayout) cur_deal.findViewById(R.id.img_layout);
final ImageView imageView = (ImageView) cur_deal.findViewById(R.id.image_AllPost);
pBar = (ProgressBar) cur_deal.findViewById(R.id.pBar_AllPost);
holder.lLinearLayout.removeAllViews();
if(bmp.size() > 0)
{
int index = bmp.size() -1;
rLayout.getLayoutParams().height = fixedHeight;
Bitmap lastbitmap = bmp.get(index);
imageView.setImageBitmap(lastbitmap);
pBar.setVisibility(View.VISIBLE);
holder.lLinearLayout.addView(cur_deal);
}
//OnClickListener for camera button in the List
holder.imgBtn_Camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)context).startActivityForResult(cameraIntent, DR_CAMERA_REQUEST);
Log.e("Camera", " Open");
}
});
return row;
}
final class Holder {
ImageView imgBtn_Camera;
LinearLayout lLinearLayout;
HorizontalScrollView horizontalScrollView;
}
}
Here is my all_post_row.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:background="#android:color/white"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="#+id/innerlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#android:color/white"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/buttonslayout"
android:layout_width="match_parent"
android:layout_height="26dp"
android:layout_marginTop="10dp"
android:background="#D8D8D8"
android:orientation="horizontal">
<ImageView
android:id="#+id/imgButton_FoloowUp"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_weight="1"
android:contentDescription="desc"
android:src="#drawable/follow_up_grey" />
<ImageView
android:id="#+id/imgButton_Camera"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="desc"
android:src="#drawable/camera_grey" />
<ImageView
android:id="#+id/imgButton_RecordAudio"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:contentDescription="desc"
android:src="#drawable/recorder_gray" />
</LinearLayout>
</LinearLayout>
Here is horizontal.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:paddingLeft="1dp">
<ImageView
android:id="#+id/image_AllPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ProgressBar
android:id="#+id/pBar_AllPost"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Change
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
to:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Right Drawer - how to add Toolbar button?

I have extended the well-known Google Navigation Drawer Example by adding Toolbar and right-side Drawer in a simple test project at GitHub:
How to add a "burger" button to the Toolbar - which would open the right Drawer (currently you can only swipe it with a finger)?
The "burger" button for the left Drawer is already there and works well (probably added by ActionBarDrawerToggle?)
Here is my current activity_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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice" />
<ListView
android:id="#+id/right_drawer"
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
And here is the MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mActionList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mPlanetTitles;
private String[] mActions;
private int[] mIcons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mActions = getResources().getStringArray(R.array.music_actions);
TypedArray ta = getResources().obtainTypedArray(R.array.music_icons);
mIcons = new int[ta.length()];
for (int i = 0; i < mIcons.length; i++)
mIcons[i] = ta.getResourceId(i, R.drawable.ic_menu_black_24dp);
ta.recycle();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mActionList = (ListView) findViewById(R.id.right_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setCompoundDrawablePadding(24);
view.setCompoundDrawablesWithIntrinsicBounds(
R.drawable.ic_stars_white_24dp, 0, 0, 0);
return view;
}
});
mActionList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mActions) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setCompoundDrawablePadding(24);
view.setCompoundDrawablesWithIntrinsicBounds(mIcons[position], 0, 0, 0);
return view;
}
});
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
mToolbar,
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
mToolbar.setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
mToolbar.setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
I did try the following 2 lines too - but they haven't changed anything (neither the left, nor the right buttons):
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
you have to create custom toolbar like below :
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:layout_width="wrap_content"
android:src="#drawable/ic_menu"
android:padding="#dimen/eight"
android:id="#+id/ivCustomDrawable"
android:adjustViewBounds="true"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:textSize="20sp"
android:text="Custom Toolbar"
android:textColor="#android:color/white"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
and in Java file add this lines:
private void setupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ivCustomDrawable = (ImageView) toolbar.findViewById(R.id.ivCustomDrawable);
ivCustomDrawable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START); //What ever your drawer gravity
}
});
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
}
}

Why does my ListView not show any content?

I'm trying to create a NavigationDrawer layout in Android, the main panel is working properly, and the drawer is a ListView. I've tried replacing the ListView with a TextView, which works fine. So, here's my code to add String content to the ListView.
Here is the relevant code.
private ArrayList<String> drawerContentStrings = new ArrayList<>();
private DrawerLayout homeLayout;
private ListView navigationDrawer;
private GridLayout homeContent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
//homeContent = (GridLayout) findViewById(R.id.home_content);
//drawerContentStrings = getResources().getStringArray(R.array.navigation_content_strings);
//homeLayout = (DrawerLayout) findViewById(R.id.home_layout);
navigationDrawer = (ListView) findViewById(R.id.navigation_drawer);
System.out.println(drawerContentStrings.get(0));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.nav_panel_item, drawerContentStrings);
navigationDrawer.setAdapter(arrayAdapter);
}
Here is the code for nav_panel_item (in the layout folder)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="100dp"
android:layout_height="100dp"
android:textColor="#000"
/>
And here is the code for the actual DrawerLayout: (idk if this is even relevant)
<GridLayout
android:id="#+id/home_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="2">
<Button
android:id="#+id/button1"
android:layout_gravity="left|top"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_gravity="left|top"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_gravity="left|top"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_gravity="left|top"
android:text="Button" />
</GridLayout>
<ListView android:id="#+id/navigation_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="#aff"/>
So, how do I make this work? Because I have no idea. Thanks!
In strings.xml, create an array list:
<string-array name="drawer_options">
<item>Test</item>
<item>Test</item>
<item>Test</item>
</string-array>
Create a new layout file, called activity_drawer.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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Create a new Activity, called BaseActivity; it will help you to create a NavigationDrawer.
public class BaseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] options;
private TextView titleActionBar;
protected FrameLayout content;
protected void onCreateDrawer(final int layoutResID) {
setContentView(R.layout.activity_drawer);
content = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, content, true);
setupActionBar();
options = getResources()
.getStringArray(R.array.drawer_options);
drawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.nav_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options));
drawerList.setOnItemClickListener(this);
setupDrawer();
}
private void setupActionBar() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
titleActionBar = (TextView) findViewById(R.id.title_action_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
titleActionBar.setText("AppName");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
titleActionBar.setText("Options");
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
}
//get the position of the clicked list item
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 2:
//For example, I redirect users to LoginActivity when they click on position 2 //(in this case, last item)
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
drawerLayout.closeDrawer(drawerList);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
In MainActivity:
public class HomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreateDrawer(R.layout.home_activity);
}
}
home_activity.xml is a "normal" layout file. Example:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout 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:orientation="vertical"
android:id="#+id/home_activity"
android:weightSum="1">
<TextView
android:gravity="center"
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:padding="10dp"
android:textSize="20dp"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
I think it is because your set GridLayout height to match_parent which takes all the screen.
Change your GridLayout height to wrap_content and check.
<GridLayout
android:id="#+id/home_content"
android:layout_width="match_parent"
android:layout_height="match_parent" // <---- change here to wrap_content and see.
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="2">

Android kitkat - AppCompat: Doesn't load fragments in my FrameLayout container.(Using a Navigation drawer)

I have implemented a DrawerLayout with Material Design, using AppCompat and it's working ok. The problem is when I try to load a fragment in the container never appear.. and I don't know why.
Below is my activity class
public class MyActivity extends ActionBarActivity {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView leftDrawerList;
private ArrayAdapter<String> navigationDrawerAdapter;
private String[] leftSliderData = {"Estaciones", "Favorito", "Contacta"};
private int mCurrentSelectedPosition = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
nitView();
if (toolbar != null) {
setSupportActionBar(toolbar);
}
initDrawer();
}
private void nitView() {
leftDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("TEST", position + "");
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new Fragment();
switch(position) {
case 0:
fragment = new Estacion();
break;
case 1:
fragment = new Favorito();
break;
case 2:
fragment = new Contacta();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
});
}
private void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
my xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#android:color/white"
android:text="Activity Content"
android:layout_height="wrap_content" />
</RelativeLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Thanks!
your main content view of drawer must be one ViewGroup so you must put all your views as children of one viewgroup so put
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
as a child of
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#android:color/white"
android:text="Activity Content"
android:layout_height="wrap_content" />
</RelativeLayout>

Navigation drawer in all Activity

Hi all i want to put navigation drawer in all activity.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="275dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/black"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:paddingLeft="5dp" />
</android.support.v4.widget.DrawerLayout>
In other activity i am extending this class. But this this not working only this is the drawer logo is coming in the other activity page.
Please tell what i am doing wrong here.
Thanks in advance.
I find the solution. Thanks for your kind help.
Update:
Please find my answer below.
Create a layout drawer_layout:
<?xml version="1.0" encoding="utf-8"?>
<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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="275dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/black"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:paddingLeft="5dp" />
drwaer_custom_layout_file This is for each single row in drawer.(Customize based on requirement):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp" >
<LinearLayout
android:id="#+id/itemLayoutColor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/drawer_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/sku_search"
android:paddingLeft="15dp" />
<TextView
android:id="#+id/drawer_itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</LinearLayout>
<View
android:id="#+id/dividerView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#191919"
android:paddingLeft="15dp" >
</View>
Create an Adapter class. (Remove the element which is not relevant for you):
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
int selectedPosition;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems, int selectedPosition) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
this.selectedPosition = selectedPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.ItemName = (TextView) view
.findViewById(R.id.drawer_itemName);
drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);
drawerHolder.itemLayoutColor = (LinearLayout) view
.findViewById(R.id.itemLayoutColor);
// drawerHolder.dividerView = (View) view
// .findViewById(R.id.dividerView);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
drawerHolder.ItemName.setTypeface(tfNormal);
drawerHolder.ItemName.setText(dItem.getItemName());
if (dItem.getImgResID() != 0) {
drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
dItem.getImgResID()));
} else {
drawerHolder.ItemName.setTextColor(context.getResources().getColor(
R.color.black));
drawerHolder.itemLayoutColor.setBackgroundColor(context
.getResources().getColor(R.color.pGray));
drawerHolder.icon.setVisibility(View.GONE);
// drawerHolder.dividerView.setBackgroundColor(Color.GREEN);
}
if(selectedPosition == position){
drawerHolder.itemLayoutColor.setBackgroundColor(context
.getResources().getColor(R.color.lightyellow));
}
return view;
}
private static class DrawerItemHolder {
TextView ItemName;
ImageView icon;
LinearLayout itemLayoutColor;
// View dividerView;
}
}
Create a class that will extends Activity write below methods:
#Override
public void setContentView(int layoutResID) {
mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(
R.layout.navigation_drawer_layout, null);
actContent = (FrameLayout) mDrawerLayout
.findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(mDrawerLayout);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
}
return true;
}
protected void navigationDrawer(DrawerLayout mDrawerLayout,
ListView mDrawerList, int selectedPosition) {
this.selectedPosition = selectedPosition;
activity = (Activity) NavigationDrawerBaseActivity.this;
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
this.mDrawerLayout = mDrawerLayout;
this.mDrawerList = mDrawerList;
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
mDataList = new ArrayList<DrawerItem>();
**********Here add the items in the list***********
i.e.
mDataList.add(new DrawerItem(DrawerConstant.LOGOUT,
R.drawable.ic_signout));
adapter = new CustomDrawerAdapter(this,
R.layout.drawer_custom_single_layout, mDataList,
selectedPosition);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position != selectedPosition) {
selectItem(position);
}
}
protected void selectItem(int position) {
// update the main content by replacing fragments
switch (position) {
case 0:
// Call the another activity
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
break;
case 1:
// Call the another activity
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
break;
case 2:
// Call the another activity
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
break;
default:
break;
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
}
Add the below code in Activity wherever you want to put Navigation Drawer:
Extends the NavigationDrawerBaseActivity class.
After setContentView call below method:
// set Naviagtion Drawer
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
super.navigationDrawer(mDrawerLayout, mDrawerList, 1);
Add below method in same Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
}
}
Create a BaseActivity class that implements the drawer, and let all your activities extend this BaseActivity.
#Override
public void setContentView(int layoutResID) {
mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
actContent = (FrameLayout) mDrawerLayout.findViewById(R.id.frame_container);
setContentView(mDrawerLayout);
getLayoutInflater().inflate(layoutResID, actContent, true);
}
Set the drawer layout as main content
I have achieved the asked scenario in the following procedure.
Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/venue_bg"
android:orientation="vertical" >
<!-- The navigation drawer -->
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/frameLayoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ExpandableListView
android:id="#+id/ExpandableList"
android:layout_width="#dimen/drawer_size"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#drawable/left_drawer_item_gradient"
android:choiceMode="singleChoice"
android:clickable="true"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<!-- content layout -->
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
setcontentView inside NavigationDrawerBaseActivity
#Override
public void setContentView(int layoutID)
{
// TODO Auto-generated method stub
fullLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main_menu_activity_container, null);
pocketFrame = (FrameLayout) fullLayout.findViewById(R.id.frameLayoutContent);
getLayoutInflater().inflate(layoutID, pocketFrame, true);
super.setContentView(fullLayout);
}
In your NavigationDrawerBaseActivity make a method which sets setcontentView & take layout as an argument, & in every extended child class override that method & pass layout in argument. In xml #+id/ExpandableList is a list which is in left & #+id/frameLayoutContent acts like a container for incoming argument layouts, this will give you the replica of fraqgments replacing in a container.
Note: This is just a work around, recommended is Fragments.
You can add an abstract method to get the layout for the activity which it's extending your navigation drawer: public abstract int NavigatonDrawerBaseActivity(); and make your class an abstract class (here use setContentView(NavigatonDrawerBaseActivity())). Then in the class which it's extending the Navigation drawer implements the method and pass it your xml layout (not use the setContentView method):
#Override
public int NavigatonDrawerBaseActivity() {
return R.layout.yourLayout;
}
And finally in your layout you need to add the same code of your NavigationDrawerBaseActivity xml:
This works form me using fragments. Hope it helps.
this work for me
public class MyDrawer extends AppCompatActivity {
ActionBarDrawerToggle toggle;
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
#Override
public void setContentView(final int layoutResID) {
fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.mydrawer, null);
frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(fullLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout3);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
drawer.closeDrawers();
int itemId = menuItem.getItemId();
Toast.makeText(getApplicationContext(), menuItem.getTitle().toString(),
Toast.LENGTH_LONG).show();
//navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (toggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/drawer_framelayout">
<FrameLayout
android:id="#+id/drawer_frame2"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<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_layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<FrameLayout
android:id="#+id/drawer_frame"
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:headerLayout="#layout/nav_header_main2"
app:menu="#menu/activity_main_drawer"
android:background="#fefefd" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
for use:
public class yourclass extends MyDrawer {
is .setOnItemClickListener work?yes
<android.support.v4.widget.DrawerLayout>
<FrameLayout>
your main content stuff here
</android.support.v4.widget.DrawerLayout>
<FrameLayout>
your main content stuff here(.setOnItemClickListener)

Categories

Resources