how to place logo in the center of actionbar toolbar in android? - android

I want to display the logo at the centre of the Action Bar. Here's my layout code: the below code should work but it did not why? please help me
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo"
android:layout_gravity="center"
/>
</LinearLayout>
And in the onCreate()
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_logo);

Your Toolbar is just like a ViewGroup that can hold other views.So an easier approach will be to place an ImageView in the Toolbar.For eg:-
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/material_blue"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:theme="#style/ToolbarCustomIconColor" >
<RelativeLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_content" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/your_icon" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
You can create a custom Toolbar like this and manipulate it as much as you want .

Related

Adding items and design on toolbar toolbar

How to add the different type of designs and color to a toolbar and showing the value(value depends on user points) on toolbar gets updated whenever the user goes to another activity.
You can take RelativeLayout inside Toolbar and customize it according to your requirement. Here is sample Code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout 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="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"
app:navigationIcon="#mipmap/ic_launcher_round"
app:titleTextColor="#android:color/white">
<RelativeLayout
android:id="#+id/rl_plane_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible">
<ImageView
android:id="#+id/tv_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:src="#mipmap/ic_launcher_round"
android:text="Submit" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

app:elevation="0dp" hide layout inside toolbar

I have take custom layout inside the toolbar and perform action according to it.
but in some case i want make toolbar transparent so when i have set app:elevation="0dp" than it will hide my custom layout but still action will performed when click on that area.
I have used following layout inside toolbar.
Edited
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarMain"
app:elevation="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#null"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="#null"
android:padding="#dimen/activity_vertical_margin"
android:src="#drawable/ic_menu" />
<FrameLayout
android:id="#+id/frmNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/iv_menu"
android:layout_toRightOf="#+id/iv_menu"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:padding="#dimen/padding_5">
<ImageView
android:id="#+id/iv_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#null"
android:padding="#dimen/padding_5"
android:src="#drawable/ic_notification" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="#dimen/padding_5"
android:background="#drawable/bg_bage">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/eight"
android:textColor="#android:color/white"
android:textSize="#dimen/font_8" />
</FrameLayout>
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/frmNotification"
android:contentDescription="#null"
android:src="#drawable/logo_header" />
<ImageView
android:id="#+id/ivElasticSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="#dimen/padding_10"
android:paddingRight="#dimen/padding_15"
android:src="#drawable/ic_search" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
How to overcome this issue because in some scenario we want transparent toolbar but icon inside it should be display.
Here I have attached image how actually look like.
Your idea of app:elevation="someValue" is wrong, it does not hide the toolbar.The attribute app:elevation is used to show how much you want to make the toolbar or any other view that supports that attribute raised above the surface. Consider the surface as the floor on which the views are placed, now if you want to show some view like toolbar raised to other views you use that attribute.
if you want to make it transparent do it like this.
According to Material Design Guidelines, the elevation for appbar should be 4dp. So you should not set it to 0dp.
Use android:background="#null" instead of app:elevation="0dp".
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null">
EDIT
You want to remove the shadow of the toolbar. To do that use app:elevation="0dp" only for appbar layout. Your code before revision shows you used for both toolbar and appbar. Remove it for toolbar.
UPDATE
I tested the code and the following code seemed to do the work.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/testbg">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarMain"
app:elevation="0dp"
android:background="#null"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#null"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<!-- Rest of the code -->
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
OUTPUT IMAGE

Android the navigation toolbar icon and logo can not align

The toolbar layout I set android:layout_gravity="center_vertical"
but it not work
Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#drawable/toolbar_bg"
android:fitsSystemWindows="true"
app:popupTheme="#style/Theme.AppCompat.Light.DarkActionBar"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#android:drawable/sym_def_app_icon" />
main layout:
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/head_tool_bar"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/head_tool_bar" />
</RelativeLayout>
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment
android:id="#+id/navigation_drawer"
android:name="com.voogolf.helper.home.NavigationDrawerFragment"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/drawer_new_home" />
</android.support.v4.widget.DrawerLayout>
How can I set drawer icon and logo align top? Why left drawer icon have padding
like this?
you should try setting layout gravity like this.
android:layout_gravity="left|center_vertical"
and make sure padding and margin is 0dp
Android support v7 toolbar is just like other views. You can customize or add whatever the view you are required inside the toolbar.
Your imageView should be inside the toolbar and make imageView aligned whatever you want by gravity or layout_gravity attribute.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home_button"
android:background="#color/colorToolbarWelcomeBg"
android:scaleType="fitCenter"
android:src="#drawable/back_arrow"
android:layout_gravity="left|centre"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="#color/colorToolbarWelcomeBg"
android:text="Settings"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:id="#+id/welcomeToManager"
android:gravity="center"/>
</LinearLayout>
and place this layout inside <toolbar> Above layout </toolbr> This code showing just an idea not actual code :)
add this property attribute to imageView android:layout_gravity="center_vertical"
This is my toolbar with a back button and text in center.You can refer this code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/back_arrow"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="left|center"
android:visibility="visible"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/back_arrow" />
<com.indosat.rider.views.CustomTextView
android:id="#+id/toolbar_custom_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Register Page"
style="#style/roboto_medium"
android:textAllCaps="true"
android:textColor="#color/white"
android:textSize="19sp" />
</FrameLayout>
</android.support.v7.widget.Toolbar>
Let me know if you need any help in arranging your final setup.
if you are trying to customize your toolbar then do something like this.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
...>
<RelativeLayout>
<ImageView
... />
//here you can add more view items
</RelativeLayout>
</android.support.v7.widget.Toolbar>
if you have used toolbar and setting up the action bar using ActionBarDrawerToggle then add this code to your ActionBarDrawerToggle object.
toggle.setDrawerIndicatorEnabled(false); // this will disable your default haburger icon
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon); // this will replace the toggle home indicator

Landscape imageview overlap toolbar and statusbar

I have an imageview which is displayed in center of the screen. In portrait mode it displays fine but when i rotate screen in horizontal mode then imageview is opelapping my toolbar and status bar like in screenshot attached below. I tried couple of settings but it didn't change anything. I tired below toolbar option but it will attach imagview after toolbar but i need to display imageview in center.So I need a help to fix this laypout.Here is my layout xml code.
Imageview layout:
<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=".DetailActivity"
>
<include android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:transitionName="photo_hero"
android:id="#+id/image"
/>
</RelativeLayout>
toobal layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark" >
</android.support.v7.widget.Toolbar>
screenshot:
http://s9.postimg.org/6lyxbaq27/landscape_problem.png
http://s9.postimg.org/n8ghkdizz/landscape_problem_2.png
Put this:
android:layout_below="#+id/toolbar"
inside of your <ImageView> tag.
It should look like:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:transitionName="photo_hero"
android:id="#+id/image"
/>
Instead of relative layout try to use Frame Layout like:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_parent_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:transitionName="photo_hero"
android:id="#+id/image"
/>
</LinearLayout>
<include layout="#layout/toolbar"/>
As Viktor suggested. I used linearlayout with orientation vertical and with few parameter changes in my imageview.Here is the final code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DetailActivity"
android:orientation="vertical"
>
<include android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:id="#+id/image"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>

Place an image partially above toolbar

I would like how can do to have an image in my relative layout to be above (on top layer) my toolbar (in the same relative layout) and not below it. Any idea ?
The layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/splash"
android:adjustViewBounds="true"
android:layout_below="#id/my_toolbar"
android:gravity="top"
android:layout_alignTop="#id/my_toolbar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<include
android:id="#+id/my_toolbar"
layout="#layout/my_toolbar"
></include>
</RelativeLayout>
</FrameLayout>
the toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:background="#7dca2c"
android:elevation="4dp"
>
</android.support.v7.widget.Toolbar>
You don't need the RelativeLayout, your FrameLayout will handle that just fine.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/my_toolbar"
layout="#layout/my_toolbar"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/splash"
android:adjustViewBounds="true"
android:layout_gravity="top|center_horizontally"/>
</FrameLayout>
By having the included layout as the first child, it will be the first layer - and the ImageView will be the second. Adding android:layout_gravity="top|center_horizontally" to the ImageView will then align it correctly within the ViewGroup.

Categories

Resources