-1

I am new in android development and I want make a navigation drawer for my client.

How can I add a logo to my action bar in navigation drawer and remove the title.

my code :

Activity

 public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#D5D2D4")));
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
    actionBar.setDisplayUseLogoEnabled(true);
}

Manifest Activity

   <activity
        android:name=".activity.Grid_Home_Activity"
        android:label="@string/title_activity_grid__home_"
        android:theme="@style/Theme.ActionBarJp">
    </activity>

Styles

<style name="Theme.ActionBarJp" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarSize">60dp</item>
    <item name="logo">@drawable/logo</item>
    <item name="displayOptions">useLogo|showHome</item>
</style>

Can anyone help ?

amdixon
  • 3,814
  • 8
  • 25
  • 34
Jishad
  • 2,876
  • 8
  • 33
  • 62

4 Answers4

0

ActionBar is deprecated. Use ToolBar . Then at your Toolbar object center the ImageView with the logo. Take a look at this

Community
  • 1
  • 1
EE66
  • 4,601
  • 1
  • 17
  • 20
0

you can use https://github.com/jgilfelt/SystemBarTint this Library and use as below.

 public ActionBar enableCustomCenterTitle(Context context, boolean isShowBack, boolean isWhite) {
    ActionBar actionBar = enableCustomTitle();

    View actionbarLayout = LayoutInflater.from(context).inflate(
            R.layout.actionbar_layout, (ViewGroup) actionBar.getCustomView(),false);
    if (isWhite) {
        actionbarLayout = LayoutInflater.from(context).inflate(
                R.layout.actionbar_layout_white, (ViewGroup) actionBar.getCustomView(),false);
        TextView back = (TextView) actionbarLayout.findViewById(R.id.action_bar_back);
        back.setTextColor(getResources().getColor(R.color.orange));
        mTintManager.setStatusBarTintColor(getResources().getColor(R.color.white));
        setStatusBarDarkMode(true, this);
    }
    ActionBar.LayoutParams params = new
            ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);

    View backView = ButterKnife.findById(actionbarLayout, R.id.action_bar_back);
    if (isShowBack) {
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        backView.setVisibility(View.VISIBLE);
    } else {
        backView.setVisibility(View.GONE);
    }
    actionBar.setCustomView(actionbarLayout, params);
    return actionBar;
}
0

You can use setLogo to set a resDrawable or Drawable object. As to the logo size on the ActionBar, I believe it's 32x32 dp; Optical square, 24x24 dp according to this

To remove the title, just use actionBar.setTitle(""). This code makes it looks like there is no title.

One more thing, You should really change ActionBar to ToolBar.

Wesley
  • 4,084
  • 6
  • 37
  • 60
0

As actionbar is deprecated, it is better to user ToolBar and can utilize material design concepts in your app.

Do according below: 1. create toolbar.xml in your res folder.

<?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:background="@color/grey"

>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/iv_Logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

</LinearLayout>
</android.support.v7.widget.Toolbar>
  1. Open the layout file of your main activity (activity_main.xml) and add the toolbar.

    <?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:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">
    
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    </LinearLayout>
    

  2. In your MainActivity.java, add reference of Toolbar widget in onCreate() method :

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    
Karan Maru
  • 991
  • 6
  • 17
  • I am using navigation drawer, is it any issues with using the toolbar – Jishad Jun 18 '15 at 09:12
  • no, it would works properly. also go through this link :http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ , it will solve your problem. – Karan Maru Jun 18 '15 at 09:30