How to add CheckBox to Toolbar? - android

I've tried using the following code but no checkbox appear, only text :
<item
android:id="#+id/menuShowDue"
android:actionViewClass="android.widget.CheckBox"
android:title="#string/string_due"
android:checkable="true"
app:showAsAction="ifRoom" />
Is it not possible to add it via menu ? Should I do something else ?
I'm using android.support.v7.widget.Toolbar as Toolbar.

Please make changes as shown below to app:actionViewClass and app:showAsAction and it should work for you.
<item
android:id="#+id/menuShowDue"
android:checkable="true"
android:title="#string/string_due"
app:actionViewClass="android.widget.CheckBox"
app:showAsAction="ifRoom|withText" />
Also make the relevant changes to onCreateOptionsMenu(). Sample text pasted below;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
CheckBox checkBox = (CheckBox) menu.findItem(R.id.menuShowDue).getActionView();
checkBox.setText("Sample Text");
return true;
}

You can set the title at OnCreateOptionsMenu. Use MenuItem should be sufficient.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem checkBoxMenuItem = menu.findItem(R.id.menuShowDue);
checkBoxMenuItem.setTitle("Sample Text");
return true;
}
In OptionsItemSelected, check if checkbox selected or not:
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuShowDue:
if(item.isChecked())
{
item.setChecked(false);
}else{
item.setChecked(true);
}
break;
}

Related

Android Studio ShareActionProvider

I added a ShareActionProvider menu item to my add, but when I press the "Share" button, the onOptionsItemSelected function doesn't capture the event, and I don't succeed to capture the changes in the text (the shareContent is initialized with the onCreate data through the onCreateOptionsMenu function).
I found in old posts that in older versions (support.v7), the "MenuItemCompat" needed to be used, but it's not recognized in android.widget.ShareActionProvider class.
This is part of my code:
menu.xml:
<item
android:id="#+id/action_share"
android:title="#string/action_title_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="always" />
mainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
switch (item.getItemId() )
{
case R.id.action_share:
//share note content
noButtonWasPressed = false;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
setShareIntent(createShareIntent());
return true;
Any suggestions?

Menu items keeps on adding on every menu button press

I have added two menu item . Both of them works good but whenever i press menu button new menu items appear beside the old one . Like below u can see
below is my menu item xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:title="Search"
android:icon="#drawable/search_white_24dp"
android:id="#+id/searchmenu"
app:showAsAction="always">
</item>
<item
android:icon="#drawable/settings_white_24dp"
android:title="Setting"
android:id="#+id/settingmenu"
app:showAsAction="always"
/>
</menu>
and here is my code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchmenu:
editText = DuwaManager.openSearchBox(this,
getSupportActionBar(), "DuwaListView");
break;
case R.id.settingmenu:
break;
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
please help where i am wrong ?
Sorry this will get to long but , I have second Question
2) My second question i have another menu. xml in which there is share icon in place of search icon , But when i am trying to show on action bar with same code menu item does not show on action bar . below is my another menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:title="Share"
android:icon="#drawable/share_white"
android:id="#+id/share_tarika"
app:showAsAction="always"
/>
<item android:title="Setting"
android:icon="#drawable/settings_white_24dp"
android:id="#id/settingmenu"
app:showAsAction="always"/>
</menu>
and its code
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tarika_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_tarika:
break;
case R.id.settingmenu:
break;
}
return super.onOptionsItemSelected(item);
}
Try this
#Override
public void onCreateOptionsMenu(Menu menu ) {
menu.clear();// use menu.clear
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu."your current activity name ", menu);
return true;
}
use menu.clear()
The best to do is as this :
#Override
public void onCreateOptionsMenu(Menu menu ) {
getMenuInflater().inflate(R.menu."your current activity name ", menu);
return true;
}
if you have just one menu. Then set menu from Activity page and return true then no need to clear menu. and no duplicate menu will appear.
But if you use two fragment and have to change activity menu as per fragment then only need to clear menu and reset it with new one.
And if you are using onCreateOptionsMenu(Menu menu) in fragment then remove them.

Is any option to add TICK MARK on the right side of the toolbar?

By default android have an option to enable BACK ARROW on the left of the toolbar using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
same like i need TICK MARK on the right of the toolbar, is there any method to enable TICK MARK in android by default.
First download the "done" icon as png from the materials icon library
Copy the icons to your projects drawable folder.
Then create a file in res/menu called menu_example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_done_white_24dp"
android:title="#string/action_done"
app:showAsAction="always" />
</menu>
Then add the following code to your Activity.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
To have a onClick listener for the tick you can implement this:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast toast = Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT);
toast.show();
return true;
}

Check checkboxes nullPointerException

I'm confused as to how to check checkboxes that are in the activities' menu file, not its layout file. First I had the code below but with findViewById(R.id.uniformScale); rather than findViewById(R.menu.dressing_room).findViewById(R.id.uniformScale); but that threw a nullPointerException so I added the findViewById(R.menu.dressing_room) but that causes the same error. I know I have to reference the checkbox in the menu file because it's not in the layout file but I'm not sure how. Or I add it to the layout file, but i'm not sure how to do that either. Note: they might be radioButtons because checkableBehavior="single" but I've tried changing type to RadioButton and get the same exceptions thrown. Thanks.
Within the menu file for the activity:
<item
android:title="Sticker Settings"
android:id="#+id/action_cancel1"
android:icon="#drawable/ic_action_settings">
<menu>
<group android:checkableBehavior="single"
>
<item android:id="#+id/uniformScale"
android:title="Change Size"
android:checked="true" />
<item android:id="#+id/rotation"
android:title="Rotate"
android:checked="false" />
</group>
</menu>
</item>
Within the activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainActivity, menu);
checkCheckboxes();
return true;
}
private void checkCheckboxes(){
CheckBox checkBox1 = (CheckBox) findViewById(R.menu.dressing_room).findViewById(R.id.uniformScale);
if (checkBox1.isChecked()) {
photoSorter.setRotate(1);
photoSorter.setAnisotropic(2);
}
else{
checkBox1.setChecked(true);
photoSorter.setRotate(2);
photoSorter.setAnisotropic(1);
}
}
EDIT Trying blackbelts answer:
private void checkCheckboxes(Menu menu){
MenuItem checkBox1 = (MenuItem) menu.getItem(R.id.uniformScale);
if (checkBox1.isChecked()) {
photoSorter.setRotate(1);
photoSorter.setAnisotropic(2);
}
else{
//checkBox1.setChecked(true);
photoSorter.setRotate(2);
photoSorter.setAnisotropic(1);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainActivity, menu);
checkCheckboxes(menu);
return true;
}
Change
private void checkCheckboxes()
to
private void checkCheckboxes(Menu menu)
and use menu.findItem(R.id.uniformScale) to retrieve the element

android - Action bar sherlock add unnecessary button

i use action bar sherlock and i set custom layout in the menu:
<item
android:id="#+id/action_menu"
android:actionLayout="#layout/menu_item"
android:icon="#drawable/b_menu"
android:orderInCategory="1"
android:showAsAction="always"/>
<item
android:id="#+id/action_audio"
android:actionLayout="#layout/audio_item"
android:icon="#drawable/b_audio"
android:orderInCategory="2"
android:showAsAction="always"/>
<item
android:id="#+id/action_mySong_details"
android:actionLayout="#layout/my_song_properties"
android:icon="#drawable/b_mysong"
android:orderInCategory="2"
android:showAsAction="always"/>
to control those items i use:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Create an option menu from res/menu/items.xml */
getSherlock().getMenuInflater().inflate(R.menu.main_tab, menu);
/** Get the action view of the menu item whose id is search */
View v = (View) menu.findItem(R.id.action_menu).getActionView();
Button b = (Button) v.findViewById(R.id.btnMenu);
return super.onCreateOptionsMenu(menu);
}
for some reason the action bar add unnecessary item, does anybody knows how this case happene?
You are using Action bar sherlock. So use SupportMenuInflater.
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_menu:
// Intent i=new Intent(A.this,B.class);
//startActivity(i);
return true;
}
return false;
}
I found the mistake: i remove the android:orderInCategory from all the items and it's ok now, i this case i don't need this option.

Categories

Resources