• 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

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

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 />

About Lorensius Londa

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

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 *

 

About Me

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania. 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

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

  • @malwrhunterteam Hi just want to make clarification here because i see my domain there (londatiga). Seems that they… https://t.co/SZ6oFCMlh183 days ago
  • Under-cabinet kitchen lighting with Nodemcu (Esp8266) and Openhab https://t.co/zXeaF5Ij5G via @YouTube #openhab #iot #smarthome #esp8266233 days ago

Copyright © 2021 · Londatiga.Net . All Rights Reserved