I wish to show an ActionBar on PreferenceActivity, as it's not available for pre-Honeycomb devices (even in the support library).
Because such a class isn't available on the support library, I can't just call "setSupportActionBar" . I also don't wish to use a library (like this one) since all of the libraries I've found still use Holo style, so they didn't get updated so far, with this in mind.
To do this, I'm trying to mimic the look&feel of the title of the ActionBar into the new Toolbar class, that was presented on support library v7 .
I've succeeded putting a title and a an "up" button, but I can't find out how to make them clickable like a normal action bar, including the effect of touching.
Here's the code:
SettingsActivity.java
public class SettingsActivity extends PreferenceActivity
{
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
addPreferencesFromResource(R.xml.pref_general);
final Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.windowBackgroundColor));
toolbar.setTitle("Settings");
toolbar.setClickable(true);
toolbar.setLogo(getResIdFromAttribute(this,R.attr.homeAsUpIndicator));
}
public static int getResIdFromAttribute(final Activity activity,final int attr)
{
if(attr==0)
return 0;
final TypedValue typedvalueattr=new TypedValue();
activity.getTheme().resolveAttribute(attr,typedvalueattr,true);
return typedvalueattr.resourceId;
}
}
activity_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.antonioleiva.materialeverywhere.SettingsActivity" >
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/abs__ab_solid_shadow_holo" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
I've tried all of the click listeners types of the Toolbar class, but none helped.
The only thing that almost worked is to use "setNavigationIcon" instead of "setLogo", but that actually makes the title&icon being clicakable, yet only the icon shows its effect of being clicked (even if I click on the title).
How can I add the look & feel of the Toolbar, to have the title & logo of it to look like the ActionBar?
The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and customizable in terms of appearance and functionality. Unlike ActionBar, its position is not hardcoded i.e., not at the top of an activity.
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.
I had the same problem and found a quite convenient way for it (for the clickable title), doing this with the Toolbar
. It does not matter, if the Toolbar
is used as ActionBar
or not, it works for both cases.
Just set the click listener on the Toolbar
itself, this will result in the complete Toolbar
firing the click listener, if not a menu item or the home icon is clicked. For me, that's exactly what I expect...
For example:
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getThis(), "Toolbar title clicked", Toast.LENGTH_SHORT).show();
}
});
onOptionsItemSelected
, home click and similar WON'T fire the onClick
event. Seems like a good solution
Just remember that the Toolbar is basically just a ViewGroup so you can add a TextView to it and listen to onClick events like that.
Add TextView to Toolbar in XML:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
Listen for clicks in your Activity:
toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"Clicked");
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With