Can't get it to link to webpages. "Reset" and "About" should do. Using webview. Need for the links to open within the webview.
here's the MainActivity.java:
package com.test.apppackage;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
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); // enable javascript
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("https://www.google.com");
setContentView(mWebview );
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.menu_reset:
mWebview .loadUrl("https://www.bbc.co.uk");
setContentView(mWebview );
break;
}
return super.onOptionsItemSelected(item);
}
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
here's the activity main.xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_reset"
android:title="#string/menu_reset"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/menu_about"
android:title="#string/menu_about"
android:showAsAction="never"
/></menu>
here's the strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">App name</string>
<string name="menu_reset">Reset</string>
<string name="menu_about">About app</string>
You are confusing android.R and R
Your items are called #+id/menu_reset, which is created in your com.test.apppackage.R.java file.
But you are later comparing to android.R.id.menu_reset, which would be in android.R.id, but doesn't exist.
You need to use only your R.java in this case. Use com.test.apppackage.R.id.menu_reset.
In some cases, you can use android values, but anyway mixing won't work.
edit
apparently, android.R.menu_reset doesn't even exist
more edit
R is actually R.id, of course
Related
I am working on WebView. I don't know whether it's possible or not. I am opening an url inside of this view which is basically a login page. Now want to get the information whether user has successfully logged in or not. If yes then navigate to custom page (Android native) else keep on that page.
Thanks in advance.
We can achieve it via using this..
Create a function of javascript in the HTML page of Login Success/Failue HtmL page.
in this javascript function ,call your native Function by Using addJavascriptInterface:
package com.example.androidjsdemo;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
#JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
// Here you can Start WhatEver Activty launch///////
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webMy = (WebView) findViewById(R.id.webMy);
webMy.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings setting = webMy.getSettings();
setting.setJavaScriptEnabled(true);
//File file = new File("andoid");
webMy.loadUrl("file:///android_asset/mine.html");
}
#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) {
// 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
mine.html
<HTML>
<script>
function called()
{
// alert("hi");
Android.showToast("Hi Testing");
}
</script>
<Body>
Hi Sumit
<button onclick="called()">click me</button>
</Body>
</HTML>
activity_main.xml
<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"
tools:context="com.example.androidjsdemo.MainActivity" >
<WebView
android:id="#+id/webMy"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
When user complete successful singing he should redirect to other web page with url that indicate the login was successful. it can look something like http://www.yourwebsite.com/islogin?yes.
than all you need to do is to check in your app what the login status by verify that the url contain islogin and that it set to yes and do whatever you want.
here a small sample to help you begin:
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(isUserLoggedIn(url)){
doWork();
return true;
} else {
return false;
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
public void onPageFinished(WebView view, String url) {
}
});
For Example below is your login URL which you load in webview for login
String loginURL = "http://www.myurl.com/login";
Two possibility, when user click on login button
1) Success
Navigate to Other page as per web code. So we need to check it and open our native application screen.
2) Fail
Same login page but display error message according to user input.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains(loginURL)) {
// Login Fail
return false;
} else {
// Login Success
// Open Android Native screen
return true;
}
}
});
I'm trying to create the Action Bar by following the Android Studio tutorial. I am familiar with VBA, C++, and Python, but this is the first time I've ventured into an App, which uses java, xml, etc. in one. I say that because I think I've misplaced (or misnamed) something in my code, as when I try to run this, I get the error error: duplicate class: com.example.batman.myfirstapp.MyActivity.
I don't understand how it's a duplicate, I only have one "MyActivity.java", so am thinking that I can't do more than one public class MyActivity extends ... per .java file?
Here's what I have (please let me know if I need to include other code):
MyActivity.java
package com.example.batman.myfirstapp;
import android.content.Intent;
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;
import android.support.v7.app.*;
public class MyActivity extends ActionBarActivity{
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
public class MyActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";
/**
* Called when the user clicks the Send button
*/
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);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// If your minSdkVersion is 11 or higher, instead use:
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
main_activity_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myfirstapp="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
myfirstapp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
myfirstapp:showAsAction="never" />
</menu>
Again, I'm not sure what other files (strings.xml, DisplayMessageActivity.java, etc.) would be helpful to debug the above, so let me know what else I can include.
Thank you for any advice/help!
You're intuition is correct! You can only have one MyActivity class in your file, here's what it might look like:
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = ".com.batman.myfirstapp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private 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);
}
}
Your assumption is correct. In your .java file you have two classes named MyActivity. You can only have one.
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.
I am trying to make a simple android app that loads a webpage into a WebView.
package com.example.abhi.molinahealthcare;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Molina_HealthCare extends ActionBarActivity {
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView webview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_molina__health_care);
WebView myWebView = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
openURL();
}
private void openURL() {
webview.loadUrl("http://www.google.com");
webview.requestFocus();
}
#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_molina__health_care, 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ACTIVITY:-
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I was not able to open it. I have the INTERNET permission declared in the AndroidManifest.xml
You are not initializing webview object of WebView which you are using in openURL method. do it by initializing webview instead of myWebView in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_molina__health_care);
webview = (WebView) findViewById(R.id.webview);
...
Okay so here is my main activity class.
In my WebView there are various links (mostly PDF). Now when a user clicks a link then what I'd like to do is i would like to load the URL in a variable then pass this variable to Google doc viewer code inside the WebView only to show the PDF contents.
package com.aeonindustries.skfgi;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView=(WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setVerticalScrollBarEnabled(false);
webView.loadUrl("file:///android_asset/index.html");
}
#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;
}
}
So how am going to do that?
Thanks in advance.
Ok, here some code:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// insert here code to handle your goal
return true;
}
And somewhere in your Activity:
webView.setWebViewClient(new MyWebViewClient());