0

I was trying to add image inside actionBar with help of this following answer https://stackoverflow.com/a/30998517/3836908

here is what i tried to add image

<?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"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/tool_bar"
            layout="@layout/toolbar">
        </include>

        <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"
            tools:context=".MainActivity">

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/recyclerView">

            </android.support.v7.widget.RecyclerView>

        </RelativeLayout>

    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:itemBackground="@drawable/custom_ripple"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        android:id="@+id/nv">

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

here is the result: enter image description here

instead of adding image to title bar it creates a new bar to display image.

her is what am trying to do i want to add image to appBar something like below image, enter image description here

her is my actual code before

<android.support.v4.widget.DrawerLayout
    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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark">

    <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"
        tools:context=".MainActivity">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recyclerView">

        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:itemBackground="@drawable/custom_ripple"
        app:menu="@menu/navigation_menu"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        android:id="@+id/nv">

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>
Stacker
  • 23
  • 7
  • That example is assuming that you've set a `NoActionBar` theme for the `Activity`, and then called `setSupportActionBar()` with your `Toolbar` (which they do mention at the end). – Mike M. May 28 '20 at 09:44
  • @MikeM. no here is what `Theme.AppCompat.Light.DarkActionBar` thats the one theme i added to activity though `style.xml` – Stacker May 28 '20 at 09:46
  • Yeah, that theme has a default `ActionBar` already, which is the top one you see in your first image. You need to disable that if you want to use your own `Toolbar` in its place; e.g., `Theme.AppCompat.Light.NoActionBar`. – Mike M. May 28 '20 at 09:48
  • @MikeM. so the only option is to hide default actionbar, and use custom actionbar ? – Stacker May 28 '20 at 09:53
  • Not necessarily, but that's what you appear to be attempting to do, since you have your own `Toolbar`, apparently. – Mike M. May 28 '20 at 09:55
  • @MikeM. this is what am trying to do https://i.stack.imgur.com/g5GAc.jpg – Stacker May 28 '20 at 09:58
  • Yeah, I got that, but there are multiple ways to do it. You're mostly set up to do it with a custom `Toolbar`. Is there some reason that you don't want that? – Mike M. May 28 '20 at 09:58
  • @MikeM. i just want to add image and use `setOnClickListener` to change `GridLayoutManager` spam count. – Stacker May 28 '20 at 10:16
  • Well, if you go with the custom `Toolbar` option, you need to change your theme, and add that `setSupportActionBar()` call, but you could then find your `Button` with `findViewById()` like normal, and set a regular `OnClickListener` on it. If instead you want to add your icon to the existing `ActionBar`, then you'd pretty much have to add it as an options menu item, and that setup is quite different. – Mike M. May 28 '20 at 10:23
  • @MikeM. can you guide me with easy method, a bar with title, image(`setonclicklistner`) – Stacker May 28 '20 at 10:27

0 Answers0