Android Webview: Why Doesn't Refresh Work In Options Menu? - android

I've been racking my brains over this for ages, but think I'm too close to the woods to see the trees. Can anyone tell me why item1 in the menu below doesn't work to refresh the webview?
(The exit button (item2) works just fine, if that matters.)
package com.my.project;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.view.View;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
public class MyProjectActivity extends Activity
{
final Activity activity = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
WebView WebView = (WebView) findViewById(R.id.webview);
WebView.getSettings().setJavaScriptEnabled(true);
WebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
WebView.loadUrl("http://www.mydomain.php");
}
// Create Menu Buttons
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
// Set Menu Button Actions
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
reload();
return true;
case R.id.item2:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

try this just minor changes:::
public class MyProjectActivity extends Activity
{
final Activity activity = this;
WebView webView;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadUrl("http://www.mydomain.php");
}
// Create Menu Buttons
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
// Set Menu Button Actions
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.item1:
webView.reload();
break;
case R.id.item2:
finish();
break
}
return super.onOptionsItemSelected(item);
}
}

Related

Error why working contextual action mode start ActionModeis not applicable for the arguments (ActionMode

I am following a tutorial 11. Exercise: Using the contextual action mode
But I am having this error :
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
Error: The method startActionMode(ActionMode.Callback) in the type Activity is not applicable for the arguments (ActionMode.Callback)
I checked this stackoverflow answer
they said to add
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
I had this error
The method getActivity() is undefined for the type Display
what I am doing wrong ? the below is my code.
package com.example.sqlfirst;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Display extends ActionBarActivity {
private final static String TAG = "MainActivity";
protected Object mActionMode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_main);
//have to use getSupportActionBar from android.support.v7.app
// ActionBar actionBar = getSupportActionBar();
//getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
View view = findViewById(R.id.gridview);
view.setOnLongClickListener(new View.OnLongClickListener() {
// called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// start the CAB using the ActionMode.Callback defined above
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
}
#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.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.ic_action_person:
Toast.makeText(this, "Create a new account please", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Register.class);
startActivity(intent);
return true;
case R.id.ic_action_search:
Toast.makeText(this, "Search for new images", Toast.LENGTH_SHORT).show();
Intent isearch= new Intent(this,Search.class);
startActivity(isearch);
return true;
case R.id.ic_action_picture:
Toast.makeText(this, "Search for new photos", Toast.LENGTH_SHORT).show();
Intent iphotos= new Intent(this,Display.class);
startActivity(iphotos);
return true;
}
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.activity_main_actions, menu);
return true;
}
// called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_action_picture:
Toast.makeText(Display.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
Your Display class is extending ActionBarActivity, that means that it´s an Activity so there´s no need to use getActivity(), you can directly make use of the methods like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* this method is available within your ActionBarActivity*/
startSupportActionMode(modeCallBack);
setContentView(R.layout.grid_main);
// The rest of your code comes here
}

Open pop up after clicking android menu item

I want to open pop up or dialog box after clicking on menu item. I have written the code but the dialog box is not opening. The menu has two items "about" and "help". Is there any other way to show pop up message.?
Here is my code:
package com.easy.convert;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class AndroidMenusActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Initiating Menu XML file (menu.xml)
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_help:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.aboutus);
dialog.setTitle("Help");
dialog.setCancelable(true);
dialog.show();
return true;
case R.id.menu_about:
Dialog dialog1 = new Dialog(this);
dialog1.setContentView(R.layout.aboutus);
dialog1.setTitle("About");
dialog1.setCancelable(true);
dialog1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

Browse multiple pages in WebView on menu selection

Goooood Evening all. I have a webview that loads a specific webpage. The user can then select Menu and then navigate to TWO other designated pages. The problem is that the second two pages will not load at all. I can't understand why and would like some assistance. Thank you.
public class News extends Activity{
WebView mgebview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
mgebview = (WebView) findViewById(R.id.webview);
mgebview.getSettings().setJavaScriptEnabled(true);
mgebview.loadUrl("http://goo.gl/rQp3tF");
mgebview.setWebViewClient(new WebViewClient());
} #Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.newschoices, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()){
case R.id.bookmark:
Intent addContact = new Intent(this, BookGoogle.class);
startActivity(addContact);
break;
case R.id.dlrnews:
//Intent dlr = new Intent (this, DlrNews.class);
//startActivity(dlr);
mgebview = (WebView) findViewById(R.id.webview);
mgebview.getSettings().setJavaScriptEnabled(true);
mgebview.loadUrl("www.racingblog.com");
mgebview.setWebViewClient(new WebViewClient());
break;
case R.id.gtplanetnews:
mgebview = (WebView) findViewById(R.id.webview);
mgebview.getSettings().setJavaScriptEnabled(true);
mgebview.loadUrl("www.gtplanet.new");
mgebview.setWebViewClient(new WebViewClient());
break;
}
return super.onOptionsItemSelected(item);
}
}
The reason it is not working is because you are not returning a boolean! Try using this code:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()){
case R.id.bookmark:
Intent addContact = new Intent(this, BookGoogle.class);
startActivity(addContact);
return true;
break;
case R.id.dlrnews:
mgebview.loadUrl("www.racingblog.com");
return true;
break;
case R.id.gtplanetnews:
mgebview.loadUrl("www.gtplanet.new");z
return true;
break;
default:
return super.onOptionsItemSelected(item);
}
}
Returning true lets us "consume" it here.
As a reference: http://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected%28android.view.MenuItem%29
Sample Activity I just created, it works using the nexus 7:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("https://www.google.com/");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_settings:
myWebView.loadUrl("http://stackoverflow.com/questions/19508436/browse-multiple-pages-in-webview-on-menu-selection/19508494#19508494");
return true;
}
return super.onOptionsItemSelected(item);
}
}
Manifest make sure you add: <uses-permission android:name="android.permission.INTERNET"></uses-permission>
R.layout.activity_main :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>

Selecting Menu Items has no effect

I'm creating an app for Android and I wanted to make items in the context menu.
This wasn't a Problem and they were shown. But when I click on them, nothing happens.
I configured the things I needed to configure, but I really am not able to find the Problem. Do you see something? Here my complete code of the Main-Java-Code.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.loadUrl("ABC");}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem about) {
//respond to menu item selection
switch (R.menu.optionsmenu) {
case R.id.about:
startActivity(new Intent(this, SecondActivity.class));
return true;
case R.id.download:
startActivity(new Intent(this, DownloadActivity.class));
return true;
case R.id.impressum:
startActivity(new Intent(this, ImpressumActivity.class));
case R.id.license:
startActivity(new Intent(this, LicenseActivity.class));
}
return false;
}
I want them to Show the Activitys, but nothing happens.
Thanks for your help
Thanks to Phil, the item selection is now working. Here my other codes, the app breaks down every time I select the others.
Here is LicenseActivity:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
/**
* Created by Florent on 16.08.13.
*/
public class LicenseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView = (WebView) findViewById(R.id.licenseview);
myWebView.loadUrl("URL");
}
}
Second Activity is only a design activity, the other activitys are the sames as the License Activity.
I see your misstake. You need to get the menuitems Id inside the switch using MenuItem.getItemId(), and return return super.onOptionsItemSelected();:
public boolean onOptionsItemSelected(MenuItem about) {
//respond to menu item selection
switch (about.getItemId()) { // call this here
case R.id.about:
startActivity(new Intent(this, SecondActivity.class));
return true;
case R.id.download:
startActivity(new Intent(this, DownloadActivity.class));
return true;
case R.id.impressum:
startActivity(new Intent(this, ImpressumActivity.class));
case R.id.license:
startActivity(new Intent(this, LicenseActivity.class));
}
return super.onOptionsItemSelected(about); // return this instead of false
}
Also, do not forget to register your Activities inside your Manifest file.
And make sure you are calling setContentView(...) inside your Activity's onCreate() method.
public class LicenseActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whateveryourlayoutis); // DONT FORGET THIS
WebView myWebView = (WebView) findViewById(R.id.licenseview);
myWebView.loadUrl("URL");
}
}

Android menu button, to refresh page

What code would i add so that when the menu button refresh is pressed it will refresh the webview that is in my application.?
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView WV = (WebView) findViewById(R.id.webView1);
WV.getSettings().setJavaScriptEnabled(true);
WV.loadUrl("http://home.btconnect.com/MrJFisher/");
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
WebView.reload();
}
return super.onOptionsItemSelected(item);
}
};
beginner at this. so i do apologise.
EDIT:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class Main extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://home.btconnect.com/MrJFisher/");
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
mWebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== R.id.item1){
WV.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
See the WebView docs

Categories

Resources