Just like the screenshot of Google Plus above, I want to show a circular avatar image in the action bar. What should I do?
Asked
Active
Viewed 6,711 times
1
-
1Look [link](http://stackoverflow.com/questions/16208365/create-a-circular-image-view-in-android?answertab=votes#tab-top) – Skizo-ozᴉʞS ツ Jan 27 '15 at 09:11
-
@MD thanks but you can't ask STFW to all the questions. any way I've searched for several keywords but in vain. – Bitex Jan 27 '15 at 09:12
-
people who want solution for toolbar look at my answer https://stackoverflow.com/a/67459738/10390808 – Abhinav Chauhan May 09 '21 at 16:03
3 Answers
13
You can do this programatically using a Bitmap:
First do this on your activity's onCreate():
getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable drawable = new BitmapDrawable(getResources(), createCircleBitmap(sourceBitmap));
getSupportActionBar().setIcon(drawable);
Here is the createCircleBitmap() method:
public Bitmap createCircleBitmap(Bitmap bitmapimg){
Bitmap output = Bitmap.createBitmap(bitmapimg.getWidth(),
bitmapimg.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmapimg.getWidth(),
bitmapimg.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmapimg.getWidth() / 2,
bitmapimg.getHeight() / 2, bitmapimg.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapimg, rect, rect, paint);
return output;
}
PS: If you don't have the Bitmap, you can use this to transform a Drawable into a Bitmap:
Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_drawable);

Rodrigo Venancio
- 1,284
- 1
- 12
- 17
5
- Create circular imageview.
- Create custom view for actionbar using the circular imageview
- Inflate the view and display custom view as action bar.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/black_pattern" >
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
<com.example.CircularImageView
android:id="@+id/circularimageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"/>
</RelativeLayout>
Activity.java
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);

Fabin Paul
- 1,701
- 1
- 16
- 18
-
that's a solution. I'm using Toolbar as an action bar in Android 5.0 and I'm trying to create a drawable of the circular image via code, and use getSupportActionBar().setLogo() to put it onto the action bar. thanks a lot – Bitex Jan 27 '15 at 09:58
-
for androidx, please use below code:- ActionBar mActionBar = getSupportActionBar(); mActionBar.setDisplayShowHomeEnabled(false); mActionBar.setDisplayShowTitleEnabled(false); LayoutInflater mInflater = LayoutInflater.from(this); View mCustomView = mInflater.inflate(R.layout.actionbar, null); mActionBar.setCustomView(mCustomView); mActionBar.setDisplayShowCustomEnabled(true); – chitraksh bairathee Nov 17 '20 at 09:25
-1
when you Start Creating a Project (You Need to Follow the Third Step Below)
Step 1: New Android Project
Step 2 : Configure Your Project
Step 3 : Configure Launcher Icon
You can Choose a shape as Circle
I hope u needed this answer

mdsaleem1804
- 110
- 14
-
1that's not what I want. I want to display the user's avatar there instead of the app's icon – Bitex Jan 27 '15 at 09:15
-