How to add image in toolbar
Asked Answered
J

6

21

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

enter image description here

Jarrad answered 23/10, 2015 at 11:13 Comment(1)
@Blackbelt i am not talking about hamburger icon..i want image instead of titleJarrad
V
8

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>
Vex answered 23/10, 2015 at 11:17 Comment(5)
i am saying to use image as your toolbar backgroundVex
without using imageview how you gonna set the image??Jarrad
i have setted your image as toolbar background...read answer then try and then comment if not workVex
I used your method and set setTitle("") to the toolbar but i still get the name of my project in the title. I searched and removed it, now I am getting the package name in the title...Estremadura
I was successful in removing the title by app:title=" "; notice the spaceJarman
N
30
  <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();
            }
        });
    }
Needful answered 23/10, 2015 at 13:3 Comment(3)
getDrawable is deprecated as of API22Ungovernable
Having ImageView inside Toolbar is not allowed in version 28.Enamelware
you can put any views @EnamelwareNeedful
D
13

Use the logo display on the action bar.

    getSupportActionBar().setLogo(R.drawable.logo);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
Decipher answered 23/10, 2015 at 11:28 Comment(2)
it can display in center of toolbar and title moved to right sideAfield
I am using this method, but the image (logo) is now in the center of toolbar and the title is gone. How can I move logo to the right (end), and title to the left (start)?Sedimentary
V
8

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>
Vex answered 23/10, 2015 at 11:17 Comment(5)
i am saying to use image as your toolbar backgroundVex
without using imageview how you gonna set the image??Jarrad
i have setted your image as toolbar background...read answer then try and then comment if not workVex
I used your method and set setTitle("") to the toolbar but i still get the name of my project in the title. I searched and removed it, now I am getting the package name in the title...Estremadura
I was successful in removing the title by app:title=" "; notice the spaceJarman
J
1

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>
Jarrad answered 23/10, 2015 at 11:38 Comment(0)
P
0

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"
    />
Padre answered 23/10, 2015 at 11:18 Comment(6)
Is your drawable a valid resource?Padre
what do you mean valid resource?Jarrad
Is it a PNG image which you can see when opened in the editor? Are you actually including the Toolbar within activity_main.xml?Padre
yes it is png image..and see in my question and for more i already mentione the link of my exampleJarrad
Post activity_main.xmlPadre
Let us continue this discussion in chat.Jarrad
M
0

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"  
Martinamartindale answered 23/10, 2015 at 11:19 Comment(1)
i already tried with src no change with that..see my edited question for code nowJarrad

© 2022 - 2024 — McMap. All rights reserved.