Displaying Menu items through webview in android application - android

I am new to Android application development and still learning. I have created a simple web app using webview and that is working well now I have created a menu in my app and trying to add an about and help and exit item on that. Now I would like them to open inside another webview or the existing webview that I have created. I am confused that should I create another method or a class or another way you prefer
I mean when I will choose about from menu then I will be redirected to a page in my asset folder called about.html and for help it will be help.html .
this is my MainActivity.java
package com.example.mywebapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mWebView = new WebView(this);
mWebView.loadUrl("file:///android_asset/index.html");
mWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
this.setContentView(mWebView);
}
// For Options Menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
// Then it should redirect to "file:///android_asset/about.html"
return true;
case R.id.help:
// Then it will be redirect to "file:///android_asset/help.html"
return true;
case R.id.exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
And my main.xml from layout folder
<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" />
</RelativeLayout>

Found a solution
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.exit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
and then creating a About.class with these code
package com.example.mywebapp;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class About extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
webView = (WebView) findViewById(R.id.webView);
//webView.getSettings().setJavaScriptEnabled(true);
//webView.loadUrl("http://www.google.com");
webView.loadUrl("file:///android_asset/about.html");
}
}
and finally in AndroidManifest
<activity
android:name=".About"
android:theme="#android:style/Theme.NoTitleBar" />
i hope this ans would help newbie like me ...

Related

Webview back button won't go back

Please Help
When you press the Back Button to get out of the application
I tried a lot of solutions, but did not succeed
I will put you code
MainActivity
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity{
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://www.alrofaiy.com/");
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
setProgress(progress * 100);
}
});
webview.setWebViewClient(new InsideWebViewClient());
}
#Override
public void onBackPressed() {
super.onBackPressed();
if(webview.canGoBack()){
webview.goBack();
}
}
private class InsideWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#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);
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.
switch (item.getItemId()){
case R.id.item1:
try {
Intent abb = new Intent(Intent.ACTION_SEND);
abb.setType("text/palin");
abb.putExtra(Intent.EXTRA_SUBJECT , "APP");
String aliwi = "/n /n/n";
aliwi = aliwi + "https:// /n/n";
abb.putExtra(Intent.EXTRA_TEXT, aliwi);
startActivity(Intent.createChooser(abb,""));
}
catch (Exception o)
{o.toString();}
return true;
case R.id.item2:
Intent AAA = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:info#alrofaiy.com?subhect= "));
startActivity(AAA);
return true;
case R.id.link1:
webview.loadUrl("http://www.alrofaiy.com/login_in");
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:padding="0dp">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusableInTouchMode="false"
android:padding="0dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/text_call"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:gravity="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/webView"
android:layout_alignEnd="#+id/webView"
android:autoLink="phone"
android:clickable="true"
android:linksClickable="false"
android:textColor="#fff"
android:background="#000"
android:paddingTop="2dp" />
</RelativeLayout>
What's the solution?
When you run the application and view the site does go out when you press the back button
#Override
public void onBackPressed() {
super.onBackPressed();
if(webview.canGoBack()){
webview.goBack();
}
}
Should be
#Override
public void onBackPressed() {
if(webview.canGoBack()){
webview.goBack();
} else {
super.onBackPressed();
}
}

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

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

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);
}
}

Categories

Resources