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"
/>
Related
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"
I just a newbie in android programming, i am building a new app from a tutorial.
I have make the main menu like this
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context="id.go.kemenkeu.itjen_kemenkeu.MainActivity">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
app:showAsAction="never"
android:title="Settings" />
<item
android:id="#+id/navigate"
android:title="#string/next"
android:orderInCategory="200"
app:showAsAction="always"
android:icon="#drawable/ic_next"/> </menu>
and the MainActivity.Java like this
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings){
Toast.makeText(this,"Hi, Anda Baru Saja Menekan"+item.getTitle(),Toast.LENGTH_SHORT).show();
return true;
}
if(id==R.id.navigate){
startActivity(new Intent(this, SubActivity.class));
}
return super.onOptionsItemSelected(item);
} }
I call the +id/navigate in MainActivity.Java, but when i run in emulator, the style inside +id/navigate didn't show. What is wrong here?
you have to override createoptionmenu for intialize your own menu in toolbar.
try adding this in you activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mymenu_main, menu);
return super.onCreateOptionsMenu(menu);
}
without onCreateOptionsMenu you cant show menu. so add onCreateOptionsMenu to your Activity Class like,
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
Happy Coding!!!
You must inflate the option menu
Add this line.
getMenuInflater().inflate(R.menu.main, menu);
You have to override the onCreateOptionsMenu in you Activity and inflate the menu and check it
more details you have to check in Developer Site
In my application I have an app bar like the one in the image below
(source: jayway.com)
On the left of the appbar I have a TextView and on the right a SearchView and a menu.
How can I implement the search action?
I would like the searchview expanding to the entire width of the app bar and hiding the TextView on the left, when search icon is pressed.
Something like Youtube or Gmail apps.
please help me. Thanks.
The item (add it in res/menu/menu.xml):
<menu>
<!-- ... ->
<item android:id="#+id/action_search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
don't forget to override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
MenuItem menuItemSearch = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)
MenuItemCompat.getActionView(menuItemSearch);
return true;
}
I've been trying to get an Action Button working on my Android app, and been failing miserably. I've read so many questions and answers and nothing seems to work. The video link always just appears in the menu which I do not want
Here is the menu Xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:puertogaleradivesites="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="#+id/youtube"
android:icon="#drawable/ic_play_arrow_black_24dp"
android:title="YouTube"
puertogaleradivesites:showAsAction="ifRoom"/>
<item android:id="#+id/action_settings"
puertogaleradivesites:showAsAction="never"
android:title="Settings"/>
</menu>
And here is the main Java Fragment
public class DivesiteFragment extends Fragment {
private Divesite mDivesite;
private TextView mDepthField;
private TextView mSiteDescription;
private TextView mDistance;
private ProportionalImageView mImageView;
public static final String EXTRA_SITE_ID = "com.blueribbondivers.extraSiteID";
private Context mContext;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
UUID crimeId = (UUID)getActivity().getIntent()
.getSerializableExtra(EXTRA_SITE_ID);
mDivesite = DiveSites.get(getActivity()).getDiveSite(crimeId);
mContext = getActivity().getApplicationContext();
//getActivity().getActionBar().setTitle(Html.fromHtml("<font color=\"#1c3565\">" + mDivesite.getName() + "</font>"));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_divesite, parent, false);
Resources resources = mContext.getResources();
//setHasOptionsMenu(true);
mDepthField = (TextView)v.findViewById(R.id.divesite_display_depth);
mDepthField.setText(mDivesite.getMaxDepth());
mSiteDescription = (TextView)v.findViewById(R.id.divesite_display_description);
mSiteDescription.setText(mDivesite.getSiteDescription());
mSiteDescription.setMovementMethod(new ScrollingMovementMethod());
mDistance = (TextView)v.findViewById(R.id.divesite_display_distance);
mDistance.setText(mDivesite.getTravelTime());
mImageView = (ProportionalImageView)v.findViewById(R.id.divesite_display_imageView);
String imageName = mDivesite.getPhoto();
final int resourceID = resources.getIdentifier(imageName,"drawable",mContext.getPackageName());
mImageView.setImageResource(resourceID);
setHasOptionsMenu(true);
return v;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.divesitemenu, menu);
super.onCreateOptionsMenu(menu,inflater);
}
}
I have the full project on Github also https://github.com/Jonnyblue/PuertoGaleraDiveSites
Any help would be greatly appreciated.
For displaying menus like Youtbe, gmail:
You should use yourapp:showAsAction="always"
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".HomeActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
yourapp:showAsAction="always"
android:title="#string/search"/>
<item
android:id="#+id/action_refresh"
android:icon="#drawable/ic_refresh"
yourapp:showAsAction="always"
android:title="#string/refresh"/>
</menu>
Hope it will help you.
Your activity extends FragmentActivity. If you'd like a backward compatible action bar that works the same on all API levels (things changed significantly in API 21), you should instead extend AppCompatActivity
If you want to show an ActioBar item a as a button but not as an overflow menu, You need to define an android:orderInCategory for it. So that it doesn't comes inside the overflow menu.
<?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/action_search"
android:icon="#drawable/ic_magnify"
android:orderInCategory="100"
app:showAsAction="ifRoom|collapseActionView"
android:title="#string/noticiation_count"/>
</menu>
Also as you are using fragments its safe to use this line inside the onCreate() method,
setHasOptionsMenu(true);
Tested and working perfectly with Fragments.
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