Using deprecated TabBar and SherlockActionBar in Android v.4 - java

I am using the deprecated Tab Bar at the bottom of my App. an example of my Tab Bar.
Then I used the ActionBarSherlock at the top of my Application. It works pretty fine for Android v2.3.5 and version3.2,
but when I run it on Android v4, the ActionBar disappear, and items on ActionBar appear as option-menu and the sub-menus appear as context-menu.
I can not change the architecture and use fragment instead of deprecated Tab Bar.
Do you have any idea how can I make the ActionBar appear in Android v4 ?
Or do you think it is impossible since I can not use fragment in this case.
public class MainActivity extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Styled);
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
System.out.println("Hello");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
And style.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
<item name="titleTextStyle">#style/TitleText</item>
<item name="android:titleTextStyle">#style/TitleText</item>
</style>
<!-- Title on action bar -->
<style name="TitleText" >
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">12.5dip</item>
</style>
</resources>

First of all in your application it's better to use SherlockFragmentActivity and SherlockFragment as it's content. At least in my opinion. Second on early versions of Android the actionbar items will be shown only when you set this value for them : android:showAsAction="always" .
For example :
<item
android:id="#+id/sync"
android:orderInCategory="1"
android:title="Sync"
android:icon="#drawable/ic_action_refresh"
android:showAsAction="always" />
In devices which has hardware menu button, the rest of elements will be places as sub-menus even if the android which you are running the app is 3+.
Using deprecated TabBar is not a problem for you, but there is a good example on how to use tabs with ActionBarSherlock using Fragments which you can find here : FragmentTabManager

Related

how to replace settings item with imagebutton

I am developing a navigation drawer activity in android studio, in menu folder there is a settings item at the left corner. I want to remove it and add an image button over there in action bar. Can anyone tell me how to add that image button and how to perform click event on that button in main activity.
Check below image for reference -
I hope this helps, add this inside your menu.xml layout -
The showAsAction attribute allows you to define how the action is displayed. For example, the ifRoom attribute defines that the action is only displayed in the action bar if there is sufficient screen space available.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="#drawable/ic_settings"
android:title="Settings">
</item>
</menu>
just try this.
no need to add imagebutton you just need to add
android:showAsAction="always"
and icon as
android:icon="#drawable/Your_icon"
do this in the menu.xml file, in item setting
Navigate to main_menu.xml in your menu folder under res and modify it as
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/image_button"
android:icon="#android:drawable/ic_menu_search"
android:title="Clickme"
app:showAsAction="always" />
</menu>
In your MainActivity
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId == R.id.image_button){
//do action on imagebutton click
return true;
}
return super.onOptionsItemSelected(item);
}

Items in menu not showing action bar, rather being displayed without any icons in three dots

Previously my app name was being displayed hence I had to use getSupportActionBar().setDisplayShowTitleEnabled(false) to remove the text, but now in its place a blank action is displayed with three dots on the side showing the list items that I have defined.
I want Refresh and Back actions to be displayed directly into the action bar with their respective icons
eventdetails.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_refresh"
app:showAsAction="always"
android:title="Refresh"
android:icon="#drawable/ic_action_refresh"
/>
<item
android:id="#+id/action_settings"
android:title="Settings"
app:showAsAction="ifRoom"
>
</item>
<item
android:id="#+id/action_back"
android:title="Back"
android:icon="#drawable/ic_action_back"
app:showAsAction="always"
/>
</menu>
EventDetails.java
public class EventDetails extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarEventDetails);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.eventdetails, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_back:
Intent action_back = new Intent(EventDetails.this, EventView.class);
startActivity(action_back);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Your xmlns:app is not setup properly
change:
xmlns:app="schemas.android.com/apk/res-auto"
to
xmlns:app="http://schemas.android.com/apk/res-auto"

how to force 3 dots in actionBar menu?

I have the following menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_totalCerdit"
android:showAsAction="always"
android:title="-100"/>
<item
android:id="#+id/actions_saved"
android:icon="#android:drawable/btn_star"
android:showAsAction="always"
android:title="#string/action_settings"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="#+id/action_settings"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_full_text"
android:showAsAction="never"
android:title="#string/action_full_list"/>
<item
android:id="#+id/action_saved_text"
android:showAsAction="never"
android:title="#string/action_favorites_list"/>
<item
android:id="#+id/logout"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/log_out"/>
</menu>
When deploying on Samsung Note2 The action Bar shows all items that don't have android:showAsAction="never". The ones with that attributes are show when pressing samsung built-in left bottom menu button.
When deploring on Nexus4 I see all items with android:showAsAction="never" as sub menus of "3 dots" item.
how can I make these 3 dots appear and behave in note2 the same as in nexus4?
In Compatibility Definition Document, Google said that Android 4.4+ device SHOULD NOT implement a dedicated physical button for the Menu function. So later device would be fine.
But it seems like NoteII makes a built in menu button for Overflow in Action bar (3 dots menu button). If you really want to do it on NoteII, there is an old hack that I found:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
// presumably, not relevant
}
Try this:
#Override
public boolean onPrepareOptionsMenu (Menu menu) {
return true;
}
Add this to all your Activity classes. Now you will see the same thing on both the Note 2 and the Nexus 4.
Just implement this method
#Override
public boolean onCreateOptionsMenu(Menu menu) {present.
getMenuInflater().inflate(R.menu.yourfile.xml, menu);
return true;
}

Remove/Hide searchView on only one activity

I have 3 activities in my project and I would like to remove/hide the searchView in the actionbar on only one activity.
I tried searchView.setVisibility(View.GONE); but there is still the clickable icon.
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_search"
android:title="#string/menu_search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView" />
<item
android:id="#+id/item_clear_memory_cache"
android:title="#string/menu_item_clear_memory_cache"/>
<item
android:id="#+id/item_clear_disc_cache"
android:title="#string/menu_item_clear_disc_cache"/>
</menu>
menu.findItem(R.id.menu_search).setVisible(false); should be the way.
You can hide the searchview and the searchicon by doing this:
searchItem.setVisible(false);
searchView.setVisibility(View.GONE);
Then you can bring it back by:
searchItem.setVisible(true);
searchView.setVisibility(View.VISIBLE);
Well, after some researches here you can find my complete example. Don't forget to set visibility and visible back in activity or fragment where you need to show your search view back.
I was looking a way to hide search view in preferences.
public class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this means that we're telling the Activity that is hosting the Fragment that we want to display menus
setHasOptionsMenu(true);
}
/**
* ********************* SEARCH ********************
*/
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) item.getActionView();
item.setVisible(false);
searchView.setVisibility(View.GONE);
}
}
And menu_main.xml
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
android:icon="#android:drawable/ic_menu_search"
app:showAsAction="always|collapseActionView"
/>

Disable Android menu group

I try to disable a menu group with the following code, but it doesn't work, menu items are still enabled. Can you tell me what's wrong please?
res/menu/menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/a" android:title="A"></item>
<item android:id="#+id/b" android:title="B">
<menu>
<item android:id="#+id/c" android:title="C" />
<item android:id="#+id/d" android:title="D" />
<group android:id="#+id/group_1">
<item android:id="#+id/e" android:title="E" />
<item android:id="#+id/f" android:title="F" />
</group>
</menu>
</item>
</menu>
Java:
public boolean onPrepareOptionsMenu (Menu menu) {
menu.setGroupEnabled (R.id.group_1, false); // does not work
menu.setGroupVisible (R.id.group_1, false); // does not work either
return super.onPrepareOptionsMenu (menu);
}
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater ().inflate (R.menu.menu, menu);
return true;
}
Thanks to user432209's info, here is the answer:
menu.findItem (R.id.b).getSubMenu ().setGroupVisible (R.id.group_1, false);
I'm not sure if you can use a group like this, but try this (its worth a shot):
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
Edit: Your problem is your menu structure and how you create the menu inside onCreateOptionsMenu due to using parent/child menus.
You create a menu for the parent menu, not the child menu, so that is why the call to setGroupEnabled and setGroupVisible fail. You need to create an object in onCreateOptionsMenu that represents the child in order for that object to be passed into onPrepareOptionsMenu and your code to work.
The above-mentioned solution works well but it should be written within onCreateOptionsMenu, here is the example of it:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu
inflater.inflate(R.menu.menu, menu);
// Make the menu item visible and enable it.
MenuItem item = menu.findItem(R.id.group_1);
item.setVisible(true);
item.setEnabled(false);
}

Categories