Tuesday, September 22, 2015

android - How to display custom layout in ActionBar

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="#d0f7ff"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Apply ActionBar Custom Layout"
        android:layout_marginTop="10dp"
        />
</RelativeLayout>
actionbar_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/tv"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#5b82ff"
    android:fontFamily="sans-serif-condensed"
    android:textSize="16dp"
    android:textStyle="bold"
    android:shadowRadius="15"
    android:shadowDx="5"
    android:shadowDy="5"
    android:shadowColor="#afc8ff"
    />
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
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;
import android.view.LayoutInflater;


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);
        final Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Get the ActionBar
                ActionBar ab = getActionBar();

                // Get the custom layout
                LayoutInflater inflater = getLayoutInflater();
                View customLayout = inflater.inflate(R.layout.actionbar_custom_view,null);

                // Get the TextView reference from custom layout
                TextView tv = (TextView)customLayout.findViewById(R.id.tv);

                // Set the custom layout TextView text
                // Set the ActionBar title text for current Activity
                tv.setText("Android Example : ActionBar Custom Layout");

                // Set the ActionBar display option
                ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

                // Set the ActionBar custom layout
                ab.setCustomView(customLayout);

                // Set a solid color background for ActionBar
                //ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFF9FBFF")));
           }
        });
    }
}
More android examples