Want to add share button symbol on my app - android

Hi I have done following in my app
I am able to share my content but it comes from menuinflator not the symbol
Image shows that menuinflator has share button
If i click on share button ,I can share , But I want have the share symbol ,How to do it ?
I have read the following link
https://developer.android.com/training/sharing/shareaction.html
but symbol doesn't comes .
My code is below
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Menu xyz;
TextView t1 ;
private ShareActionProvider mShareActionProvider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.t1 = (TextView)findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_filexml, menu);
menu.getItem(0).setVisible(true);
this.xyz = menu;
// Return true to display menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share :
String code = " ";
code = this.t1.getText().toString();
Intent sendIntent = new Intent();
sendIntent.setAction("android.intent.action.SEND");
sendIntent.putExtra("android.intent.extra.TEXT", code);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu resource file (menu_filexml.xml)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
This code allows me to work as given in images but sharing symbol is not coming

you need to use custom view for your menu item. just create an layout file and name it "my_item_custom_view" and put your icon as 'ImageView' in it then assign the layout file like below:
<item
android:id="#+id/menu_share"
android:title="share"
app:showAsAction="always"
app:actionLayout="#layout/my_item_custom_view"/>

Related

Get the ID of a MenuItem in a toolbar

I have a "menu" resource "menu_test" with the following code:
<?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=".TestActivity" >
<item android:id="#+id/hideshow"
android:title="Body visbility"
android:orderInCategory="100"
android:icon="#drawable/showicon"
app:showAsAction="always" />
</menu>
In my activity.java I inflate the toolbar in the "onCreate" function and add a click listener:
private Toolbar toolbar;
...
toolbar = (Toolbar)findViewById(R.id.toolbarId);
toolbar.inflateMenu(R.menu.menu_test);
toolbar.setOnMenuItemClickListener(this);
I would like to get the ID of "hideshow" and change the icon:
testItem = (MenuItem) toolbar.findViewById(R.id.hideshow);
testItem.setIcon(R.drawable.hideicon);
However to app crashes when getting the ID in the first line. In the click listener function it works fine, since the ID is provided internally with the click:
public boolean onMenuItemClick(MenuItem item)
item.setIcon(R.drawable.hideicon);
So how can I change the icon within the onCreate function? ("findItem" does not work, since toolbar is not a menu)
I may be easier to add the menu items programmatically so you can maintain a reference to them:
public class MainActivity extends AppCompatActivity {
private static final int MENU_SETTINGS = Menu.FIRST;
MenuItem menuItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menuItem = menu.add(0, MENU_SETTINGS, 0, R.string.action_settings).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SETTINGS:
// DO SOMETHING
break;
}
return super.onOptionsItemSelected(item);
}
}
As the other answers and comments indicate, one needs a reference from onCreateOptionsMenu or onPrepareOptionsMenu.
In my case, this would require a lot of code rewrite, as this is an old project, where everything is based on using toolbars instead of menus.
However, I found an easy solution:
Instead of finding the ID and changing the icon, I created several instances of menu_test[1 .. n].xml with different icons and simply use inflateMenu(R.menu.menu_test[1 .. n]) depending on the icon I need.
If you write new code, use the solution from Abtin Gramian, so I mark that as the proper answer.

Android Sherlock Actionbar onOptionsItemSelected is not called

I m using Sherlock Actionbar to show share option in Action Bar of my application.
The Share option works fine.
I want to get the option which user selects from Share option, so that I can share
different text for facebook, messages, mail etc.
For this I am using onOptionsItemSelected function but this function
is never called.
Please help me to fix this or is there any workaround
to achieve this. Thanks..
public class MainActivity extends SherlockActivity {
private ShareActionProvider mShareActionProvider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getSupportMenuInflater().inflate(R.menu.items, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
Intent intent = getDefaultShareIntent();
if(intent!=null)
mShareActionProvider.setShareIntent(intent);
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Sample Content !!!");
return intent;
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
System.out.println("Testing...................");
return false;
}
}
Items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/share"
android:title="#string/share"
android:showAsAction="always"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
/>
</menu>
For this I am using onOptionsItemSelected function but this function is never called.
It is not supposed to be called. It is not called for any action provider.
I want to get the option which user selects from Share option, so that I can share different text for facebook, messages, mail etc.
That is not directly supported by ShareActionProvider.

Options Menu not displaying on ActionBar

I wanted to display Menu in ActionBar using ActionBarActivity.
I am getting options menu while clicking options button instead of displaying in the top of action bar.
I need the options menu at top instead of clicking the options menu from 2.3 onwards
My Code :
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle("mytest");
actionBar.setTitle("Testing");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_action, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_one:
Toast.makeText(this, "clicked search", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
My XML file :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_one"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="#string/action_settings"/>
<item
android:id="#+id/menu_item_two"
android:icon="#drawable/ic_launcher"
android:showAsAction="always"
android:title="#string/action_settings"/>
</menu>
Try it:
Create XML file on "../res/menu/menu_main.xml"
manu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:showAsAction="never"
android:icon="#android:drawable/ic_menu_settings"
android:title="#string/menu_settings"/>
</menu>
The option "never" on "showAsAction", important, if you put that the option always show on ActionBar.
Then now we must asociate this option on the MainActivity.java (class whatever you want).
MainActivity.java
public class MainActivity extends 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.activity_main, menu);
return true;
}
}
Now that we define the elements of our action bar is knowing how to respond to keystrokes made ​​by the user about them.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Log.i("ActionBar", "Settings!");;
//open Activity,Fragments or other action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If you have morer options on your menu only add item on XML menu_main.xml and on the principal class add other "case".
For Example:
MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
Log.i("ActionBar", "Save!");;
//open Activity,Fragments or other action
return true;
case R.id.menu_settings:
Log.i("ActionBar", "Settings!");;
//open Activity,Fragments or other action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_settings"
android:showAsAction="never"
android:icon="#android:drawable/ic_menu_settings"
android:title="#string/menu_settings"/>
<item
android:id="#+id/menu_save"
android:showAsAction="never"
android:icon="#android:drawable/ic_menu_save"
android:title="#string/menu_save"/>
</menu>
It is possible that this is because your device has an options button that would open the menu, so Android don't draw the launcher icon.
http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/
Try with return super.onCreateOptionsMenu(menu); instead of return true in your onCreateOptionsMenu method according to below android document...
http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems
As per above android document...
The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.
So, If you want ActionBar in android 2.3 then you have to use Android Support Library.
Edit:
Your menu XML would need to look like this:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
>
<item android:id="#+id/item_menu_ok"
android:icon="#drawable/ic_action_ok"
android:title="#string/ok"
yourapp:showAsAction="always"/>
<item android:id="#+id/item_menu_cancel"
android:icon="#drawable/ic_action_cancel"
android:title="#string/cancel"
yourapp:showAsAction="always"/>
</menu>
Replace yourapp with your app name or any namespace.
I'm not 100% sure, but it looks like your menu XML file should have a menu in it. In the menu, add the items. Like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_menu" >
<item...
<item...
</menu>
Perhaps this works.
There are also a few other things to remember in general. Android decides by itself what to show in the ActionBar (depending on what fits there and other parameters). What does not fit in the ActionBar goes into the overflow.
Do this in your oncreate()
ActionBar actionBar = getActionBar();
actionBar.show();
This should do what you want.
Action Bar
Ok, let's try another answer. If, for whatever reason, Android decides not to show the items in the action bar, then what your are seeing is indeed the correct behaviour. Items not in the action bar are shown in overflow.
One reason items don't show in the action bar is because they don't have a icon defined.
You do define an icon, as this,
android:icon="#drawable/ic_launcher"
and I suspect this is the problem. The launcher icon is of different size (larger) than the action bar items (smaller). Try defining the icon in correct size and see if it is displayed.
http://developer.android.com/design/style/iconography.html
Edit: There is an action bar icon pack to get you started so you can test to see if it works. Download it and use eclipse to import some icon..

Android - how do I add context menu with about and quit choices?

When using the Android Eclipse IDE, how do I add a menu that is triggered by the phone menu button such that it has to have an "About" choice that displays an "About my app" dialog and a "Quit" choice that exits the app?
To create a content menu with about a quit items, please add all of the following to your project after first removing any other attempts at an about menu that might potentially conflict with this code. Let Eclipse help you add the required imports or peruse the android documentation as you try to build this, and after everything's in place it should just work :-)
In /res create a folder menu containing a file main.xml with the following xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_quit"
android:orderInCategory="101"
android:showAsAction="never"
android:title="#string/action_quit"/>
</menu>
In MainActivity.java add the following after your onCreate is closed:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
AboutDialog about = new AboutDialog(this);
about.setTitle(“About “my app);
about.show();
return true;
case R.id.action_quit:
System.exit(0);
return true;
}
}
Add a new java file but extend Dialog instead of Activity, like shown:
public class AboutDialog extends Dialog {
public AboutDialog(Context context) {
super(context);
}
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.dialog_about);
}
}
Finally, add a new layout dialog_about.xml like so:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ScrollView>
Of course you’ll need to add another layout and content inside the ScrollView, and you may want other features in your menu and your quit item could use a confirmation (possibly). But this should get you started.

menu won't show up android

Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:
public class AppMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.appmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.hello:
sayHello();
return true;
case R.id.bye:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/hello"
android:title="Hello"
android:icon="#drawable/icon"/>
<item android:id="#+id/bye"
android:title="Bye" />
</menu>
Thanks !
This answer is in response to the comment discussion on the question.
You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.

Categories

Resources