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"
Related
Hi I am developing an app and I want to remove toolbar shadow, I tried adding app:elevation="0dp" to AppBarLayout but after adding this the menu button for opening navigationView does not work,Any idea?
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private SlidingMenuLayout slidingMenuLayout;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
slidingMenuLayout = (SlidingMenuLayout) this.getLayoutInflater().inflate(
R.layout.activity_main, null);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(slidingMenuLayout);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setLogo(R.drawable.notifications);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.open_drawer:
toggleMenu();
default:
return super.onOptionsItemSelected(item);
}
}
private void toggleMenu() {
slidingMenuLayout.toggleMenu();
} }
activity_main.xml:
<net.aparteman.apartemanapp.viewHolder.SlidingMenuLayout 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.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/dark_purple"
android:layoutDirection="rtl"
android:textDirection="rtl"
app:elevation="0dp"
app:headerLayout="#layout/dashboard_nav_header"
app:itemTextColor="#color/white"
app:menu="#menu/dashboard_drawer_view"
app:theme="#style/ApartemanStyle" />
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context=".activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:popupTheme="#style/AppTheme" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/dashboard_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
As elevation also affects the View's z-index, I guess what's happening is that your FrameLayout is above the Toolbar, eating the touch events.
Try to either put the FrameLayout before the AppBarLayout in the XML, or define your FrameLayout with a top margin to leave room to the Toolbar.
I want to change the app I have created so far, in order to implement a ListView. I followed this example and this example. The examples alone work, but not together with the changes I had to make to my so-far existing app.
I have a avticity_main.xml defined as follows:
<android.support.design.widget.CoordinatorLayout
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"
tools:context=".MainActivity" >
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_grid"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="4"
>
<TextView
android:text="MainTitle"
android:textSize="32dip"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal"
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
and I have the following code for my main activity:
public class MainActivity extends AppCompatActivity {
static final String[] MOBILE_OS =
new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ListView list = (ListView) findViewById(R.id.list);
list.setAdapter(new ListAdapter(this, MOBILE_OS));
The code compiles fine, but no list is shown.
ListActivity is a "convenience" class to implement a simple ListView. And that's pretty much all you can do. So, there's no setSupportActionBar(Toolbar) there.
To achieve what you want, use AppCompatActivity or default Activity.
This link provides a nice tutorial on how to use the new ToolBar instead of the old ActionBar.
You want something like this:
This layout example could be used:
P.S.: "#color/primary" could be any color like #FF009688.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
android:background="#color/primary"
android:elevation="4dp">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:id="#+id/lv"></ListView>
</RelativeLayout>
And on the Java side:
YourActivity.java
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
lv = (ListView) findViewById(R.id.lv);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
}
// Populate the ListView here...
}
}
EDIT: This other answer of mine can show you how to create a Custom Adapter to display the data you want on the ListView.
Second edit: Use this layout
<android.support.design.widget.CoordinatorLayout
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"
tools:context=".MainActivity" >
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
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"/>
</android.support.design.widget.AppBarLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_grid"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="4"
android:orientation="vertical">
<TextView
android:text="MainTitle"
android:textSize="32dip"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal"
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
What did I do?
Your LinearLayout was lacking the orientation parameter on the XML, and your TextView had a android:layout_height set to match_parent, so it was filling the entire layout. I've changed it to wrap_content and you're good to go.
setSupportActionBar(toolbar) is not available in ListActivity. You can use AppCompatActivity instead and just get a reference to the listview:
ListView list = findViewById(R.id.list); and use list.setAdapter(adapter) instead of setListAdapter(adapter);
You can't do that in ListActivity.
If you want to access getSupportActionBar(), you need to extent your class by AppCompatActivity.
My Suggestion : Don`t use ListActivty is you want to use ToolBar. Create an Activity and then only have ListView within that Activity. It'll work just fine.
OR
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
}`
You can use the new AppCompatDelegate component provided by the Support Library to easily add a Toolbar to your ListActivity.
Follow this link,
How to add Toolbar to an Activity which doesn't extend AppCompatActivity
I have a ListActivity that I want to use the toolbar in but I get the error can't resolve getSupportToolbar. Everything works fine in my main activity but here I get errors.
found this excerpt on an Android dev blog post. You have to use the toolbar as a standalone object and add your own items.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.your_toolbar_menu);
}
According to me, you haven't set the toolbar in your activity. First you have to set the app theme to#style/Theme.AppCompat.Light.NoActionBar,
Then you have to set the custom toolbar in your MainActivity like
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar_main_activity);
setSupportActionBar(toolbar);
TextView toolbar_title = (TextView) toolbar.findViewById(R.id.title_toolbar);
getSupportActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
I hope this might solve your problem.
This is how I had set up a toolbar in my List Activity. I will post the layout file details
activity1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/toolbar"
layout="#layout/toolbar_activity" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</LinearLayout>
toolbar_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/toolbar_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<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="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:minHeight="?attr/actionBarSize"
android:background="#color/windowBackground"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<Button
android:id="#+id/buttonReturn"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:background="#drawable/arrow_left" />
</RelativeLayout>
I am using the new android design CollapsingToolbarLayout. However, I am unable to get the menu to appear. Also, the homeasup indicator does not appear.
Can someone point me in the right direction? Or is this simply not possible?
EDIT
So, I finally got my laptop back. Here is the code.
The Activity
public class CheeseDetailActivity extends AppCompatActivity {
public static final String EXTRA_NAME = "cheese_name";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent intent = getIntent();
final String cheeseName = intent.getStringExtra(EXTRA_NAME);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(cheeseName);
loadBackdrop();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadBackdrop() {
final ImageView imageView = (ImageView) findViewById(R.id.backdrop);
Glide.with(this).load(Cheeses.getRandomCheeseDrawable()).centerCrop().into(imageView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_player, menu);
return true;
}
}
The Layout
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="24dp"
app:expandedTitleMarginEnd="48dp">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="#dimen/toolbar_top_margin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
app:borderWidth="#dimen/fab_border" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/card_margin">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cheese_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
The menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="ifRoom" />
</menu>
As we can see, there is no overflow, nothing. But when I press the physical menu button, the menu pops up fine.
So, I guess I was really really dumb. I used the wrong margin for the toolbar. android:layout_marginTop="#dimen/toolbar_top_margin"
The margin was supposed to be -48dp, but I was using 48dp.
I had this in my dimens. I forgot to change to toolbar_top_margin_neg after I refactored.
<dimen name="toolbar_top_margin">48dp</dimen>
<dimen name="toolbar_top_margin_neg">-48dp</dimen>
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.