0

How can I make the Title for Action bar clickable in Android? Example: In WhatsApp, when a group is tapped, a new Activity fires up with the Group information.

1
  • 2
    Create custom action bar layout Commented Aug 28, 2015 at 11:01

3 Answers 3

2

@Jayvir Chadha:

Create ActionBar with a custom layout in Android

You can inflate custom view to ActionBar setCustomView() method.

ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
    TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
    mTitleTextView.setText("My Own Title");

    ImageButton imageButton = (ImageButton) mCustomView
            .findViewById(R.id.imageButton);
    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                    Toast.LENGTH_LONG).show();
        }
    });

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);
}

custom_actionbar.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="50dp"
    android:background="@drawable/black_pattern" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@null"
        android:src="@android:drawable/ic_menu_rotate" />

</RelativeLayout>

Demo Reference .

2
  • 1
    Out of curiosity, what is the difference between using this method as opposite to treating Toolbar as a container and add it there directly in xml? pastebin.com/fbQw1Xct
    – poss
    Commented Aug 28, 2015 at 11:32
  • 1
    I think there are no basic difference Commented Aug 28, 2015 at 11:37
1

You can set a custom view with getActionBar().setCustomView. You can implement an OnClickListener for this custom view.

0

use new Toolbar, add custom view with TextView with OnClick

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
LayoutInflater mInflater=LayoutInflater.from(context);
View mCustomView = mInflater.inflate(R.layout.toolbar_custom_view, null);
toolbar.addView(mCustomView);

Not the answer you're looking for? Browse other questions tagged or ask your own question.