How to make a toolbar title clickable exactly the same as on an ActionBar without setting it as an ActionBar?

Background

I want to show the ActionBar about PreferenceActivity, since it is not available for devices with a preliminary cell (even in the support library).

Since such a class is not available in the support library, I cannot just call "setSupportActionBar". I also do not want to use a library (like this one ), since all the libraries that I found still use the Holo style, so they have not yet been updated with that in mind.

To do this, I try to imitate the look and feel of the ActionBar name in the new Toolbar class, which was introduced in the v7 support library.

Problem

I managed to put the title and the up button, but I can’t figure out how to make them clickable, like a regular action bar, including the touch effect.

Code

Here is 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> 

What i tried

I tried all types of toolbar class click listeners, but none of them helped.

The only thing that almost worked was to use "setNavigationIcon" instead of "setLogo", but it actually does a click on the title and thus only the icon shows the effect of clicking (even if I click on the title).

Question

How can I add a toolbar appearance so that the title and logo look like an ActionBar?

+6
source share
3 answers

Just remember that the toolbar is basically just a ViewGroup, so you can add a TextView to it and listen to such onClick events.

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 to clicks in your activity:

 toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d(TAG,"Clicked"); } }); 
+18
source

I had the same problem and I found a pretty convenient way for it (for the title title) by doing this with the Toolbar . It doesn't matter if the Toolbar used as an ActionBar or not, it works for both cases.

Just install the click listener directly on the Toolbar , this will cause the full Toolbar activate the click listener if the menu button or the home icon is not pressed. For me, this is exactly what I expect ...

For instance:

  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 will not trigger the onClick event. Sounds like a good solution

+18
source

You can do this quite easily without making any workarounds. You just need to look at all the child views of the Toolbar and find the TextView that received the correct header text.

 String title = toolbar.getTitle(); for(int i = 0; i < toolbar.getChildCount(); i++){ View tmpView = toolbar.getChildAt(i); if("android.support.v7.widget.AppCompatTextView".equals(tmpView.getClass().getName())){ if(((AppCompatTextView) tmpView).getText().equals(title)){ tmpView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //do whatever you want here } }); } } } 
+1
source

Source: https://habr.com/ru/post/977051/


All Articles