ActionBar Button Selected State - android

I am working with ActionBar.
I have 3 buttons on ActionBar, and have 3 Activities. When I press the button it navigate to other Activity but I want to do which Button i pressed that button shows it selected state on ActionBar.
My Menu Code is bellow
<?xml version="1.0" encoding="utf-8"?>
<!-- Scan View -->
<item android:id="#+id/action_scan"
android:icon="#drawable/scan_icon_deselected_d1"
android:title="#string/scan_view"
android:showAsAction="always" />
<!-- Settings View -->
<item android:id="#+id/action_settings"
android:icon="#drawable/settings_icon_deselected_d1"
android:title="#string/settings"
android:showAsAction="always" />
<!-- Help Menu -->
<item android:id="#+id/action_help"
android:icon="#drawable/whitequestionmark_deselected"
android:title="#string/help_menu1"
android:showAsAction="always" />
And at Style
<!-- Application theme. -->
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyTheme.ActionBar</item>
<item name="android:windowContentOverlay">#null</item>
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="MyTheme.ActionBar" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:backgroundStacked">#android:color/transparent</item>
<item name="android:displayOptions">showTitle</item>
</style>

Try like this:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
for (int i = 0; i < mainMenu.size(); i++) {
// Set default icons
if (mainMenu.getItem(i).getItemId() == R.id.action1) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default1);
} else if (mainMenu.getItem(i).getItemId() == R.id.action2) {
mainMenu.getItem(i).setIcon(R.drawable.icon_default2);
} else if (.....) {
.....
}
}
if (item.getItemId() == R.id.action1) {
item.setIcon(R.drawable.icon_highlighted1);
} else if (item.getItemId() == R.id.action2) {
item.setIcon(R.drawable.icon_highlighted2);
} else if(.....) {
.....
}
return super.onMenuItemSelected(featureId, item);
}

I Solved it to add in onCreateOptionsMenu(Menu menu).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() == R.id.action_scan) {
menu.getItem(i).setIcon(R.drawable.scan_icon_deselected_d1);
} else if (menu.getItem(i).getItemId() == R.id.action_settings) {
menu.getItem(i).setIcon(R.drawable.settings_icon_deselected_d1);
} else if (menu.getItem(i).getItemId() == R.id.action_help) {
menu.getItem(i).setIcon(R.drawable.whitequestionmark_selected);
}
}
return super.onCreateOptionsMenu(menu);
}

Related

How to add menu options when theme is NoActionBar in style.xml

I have use Fragments so, I had to use theme NoActionBar, but when I inflate option menu it does not showing anything.How to resolve this?
Here is my Code:
MainActivity.java
> #Override
> public boolean onCreateOptionsMenu(Menu menu){
> MenuInflater inflater =getMenuInflater();
> inflater.inflate(R.menu.options,menu);
> return super.onCreateOptionsMenu(menu);
> }
> #Override
> public boolean onOptionsItemSelected(MenuItem item){
> switch(item.getItemId()){
> case R.id.item1 :
> Intent box = new Intent(MainActivity.this,Developer.class);
> startActivity(box);
> break;
> }
> return true;
> }
Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Developer" android:id="#+id/item1"/>
</menu>
You can add menu right to your toolbar using android.support.v7.widget.Toolbar#inflateMenu method. This is example it kotlin
with(toolbar) {
inflateMenu(R.menu.menu_account_detail)
setOnMenuItemClickListener {
if (it.itemId == R.id.search) {
doSearch()
}
true
}
}

How to remove three dots from the toolbar of navigation drawer

I want to remove that 3 dots from toolbar and also want to add an image with textview on that position.That image and textviews are different for each fragment activity.
Also can anyone tell me how to make the toolbar transparent.
In your MainActivity you will have optionmenu, just make it false
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
Remove this item from menu.
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
visibility=false
android:title="#string/action_settings"/>
1)your First Question about removing three dots (Aditya -Vyas )solved your prob
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return false;
}
2) Now to put image view
Crate Menu Resource
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_submit"
android:title="#string/submit"
android:icon="#drawable/ask_price_back"
app:showAsAction="always" />
</menu>
Then declare onCrateOptionsMenu and onOptionsImtemSelected as below
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_submit, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_submit) {
//Your code to handle click
return true;
}
return super.onOptionsItemSelected(item);
}
3) Handling The menu from the fragment loaded in activity try this
override this method in your fragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_of_fragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_fragment) {
// Your code to handle Click
}
return super.onOptionsItemSelected(item);
}
1) From your menu.xml remove all items with tag .
This will remove three dots from your menu area.
2) Add a new item in your xml and give properties
android:icon="#drawable/image_you_want to show in the menu area"
app:showAsAction="always"
set your style like
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<!-- Style to replace actionbar overflow icon. set item 'android:actionOverflowButtonStyle' in AppTheme -->
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_launcher</item>
</style>
check this post : post
> for adding imageview and text
` <item android:id="#+id/action_cart"
android:icon="#drawable/cart2"
android:title="image"
app:showAsAction="always" />
<item
android:id="#+id/badge"
app:actionLayout="#layout/counter"
android:title="image"
android:icon="#drawable/shape"
app:showAsAction="always">
</item>`
In Layout counter
`<TextView
android:id="#+id/notif_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="20dp"
android:minHeight="20dp"
android:background="#drawable/shape"
android:text="0"
android:textSize="14sp"
android:textColor="#fff"
android:gravity="center"
android:padding="2dp"
android:singleLine="true">
</TextView>`
Just add this to you theme:
<item name="actionOverflowButtonStyle">#id/invisible</item>

Actionbar menu items not visible

I want icons on action bar, but I am getting them in the menu options...
Here is my xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="always" />
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always" />
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always" />
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always" />
</menu>
and here is the java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
menu.toggle(true);
return true;
}
if(id == R.id.coc){
startActivity(new Intent(Dashboard.this,CircleOfCare.class));
}
return super.onOptionsItemSelected(item);
}
What am I missing.. I am extending Activity.. If I extend ActionBarActivity, it says, use theme.appcompat or descendant even when I am using that theme only. Please help, thanx in advance
If you aren't using ActionBarActivity then try using
android:showAsAction="always"
instead of
app:showAsAction="always"

ActionBar icons not visible

I am adding icons to ActionBar, but instead of getting them on ActionBar, I get them in the menu option..
My Java Code-
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dashboard, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.report) {
Toast.makeText(getApplicationContext(),"Report",Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
My dashboard.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/share"
android:orderInCategory="100"
android:title="Share on Whatsapp"
app:showAsAction="never"/>
<item
android:id="#+id/medication"
android:title="My Medications"
android:icon="#drawable/medication"
app:showAsAction="always"/>
<item
android:id="#+id/coc"
android:title="Add Circle of care"
android:icon="#drawable/add_icon"
app:showAsAction="always"/>
<item
android:id="#+id/report"
android:title="Report"
android:icon="#drawable/report"
app:showAsAction="always"/>
</menu>
Please tell what am I missing..
I have appcompat as library.
Thank you
Extended ActionBar or AppCompactActivity
First menu item has app:showAsAction="never", so others menu items will always be shown only in overflow menu.

search view with back icon in android

In my project I am using search view of android.
It is working properly.
But I am having one problem i.e related to back image while searchview expands.
This is my action bar with icon and other search and drawable icon.
Now when I click on search Icon
Here Icon is changes. It's by default taking launcher image. but I want it to be same as its showing in image 1.
So can any one help me with this issue.
edit1
here is my action bar style xml.
<style name="Theme.Example" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarItemBackground">#drawable/selectable_background_example</item>
<item name="actionBarTabStyle">#style/ActionBarTabStyle.Example</item>
<item name="actionBarStyle">#style/ActionBar.Solid.Example</item>
<item name="actionModeBackground">#drawable/cab_background_top_example</item>
<item name="actionModeSplitBackground">#drawable/cab_background_bottom_example</item>
<item name="actionBarTabTextStyle">#style/ActionBarTabText.Example</item>
<!-- Light.DarkActionBar specific -->
<item name="android:textColorHighlight">#99c0c0c0</item>
</style>
edit2
substyle for actionbar
<style name="ActionBar.Solid.Example" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/ic_logo</item>
<item name="background">#drawable/ab_solid_example</item>
<item name="backgroundStacked">#drawable/ab_stacked_solid_example</item>
<item name="backgroundSplit">#drawable/ab_bottom_solid_example</item>
</style>
edit 3
This is my onCreateOptionsMenu method.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
try {
search = (MenuItem) menu.findItem(R.id.search);
MenuItemCompat.setOnActionExpandListener(search,
new OnActionExpandListener() {
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
layoutList.setVisibility(View.INVISIBLE);
return true; // Return true to collapse action view
}
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
layoutList.setVisibility(View.VISIBLE);
return true; // Return true to expand action view
}
});
SearchView searchView = (SearchView) MenuItemCompat
.getActionView(search);
setSearchTextColour(searchView);
searchView.setQueryHint(getString(R.string.search_hint));
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
// TODO Auto-generated method stub
Debugger.debugE("on close");
layoutList.setVisibility(View.INVISIBLE);
return false;
}
});
} catch (Exception e) {
// TODO: handle exception
}
return super.onCreateOptionsMenu(menu);
}
and to show actionbar
ActionBar actionBar = getSupportActionBar();
SpannableString s = new SpannableString(getString(R.string.app_name));
s.setSpan(new TypeFaceSpan(getContext(),
getString(R.string.font_gothum)), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#bb0404")));
There is nothing else that I had used.
note: In all other navigation from the main screen icon which is showing in first image is coming.
Issue is only with searchview.
Thanks in advance.
Bskania
Try changing your app theme on your styles.xml
You can do something like this:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBar</item>
</style>
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:icon">#drawable/myicon</item>
</style>
create a custom action bar :
LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_search_actionbar, null);
actionBar.setCustomView(v);
_searchView = (SearchView)v.findViewById(R.id.searchView);
....
....
....
and define ids of views and use .

Categories

Resources