Search code examples
javaandroidandroid-actionbarcustom-view

Access the content form customview in android


I am using the customview to add a xml in my Actionbar in ICS..Here is the code...I want to access the xml contents

        ActionBar actionBar = getActionBar();
    actionBar.show();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setCustomView(R.layout.abc);

In that view i have 2 button and a textview...Now i want to access those button in onclick...

Here is the xml :

abc.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Delosis V2"
        android:textSize="6.5pt"
        android:paddingTop="15dp" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

Now any solution please....


Solution

  • You use the getCustomView() method.

    To get the internal views

    View myView = actionBar.getCustomView();
    
    Button button1 = (Button)myView.findViewById(R.id.button1);
    Button button2 = (Button)myView.findViewById(R.id.button2);
    TextView textView = (TextView) myView.findViewById(R.id.textView1);
    

    You can use the findViewById(id) method on any View to get one of its internal views.