Easy share action is not working in WebView Android app - android

I have been trying almost everything to get this share button to work. The button is placed right but it's not getting clicked and the share window is not showing.
Secondly, I want it to share Web URL of the page when clicked and I am also unable to understand how to do that.
Moreover, I do not want the button to be hidden in the Action settings. I want it to be displayed on the top bar, that's what's my main problem is.
MainActivity.java
package com.yoalfaaz.yoalfaaz;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.support.v4.view.MenuItemCompat;
public class MainActivity extends AppCompatActivity {
private WebView YoWeb;
private ShareActionProvider mShareActionProvider;
SwipeRefreshLayout swipe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
LoadWeb();
}
});
LoadWeb();
}
public void LoadWeb() {
YoWeb = (WebView)findViewById(R.id.webview);
WebSettings webSettings = YoWeb.getSettings();
webSettings.setJavaScriptEnabled(true);
YoWeb.loadUrl("http://www.yoalfaaz.com");
swipe.setRefreshing(true);
YoWeb.setWebViewClient(new WebViewClient() {
//onPageFinished Method
public void onPageFinished(WebView view, String url) {
//Hide the SwipeRefreshLayout
swipe.setRefreshing(false);
}
});
}
#Override
public void onBackPressed() {
if (YoWeb.canGoBack()) {
YoWeb.goBack();
} else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
private Intent createShareIntent() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
shareIntent.setType("text/plain");
startActivity(shareIntent);
return shareIntent;
}
}
menu_main.xml
<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="com.yoalfaaz.yoalfaaz.MainActivity">
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always" />
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
</menu>
First, the button is not clickable.
Second, how to share the current page URL?
These are the two queries which I am unable to solve.

Related

making a menu button invisible

i know that has been asked alot but i'm having a problem in my app . see i added buttons in menu.xml file in menu folder and it showing up however the banner ad cover it in bottom of the app and in the upper the button is not showing up at all
here is picture of my app :Ad covers menu view pic 1 also this
without ads pic 2 . plus i want to enlarge share & rate buttons size...
here is how i want menu button to be like : menu button pic 1 menu pic 2
here is my menu xml 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">
<item
android:id="#+id/privacy"
android:title="#string/privacy_policy"
android:layout_width="150sp"
android:textSize="150sp"
android:layout_height="150sp" />
<item
android:id="#+id/Disclaimer"
android:title="#string/disclaimer"
android:layout_width="150sp"
android:textSize="150sp"
android:layout_height="150sp" />
<item
android:id="#+id/More"
android:title="#string/more_apps"
android:layout_width="150sp"
android:textSize="150sp"
android:layout_height="150sp" />
</menu>
here is my java activity code :
import com.xxx.xxx.database.DataBaseHelper;
import com.xxx.xxx.R;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
InterstitialAd mInterstitialAd;
AdRequest adRequest;
Button start;
ProgressBar progressBar ;
private final int SPLASH_DISPLAY_LENGTH = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
start = (Button) findViewById(R.id.start);
DataBaseHelper.setmDatabase(this);
AppRater.setAPP_PNAME(this);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(SettingsApp.Interstitial);
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
requestNewInterstitial();
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
progressBar.setVisibility(View.GONE);
start.setVisibility(View.VISIBLE);
}
}, SPLASH_DISPLAY_LENGTH);
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,ListViewsItems.class);
//intent.putExtra("id",""+itemClicked.getDirection());
//startActivityForResult(intent, 1);
startActivity(intent);
if (mInterstitialAd.isLoaded()) mInterstitialAd.show();
finish();
//else Toast.makeText(Start.this, "nonLoaded", Toast.LENGTH_SHORT);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.privacy:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://xxx.xxx")));
return true;
case R.id.Disclaimer:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://xxx.xxx")));
return true;
case R.id.More:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=" + "xxx")));// ADD YOUR DEVELOPER NAME HERE USE + FOR SPACE
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
You are missing the toolbar in your activity.
Try adding it to the xml of your activity:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
... />
And then setting it as the toolbar:
setSupportActionBar(findViewById(R.id.toolbar))

ShareActionProvider not doing anything

I have been following the Android Development course on Udacity, and got to a lesson where we were to implement a ShareActionProvider. After attempting it myself, and then following what they did--I'm certain it's not working properly.
I'm putting the code in a Fragment; the correct item is present in the overflow menu, but when I click "Share", nothing happens. I noticed when I open the DetailsActivity, that the following appears in the logs:
17493-17493/com.dummy.sunshine.app W/MenuItemCompat﹕ setActionProvider: item does not implement SupportMenuItem; ignoring
I'm not sure what that means, but I couldn't find anything about it online. I would assume that clicking it is supposed to do something--however, they never go over what's supposed to happen after that lesson--so I'm a tad confused.
DetailActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
public class DetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
}
#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_detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
DetailFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A placeholder fragment containing a simple view.
*/
public class DetailFragment extends Fragment {
private static String mForecastStr;
private final String TAG = DetailActivity.class.getSimpleName();
public DetailFragment() {
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent(); //allows us to get data from the screen that sent us to this activity
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT); //we get the EXTRA_TEXT that the ForecastFragment sent us
((TextView) rootView.findViewById(R.id.detail_text))
.setText(mForecastStr);
}
return rootView;
}
private Intent shareDetailForecast() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr + "#SunshineApp");
return sendIntent;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_detail_fragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
mShareActionProvider.setShareIntent(shareDetailForecast());
MenuItemCompat.setActionProvider(menuItem, mShareActionProvider);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareDetailForecast());
} else {
Log.d(TAG, "mShareActionProvider is null...");
}
}
}
menu_detail_fragment.xml
<?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_share"
android:title="#string/action_share"
android:orderInCategory="5"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
menu_detail.xml
<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="com.nxt3.sunshine.app.DetailActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
This is what the app looks like on the DetailActivity page:
I believe your problem is that on your DetailActivity you are extending a FragmentActivity while you should be extending an AppCompatActivity (which is a subclass of the FragmentActivity), as you are using an action bar on your activity and using the support library.
so you should replace this:
public class DetailActivity extends FragmentActivity {
by this:
public class DetailActivity extends AppCompatActivity {
and import the support library:
import android.support.v7.app.AppCompatActivity;
I don't know if you already have it set, but you also need to set your AppTheme to be Theme.AppCompat or something similar on your AndroidManifest.xml. Something like below:
</manifest>
<application
...
android:theme=""Theme.AppCompat.Light" >
...
</application>
</manifest>
I'm doing the same exercise in the Udacity and that worked for me, I hope it helps.
I assume you want the share view appears beside the toolbar menu!
If yes, here is the right solution for your issue :
DetailActivity.java
package com.example.android.sunshine.app;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new DetailFragment())
.commit();
}
}
#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_detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
DetailFragment.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.ShareActionProvider;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public static class DetailFragment extends Fragment {
private static final String LOG_TAG = DetailFragment.class.getSimpleName();
private static final String FORECAST_SHARE_HASHTAG = " #SunshineApp";
private String mForecastStr;
public DetailFragment() {
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
// The detail Activity called via intent. Inspect the intent for forecast data.
Intent intent = getActivity().getIntent();
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
((TextView) rootView.findViewById(R.id.detail_text))
.setText(mForecastStr);
}
return rootView;
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_detail_fragment, menu);
// Retrieve the share menu item
MenuItem menuItem = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
// Attach an intent to this ShareActionProvider. You can update this at any time,
// like when the user selects a new piece of data they might like to share.
if (mShareActionProvider != null ) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}
}
menu_detail_fragment.xml
<?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_share"
android:title="#string/action_share"
app:showAsAction="always"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
menu_detail.xml
<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="com.example.android.sunshine.app.DetailActivity" >
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>

Refresh button in action bar of android app

i m very new into android app. I m trying to execute an app that should contain a refresh button in the action bar. While compiling a code, the studio is prompting this error
"Error:(50, 20) error: non-static method reload() cannot be referenced from a static context"
I have written this uptil now
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Menu;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
mWebView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://www.example.com");
setContentView(mWebView);
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.action_refresh){
WebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
menu_main.xml
<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=".MainActivity">
<item android:id="#+id/action_settings" android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item
android:id="#+id/action_refresh"
android:title="#string/refresh_button"
android:icon="#drawable/ic_action_refresh"
android:orderInCategory="1"
app:showAsAction="never"
/>
</menu>
Change WebView.reload() to mWebView.reload()
In the former case, you are calling reload() on the WebView class, whereas in the latter you are telling your specific instance of WebView to reload.
This SO question may clarify things a bit more.

Failing to Display Search Icon In Action Bar

I'm trying to display the Search Icon in the top right of my Actionbar.
See Code below for the PublicVideos.java that extends an ActionBarActivity
The getoverflow manages to display the overflow menu, but it does not display the search icon. I have added the different sizes search icons in their respective drawables folders.
package faith.faithconnect;
import java.lang.reflect.Field;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
public class PublicVideosActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_public_videos);
centreLogo();
getOverflowMenu();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.faithmenu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return super.onCreateOptionsMenu(menu);
}
private void getOverflowMenu() {
// TODO Auto-generated method stub
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) {
e.printStackTrace();
}
}
private void centreLogo() {
// TODO Auto-generated method stub
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#F7CE04")));
// getOverflowMenu();
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setCustomView(R.layout.public_videos_view);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent backhome = new Intent(this, MainActivity.class);
startActivity(backhome);
}
// public void mingleSwipe(View view)
// {
// Intent mingleintent = new Intent(getApplicationContext(),
// MingleActivity.class );
// startActivity(mingleintent);
//
// }
public void ratingSelected(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
// Try Google play
intent.setData(Uri
.parse("market://details?id=com.cubeactive.qnotelistfree"));
startActivity(intent);
}
public void shareSelected(View view) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
shareIntent.putExtra(Intent.EXTRA_TEXT, "body here");
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
The faithmenu.xml below
<?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:title="search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" ></item>
</menu>
You should use name space app for showAsAction, so it should be:
<?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:title="search"
android:icon="#drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" > </item>
</menu>
When your item have android:showAsAction="ifRoom" If there's not enough room for the item in the action bar, it will appear in the action overflow.
You can also use "always" (android:showAsAction="always") to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way. Doing so can create layout problems on devices with a narrow screen. It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room.

Search view widget and Icon not showing up in the action bar

I am a beginner to Android. Trying to add a search a widget in the action bar.
Here is my menu file code ("main_acitivity.xml"):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="#string/action_search"
android:showAsAction="always"
android:actionViewClass="android.support.v7.widget.SearchView" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
My MainActivity class code is:
package com.example.fragment;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.fragment.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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.
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.main_activity,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void sendMessage(View view){
Intent intent= new Intent(this,DisplayMessageActivity.class);
EditText editText= (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
I have tried "android.widget.SearchView" as well but it is not even showing the icon. Could you please advise what I am doing wrong ?
you missed these:
xmlns:yourappname="http://schemas.android.com/apk/res-auto"
and
yourappname:showAsAction="always"
yourappname:actionViewClass="android.support.v7.widget.SearchView"/>

Categories

Resources