Android action bar: custom tabs and overflow

It is difficult for me to execute custom tabs for the action bar: I need to make tabs (I mean buttons) to use custom graphics for normal and selected states.

I was able to kill all the native styles using

<style name="customActionBarTabStyle"> <item name="android:background">#00000000</item> </style> 

and then use Tab.setIcon () so that the tabs look the way I need, but I need them to respond to the switch (by switching between the two Drawables - for the on and off state).

I tried to create a Drawable selector as follows:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/btn_on" /> <item android:drawable="@drawable/btn_off" /> </selector> 

but the tabs do not switch to pressed mode when selected.

Also, I tried calling Tab.setIcon () on TabListener.onTabSelected () and .onTabUnselected () - no luck either.

Does anyone know a good solution for this?

In addition, I need to display the user view instead of the overflow menu - I have already gathered a lot of “tips” to rethink my interface, to “follow the Android image”, but the problem is that the user interface is not relevant to rethink - this is the client :) , so I would love to find a way around the flaws of the ActionBar.

Any suggestions are greatly appreciated, thanks in advance.

+4
source share
4 answers

Try using state_selected instead of state_pressed, just trying to do something similar here (completely changing the background image for the tabs), and it seems that the tabs of the action bar only enter true / false, but not pressed states ...

EDIT: it looks like the background property is in this state, but not the text color used on the tabs ...

+1
source

I think you will need to use android: selectableItemBackground see this , this may help.

Good luck.

0
source

Just to make the solution more relevant:

@ taf said the solution: you need to set android:duplicateParentState="true" in the parent layout, which you use as the custom view for your tab.

0
source

Here is my code and icon will change when a tab is selected, and change it if it is not selected.

  public class MainActivity extends FragmentActivity implements ActionBar.TabListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.activity_main); // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setBackgroundDrawable(null); icons = new int[] { R.drawable.ic_facebook, R.drawable.ic_facebook, R.drawable.ic_facebook, R.drawable.ic_facebook, }; icons_selected = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, }; // For each of the sections in the app, add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page title defined by // the adapter. Also specify this Activity object, which implements // the TabListener interface, as the callback (listener) for when // this tab is selected. actionBar.addTab( actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setIcon(icons[i]) .setTabListener(this) ); } } 

and this one

  @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction){ getActionBar().getSelectedTab().setIcon(icons_selected[tab.getPosition()]); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { getActionBar().getSelectedTab().setIcon(icons[tab.getPosition()]); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } 
0
source

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


All Articles