2

I want to show profile picture on Action Bar just before logout icon but its showing the image just after the App title. I have used Image View and Picasso for load image from server using volley. How can achieve this ?

actionBar.setDisplayOptions(actionBar.getDisplayOptions()
                            | ActionBar.DISPLAY_SHOW_CUSTOM);

                    imageView.setScaleType(ImageView.ScaleType.CENTER);
                    Picasso.with(HomeActivity.this).load(AppConfig.profilePic + image).placeholder(R.drawable.profile).transform(new CircleTransform()).fit().into(imageView);
                    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
                            ActionBar.LayoutParams.WRAP_CONTENT,
                            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                            | Gravity.CENTER_VERTICAL);
                    layoutParams.gravity = Gravity.RIGHT;
                    imageView.setLayoutParams(layoutParams);
                    actionBar.setCustomView(imageView);

enter image description here

Moin Khan
  • 674
  • 2
  • 9
  • 27

3 Answers3

3

You want to create custom toolbar like below

<?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/toolbarLogo"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/ivBackArrow"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_centerVertical="true"
            android:background="@drawable/ic_back_arrow_black" />

        <TextView
            android:id="@+id/toolbar_text_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="left|center_vertical"
            android:singleLine="true"
            android:layout_toRightOf="@+id/ivBackArrow"
            android:layout_marginLeft="4dp"
            android:layout_toLeftOf="@+id/iv_profile"
            android:layout_centerHorizontal="true"
            android:text="ALERT"
            android:textColor="@color/colorBlack"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/iv_profile"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/iv_logout"
            android:background="@drawable/ic_back_arrow_black" />

        <ImageView
            android:id="@+id/iv_logout"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_back_arrow_black" />
    </RelativeLayout>

</android.support.v7.widget.Toolbar>
SWAPDROiD
  • 357
  • 3
  • 15
3

this can not be done there as its standard to show title first Solution >> use custom action bar 1- build your own layout 2- change the theme to NoAction bar for your app in style file 3- use include to add your custom actionBar to any layout you want

<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"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways"
    app:layout_collapseMode="pin">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/toolbar_logo"
            android:src="@drawable/logo"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="?attr/actionBarSize"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp" />

        <TextView
            android:id="@+id/toolbar_title"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="?attr/actionBarSize"
            android:layout_gravity="center"
            android:gravity="center_vertical"
            android:visibility="gone"
            android:text="@string/app_name"
            android:textColor="@color/white"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
            />


    </FrameLayout>
</android.support.v7.widget.Toolbar>
ahmed shaaban
  • 116
  • 1
  • 3
0

You can set your image as app logo. Try the below code

Picasso.with(this).load(yourImageUrl).into(new Target()
   {
       @Override
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
       {
           Drawable d = new BitmapDrawable(getResources(), bitmap);
           ab.setIcon(d);
           ab.setDisplayShowHomeEnabled(true);
           ab.setDisplayHomeAsUpEnabled(true);
       }

       @Override
       public void onBitmapFailed(Drawable errorDrawable)
       {
       }

       @Override
       public void onPrepareLoad(Drawable placeHolderDrawable)
       {
       }
   });
Suresh Kumar
  • 2,014
  • 3
  • 19
  • 32