why the toolbar is not showing at the bottom ?
private void initToolbars() {
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(toolbarTop);
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.drawer_layout);
toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()){
case R.id.action_settings:
// TODO
break;
// TODO: Other cases
}
return true;
}
});
toolbarBottom.inflateMenu(R.menu.menu_main);
}
this xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" />
<android.support.v7.widget.Toolbar
android:id="#+id/drawer_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:minHeight="?attr/actionBarSize" />
</RelativeLayout>
I am getting toolbar at top only
Try this way
<RelativeLayout ...>
<!-- top toolbar -->
<Toolbar
android:id="#+id/top_toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
>
...
</Toolbar>
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/top_toolbar"
android:src="#drawable/week1"
/>
<!-- bottom toolbar -->
<Toolbar
android:id="#+id/bottom_toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_alignParentBottom="true">
</Toolbar>
<!-- scrollable pane -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/image"
android:layout_above="#id/bottom_toolbar">
...
</ScrollView>
</RelativeLayout>
When you already defined android:layout_alignParentBottom="true" then no need to add android:layout_gravity="bottom".
For more problem you can visit here .I hope it will help you .
How to show action items at the bottom using Toolbar
Try the below, this is working for me
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<!--Begin Top Bar-->
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</LinearLayout>
<!--Bottom bar-->
<android.support.v7.widget.Toolbar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:alignParentBottom="true"/></LinearLayout>
May be problem is with tw toolbar.
Try to use below code :
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="?attr/colorPrimary" />
Your MainActivity.java
public class MainActivityextends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
#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_home, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Hope it helps you.
Related
In my app, I am using a custom action bar. Then I tried to add a Home Button to open the Navigation Drawer. But when I tried to click the button, nothing happened. The Home Button isn't responding to clicks.
Here's my code:
activity_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"
tools:context="android.example.com.errorhomeasupbutton.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="#string/app_name"
app:titleTextColor="#android:color/white"
android:background="#color/colorPrimary">
</android.support.v7.widget.Toolbar>
</LinearLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.NavigationView
android:id="#+id/main_nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="#layout/main_nav_view_header"
app:menu="#menu/main_nav_view_menu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeAsUpIndicator(R.drawable.ic_menu);
bar.setHomeButtonEnabled(true);
drawerLayout = findViewById(R.id.main_drawer_layout);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
How can I fix this?
I'm fooling around with collapsing toolbar. Everything seems good. But My icons are not centered with the title. I think that's because of fitsSystemWindows(). But at the same time images are not covering the status bar. Any help will be appreciated. Thanks!
Java Code :
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shr___add__vehicle);
Toolbar toolbar = (Toolbar) findViewById(R.id.add_vehicle_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout appbar = (CollapsingToolbarLayout) findViewById(R.id.add_vehicle_collapse);
appbar.setTitle("My Bikes");
appbar.setExpandedTitleColor(Color.parseColor("#F05329"));
XML :
<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="com.techindyeah.soherides.SHR_Activity_Add_Vehicle">
<android.support.design.widget.AppBarLayout
android:id="#+id/add_vehicle_app_bar"
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/add_vehicle_collapse"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#color/colorPrimary"
app:expandedTitleMarginStart="20dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true">
<ImageView
android:id="#+id/rl_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#mipmap/bikeimage"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="#+id/add_vehicle_toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:minHeight="60dp"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_shr__activity__add__vehicle" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_vehicle_insert_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
app:layout_anchor="#+id/add_vehicle_app_bar"
app:layout_anchorGravity="bottom|right"
app:srcCompat="#mipmap/ic_insert_photo_white_24dp" />
This is what I have now :
Expanded
Collapsed
1) As per my knowledge back arrow does not move, you can hide it while scrolling if you want.
2) To add Option to right side of toolbar you can use menu.xml
menu.xml
<item android:id="#+id/action_map"
android:icon="#drawable/ic_map"
android:title="#string/action_map"
android:showAsAction="always"
android:orderInCategory="1" />
in Java file
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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.action_map) {
return true;
}
return super.onOptionsItemSelected(item);
}
make sure you add app:layout_collapseMode="parallax" and app:layout_behavior in your arrow image
and use
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
I want to create a toolbar like the following image as proposed in the material design guidelines:
I can achieve this via using the toolbar with an relative layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Toolbar/>
<LinearLayout android:layout_marginTop="-17dp" />
</RelativeLayout>
I am not sure this is the correct way or not. Any help is appreciated.
Thanks Gabriele for all the help. Here is working code:
Activity :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mai);
Toolbar toolbar = (Toolbar) findViewById(R.id.card_toolbar);
if (toolbar != null) {
// inflate your menu
toolbar.inflateMenu(R.menu.main);
toolbar.setTitle("Card Toolbar");
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
Toolbar maintoolbar = (Toolbar) findViewById(R.id.toolbar_main);
if (maintoolbar != null) {
// inflate your menu
maintoolbar.inflateMenu(R.menu.main);
maintoolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
return true;
}
});
}
}
}
Layout XML File:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size_x2"
android:background="#android:color/holo_orange_dark"
android:minHeight="?attr/actionBarSize" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_main"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="#dimen/action_bar_size"
android:orientation="vertical" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/card_toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/action_bar_size"
android:background="#android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/app_name"
android:textSize="24sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
Make sure your activity theme is extending Theme.AppCompat.Light.NoActionBar.
Here is what it will look like:
Few things to note:
If you are using card elevation then you need to alter the margin top so that your card aligns with the main toolbar.
I am still seeing 1-2 pixel margin between bottom of main toolbar and card toolbar. Not sure about what to do in this case. For now, I am aligning it manually.
You can obtain it using a Toolbar as ActionBar, a CardView and another Toolbar(standalone) inside the Card.
For the Toolbar standalone inside a Card you can use something like this:
<android.support.v7.widget.CardView>
<LinearLayout>
<Toolbar android:id="#+id/card_toolbar" />
//......
</LinearLayout>
</CardView>
Then you can inflate your menu to obtain the icon actions.
Toolbar toolbar = (Toolbar) mActivity.findViewById(R.id.card_toolbar);
if (toolbar != null) {
//inflate your menu
toolbar.inflateMenu(R.menu.card_toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
//.....
}
});
}
For the toolbar used as action bar and the main layout you can use:
option1:
<RelativeLayout>
<toolbar/> //Main toolbar
<View
android:background="#color/primary_color"
android:layout_height="#dimen/subtoolbar_height"/>
<CardView /> //described above
</RelativeLayout>
Option2: An extended toolbar (as actionbar) and a CardView as described above playing with margins.
I started using a collapsing toolbar in my app. After implementing it the way they did on the Android developers blog, it doesn't seem to be working correctly. I see the overflow and up navigation button, however they don't follow the toolbar when I scroll downwards. This video is what should be happening, everything works fine except for the fact that the overflow/up navigation icons don't display on the collapsed toolbar.
Projects Detail Activity
package com.austinerck.projectpanda.activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.austinerck.projectpanda.R;
public class ProjectDetailsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_details);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.toolbar_collapse);
collapsingToolbar.setTitle("Project Name");
}
#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_project_details, menu);
return true;
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_project_details.xml
<android.support.design.widget.AppBarLayout
android:layout_height="192dp"
android:layout_width="match_parent">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_collapse"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="parallax"/>
</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"
android:layout_gravity="fill_vertical"
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_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/error_color"
android:textSize="20sp"
android:text="This text is part of first Cardview\n\n"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#color/abc_search_url_text"
android:text="This text is part of second Cardview\n\n"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
If you don't want your toolbar to move, it should be pinned. The toolbar scroll off can be accomplish with enterAlways|enterAlwaysCollapsed on the collapsing toolbar:
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_collapse"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
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>