Navigation Drawer not showing up - java

So I have an old application.
I wanted to add it a navigation drawer.
So I want the navigation drawer to work with my current activity and to show ONLY on the currecnt activity
public class WebViewActivity extends Activity {
So the main activity is a webView.
<?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">
<!--
main content
-->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</RelativeLayout>
<!--
navigation list item
-->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#2A323D"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
So I expect my drawer to appear when I click the menu button or my webView detect the gesture left to right
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
drawerView = (View)findViewById(R.id.left_drawer);
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);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
AND
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
float deltaX = x2 - x1;
if (Math.abs(deltaX) > MIN_DISTANCE) {
mDrawerLayout.openDrawer(GravityCompat.START);
return false;
} else {
// consider as something else - a screen tap for example
}
break;
}
return true;
}
});
I really don't know what am I missing, but mDrawerLayout.openDrawer(GravityCompat.START); is not doing anything.
There are no errors in the log.
The suppossed drawer does not show up, nothing happens.
I followed the official guide and more online. No luck.
I don't want to use Fragments!
UPDATE
The issue is that in my 'onCreate' I have something like:setContentView(webView);
So if I remove it, my navigation drawer works!
BUT my webView does not.
What am I missing?

Remove FrameLayout from your main_activity then try

<?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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" />
</RelativeLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#2A323D"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>
Just copy and paste it

Your onCreate() must have setContentView(R.layout.drawer_layout);
where drawer_layout is the xml which has a DrawerLayout as its root view
Follow the documentation
Download sample code and see which xml is passed to setContentView()
If you follow it carefully you will notice activity_main.xml contains Drawerlayout and not the main content's xml.
Also keep in mind that DrawerLayout must contain exactly two child ViewGroups, where the first can be a fragment or FrameLayout that populates a fragment at runtime AND the second must be NavigationView
If you are still confused you can follow this guide which has step-by-step instructions. Its worth a visit.

Related

How to show App bar and Nav drawer in an inflated view

trying to figure out how to show the app bar and nav drawer in an inflated view.
i was using the basic method that came pre installed when i started my app. so it all ran from my main activity using contentmain.xml
but i now have to differnt views i use on my main activity. when they inflate i still want to be able to use the app bar and nav drawer. So ive made my own nav drawer xml called nav
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.harrops.h20droidapp2.MainActivity"
android:id="#+id/main"
style="#style/AppBarOverlay">
<android.support.v4.widget.DrawerLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/appbar"
/>
<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_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
and a seperate app_bar_main.xml
RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/maintoolbar">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/PopupOverlay"
app:title="H20 Droid App "
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"></FrameLayout>
and im trying to get it to show by
sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean layout_alt = sharedpreferences.getBoolean("alt_layout",false);
if (layout_alt==true){
loadVideoAdvert();
LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.main_alt, null);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rlmain);
tb = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(tb);
View nav;
nav = layoutInflater.inflate(R.layout.nav,null);
DrawerLayout drawer = (DrawerLayout) nav.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, tb, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)nav. findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mAdView = (AdView) view.findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("myadviewid");
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
rl.addView(view);
thanks to arron this is my answer.
make an activity called base activity. then inside the xml place the drawer layout and toolbar
<android.support.v4.widget.DrawerLayout android:id="#+id/activity_container"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/PopupOverlay"
/>
<FrameLayout
android:id="#+id/activity_content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
/>
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
then inside the baseactivity java place this
}
#Override
public void setContentView(int layoutResID)
{
/**
* This is going to be our actual root layout.
*/
fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
/**
* {#link FrameLayout} to inflate the child's view. We could also use a {#link android.view.ViewStub}
*/
FrameLayout activityContainer = (FrameLayout) fullLayout.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
/**
* Note that we don't pass the child's layoutId to the parent,
* instead we pass it our inflated layout.
*/
super.setContentView(fullLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
navigationView = (NavigationView) findViewById(R.id.navigationView);
if (useToolbar())
{
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean customBackground = sharedpreferences.getBoolean("customBackground",false);
if(customBackground==true){
toolbar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.altbuttonFocused)));
}
setSupportActionBar(toolbar);
}
else
{
toolbar.setVisibility(View.GONE);
}
setUpNavView();
}
/**
* Helper method that can be used by child classes to
* specify that they don't want a {#link Toolbar}
* #return true
*/
protected boolean useToolbar()
{
return true;
}
protected void setUpNavView()
{
navigationView.setNavigationItemSelectedListener(this);
if( useDrawerToggle()) { // use the hamburger menu
drawerToggle = new ActionBarDrawerToggle(this, fullLayout, toolbar,
R.string.nav_drawer_opened,
R.string.nav_drawer_closed);
fullLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
} else if(useToolbar() && getSupportActionBar() != null) {
// Use home/back button instead
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
/**
* Helper method to allow child classes to opt-out of having the
* hamburger menu.
* #return
*/
protected boolean useDrawerToggle()
{
return true;
}
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
fullLayout.closeDrawer(GravityCompat.START);
selectedNavItemId = menuItem.getItemId();
return onOptionsItemSelected(menuItem);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id ==R.id.nav_settings){
startActivity(new Intent(this,SettingsActivity.class));
finish();
}
}
then in any activity i want to use toolbar and nav view i just extend BaseActivity

onNavigationItemSelected not being called for Bottom Navigation Bar

I'm trying to implement a bottom nav bar that changes fragments when the nav items are clicked. However, when I click on the nav bar items, the fragments don't change. Using log.d, I noticed onNavigationItemSelected is not being called. How do I fix this?
To note, the startFeedFragment, startScheduleFragment, & startSettingsFragmentare implemented the same way and they work for the buttons in the toolbar. I also referenced this tutorial and this question for help.
MainActivity.java
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setUpRecycler();
startFeedFragment();
BottomNavigationView bottomNavBar = (BottomNavigationView) findViewById(R.id.navigation);
bottomNavBar.bringToFront();
bottomNavBar.setOnNavigationItemSelectedListener(this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Log.d("NAV BAR", "onNavigationItemSelected hit");
switch (item.getItemId()) {
case R.id.feedMenu:
startFeedFragment();
Log.d("NAV BAR", "feed");
break;
case R.id.myScheduleMenu:
startScheduleFragment();
Log.d("NAV BAR", "schedule");
break;
case R.id.settingsMenu:
startSettingsFragment();
Log.d("NAV BAR", "settings lol");
break;
default:
Log.d("NAV BAR", "false");
return false;
}
return true;
}
/**
* Switches out the fragment for {#link FeedFragment} and sets an appropriate title. startScheduleFragmens & startSettingsFragment are implemented the same way
*/
private void startFeedFragment()
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, new FeedFragment());
transaction.commit();
getSupportActionBar().setTitle(R.string.title_fragment_feed);
}
}
Snippet from xml file
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#android:color/white"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav"
android:elevation="8dp"/>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
Solved! It was because I had set the buttons in the menu to android:enabled="false". That meant they couldn't be pressed, which is why onNavigationItemSelected wasn't being called. I set all buttons to android:enabled="true" to fix this.
Correct code for bottom_nav.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/bottom_nav_my_schedule"
android:title="#string/menu_my_schedule"
android:icon="#drawable/ic_event_available_white_24dp"
android:enabled="true"/> <!--make sure enabled is set to true!-->
<item android:id="#+id/bottom_nav_feed"
android:title="#string/menu_feed"
android:icon="#drawable/ic_view_list_white_24dp"
android:enabled="true" /> <!--make sure enabled is set to true!-->
<item android:id="#+id/bottom_nav_settings"
android:title="#string/menu_settings"
android:icon="#drawable/ic_settings_white_24dp"
android:enabled="true"/> <!--make sure enabled is set to true!-->
</menu>
As for me the problem was when my activity main layout was LinearLayout, so I've solved it by changing it to RelativeLayout, I have absolutely no thoughts why it's working, because RelativeLayout layouts reply only for elements location, not its view or something else, but it's working.
<RelativeLayout
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:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#drawable/selector"
android:background="#color/white"
app:menu="#menu/menu_bottom"
app:itemIconTint="#drawable/menu_trainings"
app:labelVisibilityMode="unlabeled"
/>
</RelativeLayout>

DrawerLayout always returning null when resuming application?

I have a problem when resuming my application. I can create my activity just fine but for some reason when I put my app in the background, wait a few minutes, then have it come back to the foreround DrawerLayout, is always null. The first time when I load my application it seems to work fine but I can't seem to figure out what is going wrong when I re-create the activity from the background. Here is my code:
MainActivity.Java:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Listen for navigation events
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
...
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
...
}
System.out.println("Toolbar: " + toolbar);
System.out.println("Drawer: " + drawer);
System.out.println("Toggle: " + toggle);
drawer.setDrawerListener(toggle);
toggle.syncState();
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="#layout/app_bar_news_feed" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/app_bar_main" />
<include layout="#layout/include_progress_overlay"/>
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/nav_header_friends_list"
android:id="#+id/navigation_header"/>
<ListView
android:id="#+id/friends_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
The first time the application loads, I can successfully print out an object when I print out "Drawer: ", but when I set it to the background then have it come back to the foreground after a few minutes, it seems to print out null and I have no idea why. Any help would be appreciated. Thanks!
You would have to Re-Inflate your XML layout like this.
Try this so this is 2 lines, replace your setContentView
First get the View Group
ViewGroup container = (ViewGroup)view.getParent();
Now we need to Inflate your XML
setContentView(View.inflate(getApplicationContext(),R.layout.yourlayout,container));

Android Drawer Layout is Displaying under fragment content

I am adding a drawer layout to my Activity which uses fragments. I've implemented this just as described here per the android documentation. And the drawer layout Adapter and View all work in that they show up, but the drawer extends "under" the fragment content. Here is a picture of what is happening:
Does anyone know why this might be? My code for the activity's xml, and the activity add fragment is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_list); // for drawer navigation
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
fragmentTransaction.commit();
setUpDrawer(fragmentManager);
}
private void setUpDrawer(FragmentManager fm) {
String[] drawerItems = getResources().getStringArray(
R.array.drawer_array);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView drawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.item_nav_drawer, drawerItems));
// Set the list's click listener
drawerList.setOnItemClickListener(new
DrawerItemClickListener(drawer,drawerList,drawerItems,fm));
}
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/alarm_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Do I have to set a z-index somewhere? I can provide any further info needed I'd just really like to figure this out!
I don't what produces this issue , but to solve it u have to add the fragment to the content of your FrameLayout which has the id = content_frame.
So edit this line
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
to be
fragmentTransaction.replace(R.id.content_frame,
new AlarmListFragment());
You can move the drawer layout below your Fragment container in your XML. this will render the DrawerLayout in the forward view of the app. just be sure not to cover the fragment container with the parent container of the drawer so that your fragment will be accessible when you need it.
<RelativeLayout
android:id="#+id/my_parent_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="#+id/fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/my_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id=#+id/drawer_pane"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="end" />
<ListView xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:id="#+id/menu_list"
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

how can I add an image to the bottom of a Drawer view?

So i'm wanting to be able to place an logo image at the bottom of my drawer view separate from the list. I've seen people do similar things with other list view and things like that, but whenever I try implementing that i get a lot of java errors. any help with this would be greatly appreciated.
here's my layout 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/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#f4f8f9" />
</android.support.v4.widget.DrawerLayout>
I've seen in a few places to change the frame layout to a relative layout, but every time i do that i get an error.
here's my java code:
public class someActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mDrawerItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerItems = getResources().getStringArray(R.array.drawer_items);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ShelfArrayAdapter(this, mDrawerItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// between the sliding drawer and the action bar app icon
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().setSubtitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setSubtitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
/* Called whenever we call invalidateOptionsMenu() */
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mDrawerItems[position]);
mDrawerLayout.closeDrawer(mDrawerList);
// this changes fragments.
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#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);
}
}
Include a RelativeLayout inside your FrameLayout (child). Then you can put your logo at the bottom ! Hope that works!
Edit
Sorry, I thought it would be that simple, but I got it working !
Try this:
<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
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<RelativeLayout
android:id="#+id/ratafeia"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="start" >
<ListView
android:id="#+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="1dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="240dip"
android:layout_alignParentBottom="true"
android:src="#drawable/rata" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
And the Java code:
Add at the global variables:
RelativeLayout ratafeia;
Before mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);:
ratafeia = (RelativeLayout) findViewById(R.id.ratafeia);
Replace: mDrawerLayout.isDrawerOpen(mDrawerList); with mDrawerLayout.isDrawerOpen(ratafeia);
mDrawerLayout.closeDrawer(mDrawerList)
with mDrawerLayout.closeDrawer(ratafeia);
Bottom line, replace every mDrawerList with your relativeLayout !
I've tested and it is working here !
Just giving you an idea, Try the below logic (Use a wrapper for ListView) , it should work
<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" />
<RelativeLayout
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#f4f8f9" />
<ImageView
// Load your image here
with align parent bottom
/>
</RelativeLayout>
See the layout I created myself to achieve a similar thing, here..
Also, at the time of closing the drawer call drawer.close(Gravity.START), don't pass listView object here, pass the relative layout instead.

Categories