Open top menu








In this blog implement custom ActionBar programatically.  we will add new RelativeLayout on ActionBar via ActionBarLayout, because ActionBarLayout get  ActionBar height and width .
 explain below...try

first you can past the Image inside res/drawable folder...




second you can create acttionbar_relative.xml class inside res/layout folder....

...................................................................................................................................................................
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="fill_parent"    
android:layout_height="wrap_content"    
android:padding="10dp"    
android:orientation="horizontal"    
android:gravity="center_vertical">

    
<TextView        
android:id="@+id/actionbar_textview"        
android:layout_width="wrap_content"        
android:layout_height="40dp"        
android:maxLines="1"        
android:gravity="center_vertical"
android:clickable="false"        
android:focusable="false"        
android:longClickable="false"       
android:textStyle="bold"        
android:text="heloghth"
android:textSize="18sp"        
android:textColor="#ffffff"        
android:layout_centerVertical="true"        a
ndroid:layout_centerHorizontal="true" />

<Button        
android:id="@+id/btnAddExpense"        
android:layout_width="wrap_content"        
android:layout_height="40dp"        
android:text="ok"        
android:textColor="#ffffff"        
android:textStyle="bold"        
android:background="#136009"        
android:layout_alignParentRight="true"        
android:layout_centerVertical="true"/>
</RelativeLayout>
...................................................................................................................................................................



MainActivity.java. you can get ActionBar before  setContentView(R.layout.activity_main); view and then .....
..................................................................................................................................................................
package com.example.sarvesh.testproject;

import android.app.ActionBar;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActionBar abar = getActionBar();
        abar.setBackgroundDrawable(getResources().getDrawable(R.drawable.preview));//line under the action bar        View viewActionBar = getLayoutInflater().inflate(R.layout.acttionbar_relative, null);
        ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.MATCH_PARENT,
                Gravity.CENTER);
        TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
        textviewTitle.setText("BeginnerPoint");
        textviewTitle.setTextSize(14);
        abar.setCustomView(viewActionBar, params);
        abar.setDisplayShowCustomEnabled(true);
        abar.setDisplayShowTitleEnabled(false);
        abar.setDisplayHomeAsUpEnabled(true);
        abar.setHomeButtonEnabled(true);
        setContentView(R.layout.activity_main);
    }
}
...................................................................................................................................................................





0 comments