Get ActionBar height in android in Pixels programmatically

How to discover and show action title bar height dynamically in android activity application.

In this tutorial we are getting action title bar’s height dynamically because action bar size is different for each device and by getting their size we can easily set background image in it. So here is the complete step by step tutorial for Get ActionBar height in android in Pixels programmatically.

android-project-download-code-button

How to Get ActionBar height in android in Pixels programmatically.

Code for MainActivity.java file.

 package com.getactionbarheight_android_examples.com;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

 Button getHeight;
 TextView ShowHeight;
 int actionbarheight;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ShowHeight = (TextView)findViewById(R.id.textView1);
 getHeight = (Button)findViewById(R.id.button1);
 
 getHeight.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 ActionBar actionbar = getActionBar();
 actionbarheight = actionbar.getHeight();
 
 ShowHeight.setText(String.valueOf(actionbarheight) + " Pixels ");
 }
 });
 }

}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.getactionbarheight_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click here to Get ActionBar height in android in Pixels programmatically" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="52dp"
 android:text="Height Show here"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Get ActionBar height in android in Pixels programmatically

Click here to download Get ActionBar height in android in Pixels programmatically project with source code.

Leave a Reply

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