I am currently working on a project which include action bar. Here, i used custom action bar in different style which i want. But Here, i want to dynamically add new button on custom action bar using java class. I have no idea how it's possible? How can i achieve this?
My custom_actionbar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#1FA8B2" >
<TextView
android:id="@+id/actionbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/actionbar_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#FFFFFF"
android:text="@string/back"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:background="@drawable/back_button" />
</RelativeLayout>
My CustomActionbar.class
public class CustomActionbar {
ActionBar actionBar;
public CustomActionbar(final Activity activity,String title) {
actionBar = activity.getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setCustomView(R.layout.custom_action_bar);
actionBar.setDisplayShowCustomEnabled(true);
TextView title2 = (TextView) actionBar.getCustomView().findViewById(R.id.actionbar_title);
TextView back = (TextView) actionBar.getCustomView().findViewById(R.id.actionbar_back);
title2.setText(title);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
activity.onBackPressed();
}
});
}
}
Instead ofsetCustomView(int)
use setCustomView(android.view.View)
Inflate your layout, keep reference, add new views whenever you want.