4

I want to display both icon and title in the action bar of an Activity. Here is the screenshot of my app now:

enter image description here

Now I need the app logo be displayed to the left of MyApp text in the action bar. I have already tried the following code to set the logo but nothing happened:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setLogo(R.drawable.ic_launcher_web);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        setContentView(R.layout.activity_main);

    }
Pooya
  • 6,083
  • 3
  • 23
  • 43

7 Answers7

2

I just tried with your code, and this works.

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.mipmap.ic_launcher);

Make sure your parent theme is set to Theme.AppCompat.Light.NoActionBar.

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
Antrromet
  • 15,294
  • 10
  • 60
  • 75
1

You can make a new layout for Action bar and you could implement in main activity
Create new Layout

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:minHeight="60dp"
android:id="@+id/topHeaderNav"
android:background="@color/primary"
android:layout_height="60dp"
tools:showIn="@layout/activity_main">

<ImageView
    android:layout_width="50dp"
    android:maxHeight="75dp"
    android:maxWidth="75dp"
    android:id="@+id/imgTop"
    android:layout_centerVertical="true"
    android:src="@drawable/mag"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/topTitleText"
    android:layout_toRightOf="@id/imgTop"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:text="Magnetic Orange"
    android:textColor="#FFFFFF"
     />

and implement it in Main Layout

 `<include layout="@layout/navbar"
    android:id="@+id/topHeaderNavMainActivity"
    />`

I think it will help you

Madhab Dhakal
  • 103
  • 1
  • 11
0
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
Apurva
  • 7,871
  • 7
  • 40
  • 59
support_ms
  • 1,873
  • 1
  • 18
  • 33
0

Fist of all be sure you have used style with NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>

Set up your toolbar in onCreate() and be sure it's V7

Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);

Your toolbar in xml looks like

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="60dp"/>

And use those methods you have previously tried. This will work.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • I tried it but the application closes and I got the following message from adb: "Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setLogo(int)' on a null object reference" – Pooya Feb 02 '16 at 04:40
  • @Pooya are you sure you are inheriting from android.support.v7.app.ActionBarActivity – Shree Krishna Feb 02 '16 at 04:44
  • Actually i tried that before but Android Studio mentioned it as depreciated and suggests AppCompatActivity instead – Pooya Feb 02 '16 at 04:47
  • @Pooya I have added some necessary attributes in the toolbar itself in my answer. Try replacing this code. Theme affects !! – Shree Krishna Feb 02 '16 at 04:53
  • I still get the previous error. It seems that there is something wrong when trying to get the toolbar resource – Pooya Feb 02 '16 at 04:55
  • @Pooya It's really being hard finding out your issue.. Either you have multiple activities which are extending different activities or many reasons could be possible. Try extending only Activity in all of your activities you have and use the common theme I provided above. If you find another solution for this then please let me know. – Shree Krishna Feb 02 '16 at 05:04
0

if your require to customize a action bar then use costume layout in action bar, following was the code which explain how customize the action bar,

In your activity put this code onCreate method

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.actionbar_layout, null); //hear set your costume layout
        getSupportActionBar().setCustomView(v);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#8731a0")));

it is the code which activiy inherit AppCompatActivity or if you are inheriting activity then replace getSupportActionBar() with getActionBar() and use.

Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
0
     private ViewGroup actionBarLayout;

      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home_screen);
            context = this;

            actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_actionbar, null);

            actionBarLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

            // Set up your ActionBar

    /* final ActionBar actionBar = getActionBar();*/

            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setCustomView(actionBarLayout);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.green)));

            init();
            event();
        }

and used following style.

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/green</item>
        <item name="colorPrimaryDark">@color/green</item>

    </style>
Krunal Patel
  • 61
  • 3
  • 12
0

Try Custom ActionBar.

custom_actionbar.xml

Layout XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize" 
    android:clickable="true"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="1">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="15sp"
            android:background="@drawable/emaact"
            android:layout_alignParentBottom="true"
            android:layout_margin="10dp"
            android:id="@+id/imageView"/>

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

            <TextView
                android:layout_width="75dp"
                android:layout_height="match_parent"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Project"
                android:layout_gravity="center_vertical"
                android:textSize="20sp"
                android:id="@+id/textView" android:gravity="center_vertical"
                android:textStyle="bold"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.99"
            android:weightSum="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/txttitle"
                android:text="title"
                android:textStyle="normal"
                android:textSize="20sp"
                android:gravity="center"
                android:layout_weight="1.06"/>

        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menuline"
            android:layout_margin="10dp"
            android:id="@+id/slidingmenu"
            android:layout_gravity="center_vertical"/>

    </LinearLayout>
</RelativeLayout>

In Your MainActivity, Use This code.

MainActivity.java

public void customBar(){
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);

    LayoutInflater inflater = LayoutInflater.from(this);
    View customView = inflater.inflate(R.layout.custom_actionbar, null);

    TextView text =(TextView) customView.findViewById(R.id.txttitle);
    text.setText("Home");
    ImageView menus =(ImageView) customView.findViewById(R.id.slidingmenu);

    actionBar.setCustomView(customView);
    actionBar.setDisplayShowCustomEnabled(true);
}

Your Action Bar looks like... Custom ActionBar

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51