android - How to get ActionBar height programmatically

activity_main.xml
 <RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/rl"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     tools:context=".MainActivity"     android:background="#edeedb"     >     <TextView         android:id="@+id/tv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginBottom="10dp"         android:fontFamily="sans-serif-condensed"         android:textSize="20dp"         android:textColor="#ff1513"         />     <Button         android:id="@+id/btn"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Get ActionBar Height"         android:layout_marginTop="10dp"         android:layout_below="@id/tv"         /> </RelativeLayout> 
MainActivity.java
 package com.cfsuman.me.androidcodesnippets;  import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.app.ActionBar; import android.widget.TextView;   public class MainActivity extends Activity {       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Get the widgets reference from XML layout         RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);         simpulan Button btn = (Button) findViewById(R.id.btn);         simpulan TextView tv = (TextView) findViewById(R.id.tv);          btn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 // Get the ActionBar                 ActionBar ab = getActionBar();                  /*                     public abstract int getHeight ()                         Retrieve the current height of the ActionBar.                          Returns                             The ActionBar's height                 */                 // Get the ActionBar height in pixels                 // You can convert this pixels value to dp unit                 int height = ab.getHeight();                  // Show ActionBar height in TextView                 tv.setText("ActionBar height = "+ height + " pixels");            }         });     } } 
More android examples

Komentar