Button beneath navigation drawer not working when pressed - android

Yet, when the button is placed in the hierarchy so that it can be seen on top of the navigation drawer, the button functions properly. However, the button should be hidden behind the navigation drawer when it is slid out, so this is not desirable.
Below is the code from MainActivity.java
public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {
public ProgressDialog progBar;
public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
cart_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
start_request();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onNavigationDrawerItemSelected(int position) {
Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}
#Override
public void onBackPressed() {
if (mNavigationDrawerFragment.isDrawerOpen())
mNavigationDrawerFragment.closeDrawer();
else
super.onBackPressed();
}
public void start_request()
{
String pkg = getPackageName();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
startActivity(intent);
if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}
I am assuming the issue lies within the class above, but for completion's sake, I pasted the two XML files of interest below.
activity_main.xml As you can see, the ImageButton is currently "above" the navigation drawer in hierarchy so as to make it be covered by the navigation drawer. Moving the ImageButton "below" makes the button work properly, but causes it to appear on top of the navigation drawer (and not tinted like the rest of the layout).
<FrameLayout
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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fontify="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#color/myPrimaryColor">
<View
android:id="#+id/block1"
android:layout_height="240dp"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
android:background="#drawable/block_primary"
/>
<TextView
android:id="#+id/title1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/toolbar_actionbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:text="#string/title"
android:textColor="#color/white"
android:textSize="34sp"
android:fontFamily="sans-serif"
/>
<include
android:id="#+id/toolbar_actionbar"
layout="#layout/toolbar_default"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<ImageButton
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="268dp"
android:src="#drawable/ic_chevron_up"
android:background="#drawable/fab_simple"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/toolbar_actionbar"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment
android:id="#+id/fragment_drawer"
android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
fragment_navigation_drawer.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="match_parent"
android:orientation="vertical"
>
<!-- Provides a margin at the top of the navigation drawer. -->
<View
android:id="#+id/navWhiteSpace1"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#color/myNavigationDrawerBackgroundColor"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_below="#+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="#color/myNavigationDrawerBackgroundColor"/>
</RelativeLayout>

I ran into the same issue when attempting to follow various navigation drawer tutorials online.
What worked for me was using Mike Penz's MaterialDrawer library on GitHub. He has a very simple tutorial in the "readme" file found on the bottom of the page.
Hopefully someone else with a better understanding of Java can explain why your code failed.

Related

ActionBarDrawerToggle isn't working till the time i manually swipe right to open the navigation drawer. After this, it responds perfectly

Problem:
When I open my application and click on the ActionBarDrawerToggle, it doesn't open the navigation drawer. However, when i swipe right and then open and close the navigation drawer, after this the ActionBarDrawerToggle repsonds perfectly fine, like it should, by opening the drawer.
My HomeScreen.java
public class HomeScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
instantiateViews();
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
public void instantiateViews(){
toolbar = findViewById(R.id.drawer_menu_toolbar);
drawerLayout = findViewById(R.id.home_screen_main_drawer_layout);
navigationView = findViewById(R.id.home_screen_navigation_view);
}
#Override
public void onBackPressed(){
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_home:
break;
case R.id.nav_account:
break;
case R.id.nav_share:
break;
case R.id.nav_logout:
session.logoutUser();
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
I have tried everything from the following thread: Navigation Drawer ActionBar button not working even though for that person the ActionBarDrawerToggle wasn't working under all circumstances but mine isn't working till the time navigation drawer is opened. After that mine works perfectly
What I have tried doing:
1.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
2.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
3.
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
None of these work for me
My home_screen.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/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="120dp"
android:background="#color/blue">
<android.support.v7.widget.Toolbar
android:id="#+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:id="#+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="#+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="#drawable/seachbar_homescreen"
<EditText
android:id="#+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
android:visibility="gone"
app:headerLayout="#layout/home_screen_menu_drawer_header"
app:menu="#menu/home_screen_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
}
Try this, or Try to take out your Toolbar from Relative Layout.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
I have done some minor modification in your layout i hope it will help you.
<?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/home_screen_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".HomeScreen">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Top Layout-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="120dp"
android:background="#color/blue">
<android.support.v7.widget.Toolbar
android:id="#+id/drawer_menu_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout
android:id="#+id/drawer_menu_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="#+id/search_toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="10dp"
android:background="#drawable/seachbar_homescreen">
<EditText
android:id="#+id/searchHere"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/seachbar_homescreen"
android:hint="Search Here"
android:textSize="15sp"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/home_screen_navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_gravity="start"
app:headerLayout="#layout/home_screen_menu_drawer_header"
app:menu="#menu/home_screen_menu_drawer"/>
</android.support.v4.widget.DrawerLayout>

How to add image in toolbar

I am following this example to create my app now i am trying to add image in my toolbar the but image is not displaying,i am trying to set my app logo,following is my code and toolbar xml can any one tell me what is mistake?
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/MyMaterialTheme.Base"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toolbartitle"
/>
</android.support.v7.widget.Toolbar>
MainActivity.java
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private static String TAG = MainActivity.class.getSimpleName();
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
private String chng;
private Intent i;
public SearchView searchView;
public SearchManager searchManager;
// Alert Dialog Manager
AlertDialogManager alert = new AlertDialogManager();
// Session Manager Class
SessionManager session;
private String id;
private boolean b;
private String rasa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext());
id = getIntent().getStringExtra("id");
System.out.println("MAIN ID : " + id);
/* i=getIntent();
chng=i.getStringExtra("Changes");*/
// Toast.makeText(getApplicationContext(),chng,Toast.LENGTH_LONG).show();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout),mToolbar);
drawerFragment.setDrawerListener(this);
displayView(0);
}
#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;
}
This how my toolbar shows right now
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/color_blue"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#mipmap/header_title" />
</android.support.v7.widget.Toolbar>
You can set Event of button also by this method...
public static void showToolBar(Toolbar toolbar,
final AppCompatActivity activity) {
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
#SuppressWarnings("deprecation")
Drawable drawable = activity.getResources().getDrawable(
R.mipmap.back_icon);
drawable.setColorFilter(
activity.getResources().getColor(R.color.color_white),
android.graphics.PorterDuff.Mode.SRC_ATOP);
activity.getSupportActionBar().setHomeAsUpIndicator(drawable);
toolbar.setBackgroundColor(activity.getResources().getColor(
R.color.color_blue));
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
activity.finish();
}
});
}
Use the logo display on the action bar.
getSupportActionBar().setLogo(R.drawable.logo);
getSupportActionBar().setDisplayUseLogoEnabled(true);
instead you can set your image as textview background. use android:background="#drawable/toolbartitle". and setTitle("") in your code.
your final toolbar will look like
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#drawable/toolbartitle"
local:theme="#style/MyMaterialTheme.Base"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
</android.support.v7.widget.Toolbar>
It was mistake done by me,i was checking with custom toolbar xml,but i already have toolbar in my activity_main.xml..Hope my answer will help others too
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/MyToolBarStyle.Base"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/toolbartitle"
android:minHeight="?attr/actionBarSize" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- activity view -->
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/container_body"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="info.androidhive.materialdesign.activity.FragmentDrawer"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
You should set the image src attribute rather than setting the imageview background drawable.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/toolbartitle"
/>
You missed to show the source
android:src="#drawable/yourImage"
if not working try adding some genuine width and height instead of wrap content
android:layout_width="50dp"
android:layout_height="50dp"

Android NavigationDrawer items don't click

I'm using Android NavigationDrawer without Listview items into slide menu, after creating and changing that slide menu items don't click and don't work to handle to click, for example this below code is my XML layout:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/create_new_waybill"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_margin="5dp"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_add_waybill"
android:layout_gravity="center"/>
<com.sample.app.Widgets.TextViewStyle
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="#dimen/normal_text_size"
android:text="#string/slidemenu_waybill"
app:fonttype="mjbeirut"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/mainViewFragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
In this XML I want to show Toast after clicking on create_new_waybill id.
my Activity
public class ActivityMain extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private LinearLayout mSlideMenuContainer;
private ActionBarDrawerToggle mDrawerToggle;
private String[] items;
private UiFiller uiFiller;
private int currentSelectedSlideMenuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
G.currentActivity = this;
/* Getting reference to the DrawerLayout */
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mSlideMenuContainer = (LinearLayout) findViewById(R.id.slide_menu);
LinearLayout create_new_waybill = (LinearLayout) findViewById(R.id.create_new_waybill);
create_new_waybill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("ffffffffffff","aaaaaaa");
}
});
/* Initials UI widgets */
uiFiller = new UiFiller(getWindow().getDecorView());
/* Setting default fragment */
currentSelectedSlideMenuItem = 0;
updateFragment(currentSelectedSlideMenuItem);
}
}
Try setting these 3 attributes for ImageView and TextViewStyle:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
and replace Log.e("ffffffffffff","aaaaaaa");
with:
Toast.makeText(view.getContext(), "Some text", Toast.LENGTH_SHORT).show();

Android DrawerLayout not responding to button click event

I have a DrawerLayout component in my activity, but I'm not using ActionBar.
This is my main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="650dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:divider="#null"
android:choiceMode="singleChoice"
android:visibility="gone"/>
</android.support.v4.widget.DrawerLayout>
<ImageButton
android:id="#+id/categories_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/button_category"/>
In my MainActivity I try to open/close the DrawerLayout by clicking on my Button.
Here is my Activity:
public class MainActivity extends Activity {
private ImageButton categoriesButton;
private DrawerLayout drawerLayout;
private ListView drawerMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoriesButton = (ImageButton)findViewById(R.id.categories_button);
categoriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(drawerLayout.isDrawerOpen(drawerMenu)) {
drawerLayout.closeDrawer(drawerMenu);
}else {
drawerLayout.openDrawer(drawerMenu);
}
}
});
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerMenu = (ListView) findViewById(R.id.drawer);
MyAdapter myAdapter = new MyAdapter(context, getCategories());
drawerMenu.setAdapter(myAdapter);
}
}
When I click the Button the first time it does not respond, but after I open the DrawerLayout manually it works perfect.
Does anybody know what is wrong here?
Thanks!
I found the solution! In the xml, the Listview visibility needs to be set to visible. It was set to gone (I took this code from the oficial documentation).
<ListView
android:id="#+id/drawer"
android:layout_width="650dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:divider="#null"
android:choiceMode="singleChoice"
android:visibility="visible"/>
Anyway now it works. Thanks for your help!

Issue with appcompat v21 Material Design: Fragment overlay content and toolbar

I followed http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html to use the toolbar, without fragment, it works perfectly, but when I use fragment, the fragment always overlays the content, and I cannot see toolbar when displaying fragment.
screen:
and when I starts another activity from the current one, the icon of the toolbar in the new activity doesn't show up. screen:
I use Android Support Library 21.0.3.
and the code in the MainActivity class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mLeftDrawerList = (ListView) findViewById(R.id.left_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_list_item_1, data);
mLeftDrawerList.setAdapter(mNavigationDrawerAdapter);
if (mToolbar != null) {
mToolbar.setTitle("Home");
setSupportActionBar(mToolbar);
}
initDrawer();
final FragmentManager fragementMgr = getSupportFragmentManager();
FragmentTransaction transaction = fragementMgr.beginTransaction();
transaction.add(R.id.fragment_container, new MyFragment());
transaction.commit();
}
the layout xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<LinearLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<FrameLayout android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- 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>
code in MyFragment:
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(getActivity(), DetailActivity.class);
startActivity(intent);
}
});
// Inflate the layout for this fragment
return view;
}
}
layout xml for Fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_fragment"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="top"
android:textSize="18sp"
android:id="#+id/text"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/text"
android:text="#string/open"
android:id="#+id/button"/>
</RelativeLayout>
The code in DetailActivity class:
public class DetailActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Detail");
setSupportActionBar(toolbar);
}
}
layout for the detail activity:
<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=".DetailActivity">
<include layout="#layout/toolbar" />
<TextView
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Detail Activity Content"
android:layout_height="wrap_content" />
</LinearLayout>
can anyone help me out? thanks a lot!
Update: I updated the code and layout of the main activity according to reply from m iav.
new screen is blank now with the changes:
toolbar is a view in your layout , you can see it like textview ,now it is a textview overlay your fragment ,you use attrbibu to below it
so finally after checking the sample: MaterialEverywhere, I came with the solution with updating the layout:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<FrameLayout android:id="#+id/fragment_container"
android:layout_below="#id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</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>
the trick is the parent layout of the toolbar and the content frame. Instead of LinearLayout, I use RelativeLayout. and it works now as expected.
anyway, I found such change from the SDK is over complex, with so many changes, and each can cause issues. It will be much better for the developers to have clean APIs with less all these tweaks.
This should be your main layout.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<LinearLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="#layout/toolbar" />
<FrameLayout android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- 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>
And this should be your call to the new fragment:
transaction.add(R.id.fragment_container, new MyFragment())
transaction.commit();
Regarding your second question, a navigation drawer can't be shared among different activities. You have to use a new Fragment and keep the same Activity you're working in now.

Categories

Resources