• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> Android >> Android Tips: How to Place Image or Logo at The Center of Actionbar

Android Tips: How to Place Image or Logo at The Center of Actionbar

January 24, 2014 by Lorensius Londa 1 Comment

When building an Android application, sometimes we need to put  application logo at the center of Actionbar. Android doesn’t provide a straight method to place a logo image at the center of Actionbar so we have to make a custom layout contains the logo image and place it on Actionbar using custom view.

andriod center actionbar

This sample code below demonstrates how to place a logo at the center of actionbar

<br />public class MainActivity extends Activity {<br /><br /><%%KEEPWHITESPACE%%>	@Override<br /><%%KEEPWHITESPACE%%>	protected void onCreate(Bundle savedInstanceState) {<br /><%%KEEPWHITESPACE%%>		super.onCreate(savedInstanceState);<br /><br /><%%KEEPWHITESPACE%%>		getActionBar().setDisplayHomeAsUpEnabled(false);<br /><%%KEEPWHITESPACE%%>		getActionBar().setDisplayShowTitleEnabled(false);<br /><br /><%%KEEPWHITESPACE%%>		if (findViewById(android.R.id.home) != null) {<br /><%%KEEPWHITESPACE%%>			findViewById(android.R.id.home).setVisibility(View.GONE);<br /><%%KEEPWHITESPACE%%>		}<br /><br /><%%KEEPWHITESPACE%%>		LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br /><br /><%%KEEPWHITESPACE%%>		View view = inflator.inflate(R.layout.view_ab, null);<br /><br /><%%KEEPWHITESPACE%%>		ActionBar.LayoutParams params = new ActionBar.LayoutParams(<br /><%%KEEPWHITESPACE%%>			     ActionBar.LayoutParams.WRAP_CONTENT,<br /><%%KEEPWHITESPACE%%>			     ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER );<br /><br /><%%KEEPWHITESPACE%%>		getActionBar().setDisplayShowCustomEnabled(true);<br /><%%KEEPWHITESPACE%%>		getActionBar().setCustomView(view, params);<br /><br /><%%KEEPWHITESPACE%%>		setContentView(R.layout.activity_main);<br /><%%KEEPWHITESPACE%%>	}<br />}<br />

How it works:

Disable the title text and up menu

<br />getActionBar().setDisplayHomeAsUpEnabled(false);<br />getActionBar().setDisplayShowTitleEnabled(false);<br />

Hide the default application logo on the left of actionbar

<br />if (findViewById(android.R.id.home) != null) {<br /><%%KEEPWHITESPACE%%>    findViewById(android.R.id.home).setVisibility(View.GONE);<br />}<br />

Inflate the custom view contains the logo image

<br />View view = inflator.inflate(R.layout.view_ab, null);<br />

Set the custom view at the center of actionbar

<br />ActionBar.LayoutParams params = new ActionBar.LayoutParams(<br /><%%KEEPWHITESPACE%%>			     ActionBar.LayoutParams.WRAP_CONTENT,<br /><%%KEEPWHITESPACE%%>			     ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER );<br />

Finally, set the custom view into actionbar

<br />getActionBar().setDisplayShowCustomEnabled(true);<br />getActionBar().setCustomView(view, params);<br />

Remember we have to call getActionBar().setDisplayShowCustomEnabled(true) to enable custom view

[ad]

Custom view (view_ab.xml)

<br />&lt;?xml version="1.0" encoding="utf-8"?&gt;<br />&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /><%%KEEPWHITESPACE%%>    android:layout_width="wrap_content"<br /><%%KEEPWHITESPACE%%>    android:layout_height="wrap_content"&gt;<br /><br /><%%KEEPWHITESPACE%%>    &lt;ImageView<br /><%%KEEPWHITESPACE%%>        android:id="@+id/iv_logo"<br /><%%KEEPWHITESPACE%%>        android:layout_width="wrap_content"<br /><%%KEEPWHITESPACE%%>        android:layout_height="wrap_content"<br /><%%KEEPWHITESPACE%%>        android:contentDescription="@string/app_name"<br /><%%KEEPWHITESPACE%%>        android:clickable="true"<br /><%%KEEPWHITESPACE%%>        android:src="@drawable/ab_logo" /&gt;<br /><br />&lt;/LinearLayout&gt;<br />
Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Select and Crop Image on Android
  2. How to Create Custom Window Title in Android
  3. How to Create Android Image Picker
  4. Coding Tips: How to Use Custom Font in Android

Filed Under: Android, Android, Programming Tagged With: actionbar, actionbarsherlock, Android, center logo

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. rejuan says

    June 8, 2014 at 4:25 pm

    Unfortunately your code is not readable. So please give a download link of your source code.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in