Calling Custom Options menu when webview is running - android

I want to call my function onCreateOptionsMenu when the menu button is clicked when my webview is running. Right now when you click the Memnu button when the Webview is runing it displays some default menu not my custom menu.
How would I modify my code below to disable the default menu and display my custom menu? When the web view is not running it shows my custom menu when you click the menu button.
public class WebViewActivity extends Activity {
WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
final Activity mActivity = this;
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
// SETS DEFAULT MODE TO LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://Google.com");
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
mActivity.setTitle("Google.com");
mActivity.setProgress(progress * 100);
}
});
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do your menu stuff here
return true;
}
else
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}
}

You have to override the onKeyDown function, to intercept the Menu key. Otherwise the webview will consume it. Something like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// do your menu stuff here
return true;
}
else
return super.onKeyDown(keyCode, event);
}

Related

How to disable the home button in android 4.0+ versions

I tried following code but it is not getting to disable the home button above 4.0 versions in android
public class DHActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dh);
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dh, menu);
return true;
}
#Override
public void onAttachedToWindow()
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("TESTE", "BOTAO HOME");
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
}
}
In the lower versions of android you can catch the home button , in later versions for security reason Google not allowing to override home button press.

Set sharing intent using icon in action bar

I want to share the website that the user is viewing in the webview in my app, however any of the tutorials I have followed, it just isn't working. I can get the icon to load in the action bar but it doesn't click. I have had onclick listeners implemented and nothing so far. The app is only meant for ICS or higher so it doesn't need to be backwards compatible with older androids. The code I am putting up is before any share icon is implemented as I want to do it from scratch or to see if anyone has any ideas to what I can do
public class WebActivity extends Activity {
private WebView mWebView = null;
private EditText mInputUrl = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
setContentView(R.layout.activity_web_view);
Intent intent = getIntent();
String thesite = intent.getStringExtra(MainPage.EXTRA_MESSAGE);
mInputUrl = (EditText)findViewById(R.id.input_url);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String baseurl = "http://";
String url = baseurl + mInputUrl.getText().toString();
mWebView.loadUrl(url);
}
});
mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl(thesite);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setGeolocationEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
public void onProgressChanged(WebView view, int progress)
{
//Make the bar disappear after URL is loaded, and changes string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar disappear after URL is loaded
// Return the app name after finish loading
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
WebView myWebView = (WebView) findViewById(R.id.webview);
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Edit
I have used this code to try and get it working, however when I go to click the icon it doesn't do anything at all.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
return true;
}
#Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareURL();
}
return super.onOptionsItemSelected(item);
}
private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This!"));
}
I have found the answer. It was rather simple in the end.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
return true;
}
#Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_item_share:
shareURL();
}
if(item.getItemId() == R.id.menu_item_refresh){
mWebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
private void shareURL() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mWebView.getUrl());
startActivity(Intent.createChooser(shareIntent, "Share This Website!"));
}
}
This also allowed me to have a refresh button too.
I don't see any override for onCreateOptionsMenu(..), are you sure you have added your ActionItems? Take a look at http://developer.android.com/guide/topics/ui/actionbar.html.
Ali.

(Android) Is there a way to detect when text has been selected in a webview?

In API 11+ when long-pressing on text in a WebView the Contextual Action Bar (CAB) is shown. I don't want to override the existing menu items so implementing my own ActionMode.Callback isn't the correct answer. I just want to know when the default CAB is shown so that I know text selection has begun that way I can lock scrolling on the WebView. Any thoughts?
If you want to know when the default CAB is on screen, you can check for it's existence after an ACTION_UP:
public class MainActivity extends ActionBarActivity {
WebView mWebView;
boolean mSelected;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.wvExample);
mWebView.loadUrl("file:///android_asset/webpage.html");
mWebView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
View root = getWindow().getDecorView();
View cabButton = root.findViewById(R.id.action_mode_close_button);
if (cabButton != null && !mSelected) {
Toast.makeText(getApplicationContext(), "Text is now selected", Toast.LENGTH_SHORT).show();
mSelected = true;
} else {
mSelected = false;
Toast.makeText(getApplicationContext(), "No Text Selected", Toast.LENGTH_SHORT).show();
}
}
return false;
}
});
.....

Android Back Button With Webview

I am having an issue with the back button in my android app. I am trying to have it go back through the previous pages and if unable then to exit. I pick through the code on the developer website and nothing is working. I would appreciate any help.
Below is my activity page:
WebView webView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new myWebView());
webView.loadUrl("http://google.com");
webView.setInitialScale(1);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.diabetes_meal_planner, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.action_settings:
//exits app
finish();
break;
default:
break;
}
return true;
}
private class myWebView extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.startsWith("mailto:"))
{
String[] email = url.split(":");
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email[1]});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Send_Us_Your_Email");
Log.v("NOTICE", "Sending Email to" + email[1] + "with subject" + "Send Us Your Feedback");
startActivity(emailIntent);
}
view.loadUrl(url);
return true;
}
}
Dont do this onKeyDown. Override Activity.onBackPressed to do this..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView!=null && webView.canGoBack()) {
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
Try this at the end, works in my webview app

android openPanel menu is empty

I have a tabActivity with multiple activities in one tab.
The following code work on android 2.3 but it's not working on android 4.2
ActivityStack.java
public class ActivityStack extends ActivityGroup {
..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// // what is the current activity?
menu.add(0, 0, 0, "holder");
return true;
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// start a new
menu.clear();
// add some menu options
getLocalActivityManager().getCurrentActivity().onPrepareOptionsMenu(menu);
return super.onPrepareOptionsMenu(menu);
}
..
Activity1Tab1.java
here I have a button from where I am calling Activity2Tab1.java onClickListener
Intent acIntent = new Intent();
acIntent.setClass(getParent(),
Activity2Tab1.class);
ActivityStack activityStack = (ActivityStack) getParent();
activityStack.push("SecondActivity", acIntent);
Activity2Tab1.java
..
here I have multiple layouts...defined
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{ //add menus or inflate here
Log.d(TAG, "onPrepareOptionMenu");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
return true;
}
Need some help!!!
Neither in Activity1Tab1 or in Activity2Tab1 the override method onKeyUp() IS NEVER CALLED. The only called methods are from StackActivity. WHY?
After some research done and some thinking I manage to make this code work.
Instead of using onPrepareOptionMenu(menu) and onCreateOptionMenu(menu) I've override the following method in StackActivity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, keyCode+"");
getLocalActivityManager().getCurrentActivity().onKeyUp(keyCode, event);
return super.onKeyUp(keyCode, event);
}
and in Activity1Tab1 and Activity2Tab1 I had the method:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.d(TAG, ""+event.getAction());
if (keyCode == KeyEvent.KEYCODE_MENU) {
Log.d(TAG, "MENU_BUTTON_PRESSED");
if (!isMainMenuVisible) {
pushMainMenuUp();
} else {
pushMainMenuDown();
}
}
return super.onKeyUp(keyCode, event);
}

Categories

Resources