Webview back button won't go back - android

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

Related

Android Scrollview + horizantal scrollview + zoomcontrols

i am using to learn zoomcontrols. i am now at the begining android.
when closer if i, It does not seem a part of the picture. codes below. please help me
i dont want pinc zoom.. i want simply button zoom. and i am sorry for my English :( i hope you are understand me..
xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/resim"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
and java
package com.example.kusem.ikiliscrollldenemeleri;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.ZoomControls;
public class MainActivity extends ActionBarActivity {
ImageView iv;
ZoomControls zoom;
ScrollView sv;
HorizontalScrollView hsv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zoom= (ZoomControls) findViewById(R.id.zoomControls);
iv= (ImageView) findViewById(R.id.resim);
// hsv= (HorizontalScrollView) findViewById(R.id.hsv);
iv.setImageResource(R.drawable.resim2);
zoom.setOnZoomInClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
float x = iv.getScaleX();
float y = iv.getScaleY();
iv.setScaleX((float)(x+1));
iv.setScaleY((float)(y+1));
}
});
zoom.setOnZoomOutClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
float x = iv.getScaleX();
float y = iv.getScaleY();
iv.setScaleX((float) (x-1));
iv.setScaleY((float) (y-1));
}
});
}
#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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thank you in advance for your help..

Android EditText not editable

I have a 2 activities with edittexts on them the first activity work fine but the second activity's EditText is focused but I can't edit it nor can I click the button on it.
Also everything works in the emulator but not when I install it on the device. (The keyboard popup also does not come up in the second activity where as it does in the first activity)
Below is the code for the1st and second activity
First Activity Class
package com.rohan.rohan_pc.sportappnew;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends ActionBarActivity {
EditText edtEmail, edtPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Initialize components
edtEmail = (EditText) findViewById(R.id.edtEmail);
edtPassword = (EditText) findViewById(R.id.edtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (edtEmail.getText().toString().length() > 0 && edtPassword.getText().toString().length() > 0) {
new GetAllCustomerTask().execute(new ConnecterClass());
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, 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);
}
// Connect to the DB
private class GetAllCustomerTask extends AsyncTask<ConnecterClass, Long, JSONArray>
{
#Override
protected JSONArray doInBackground(ConnecterClass... params) {
// Execute in background
return params[0].usrLogin(edtEmail.getText().toString(), edtPassword.getText().toString());
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
if (jsonArray != null) {
JSONObject json = null;
try {
json = jsonArray.getJSONObject(0);
if (edtEmail.getText().toString().equals(json.getString("email"))) {
if (edtPassword.getText().toString().equals(json.getString("pass"))) {
Toast toast = Toast.makeText(getApplicationContext(), "login successful", Toast.LENGTH_SHORT);
toast.show();
Intent intent = new Intent(LoginActivity.this, SearchActivity.class);
//Pass the school_id
intent.putExtra("SCHOOL_ID", json.getInt("school_id"));
startActivity(intent);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "password incorrect", Toast.LENGTH_SHORT);
toast.show();
}
} else {
Toast toast = Toast.makeText(getApplicationContext(), "failed to login ...", Toast.LENGTH_SHORT);
toast.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
//login(edtEmail.getText().toString(),""
/*
//Got to second screen if login is successful
Intent intent = new Intent(LoginActivity.this, AfterLogin.class);
//Pass the table name through
intent.putExtra("CLIENT_DB", clientDB);
startActivity(intent);
*/
}
}
}
}
Second Activity Class
> package com.rohan.rohan_pc.sportappnew;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class SearchActivity extends ActionBarActivity {
EditText edtFName, edtLName, edtIDNum;
ListView listViewSearch;
String sFName, sLName, sIDNum;
int school_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
edtFName = (EditText) findViewById(R.id.edtFirstName);
edtLName = (EditText) findViewById(R.id.edtLastName);
edtIDNum = (EditText) findViewById(R.id.edtIDNum);
Button btnSearch = (Button) findViewById(R.id.btnSearch);
listViewSearch = (ListView) this.findViewById(R.id.listViewSearch);
//Initialize the default values for the search criteria variables
sFName = "";
sLName = "";
sIDNum = "";
//Load school_id
school_id = getIntent().getExtras().getInt("SCHOOL_ID");
if(edtFName.requestFocus()) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); }
btnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edtFName.getText().toString().length() > 0)
{
sFName = edtFName.getText().toString().trim();
}
if(edtLName.getText().toString().length() > 0)
{
sLName = edtLName.getText().toString().trim();
}
if(edtIDNum.getText().toString().length() > 0)
{
sIDNum = edtIDNum.getText().toString().trim();
}
new SearchAllCustomers().execute(new ConnecterClass());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search, 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);
}
//Search in the background
public void setListViewArray(final JSONArray jsonArray)
{
this.listViewSearch.setAdapter(new Search_ListView_Adapter(jsonArray, this));
//Setup what happens when a user clicks on a searched item
this.listViewSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try{
JSONObject clickedItem = jsonArray.getJSONObject(position);
Intent askActivity = new Intent(getApplicationContext(), AskActivity.class);
askActivity.putExtra("IDNUM", clickedItem.getString("idnum"));
askActivity.putExtra("SCHOOL_ID", clickedItem.getInt("school_id"));
startActivity(askActivity);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
// Connect to the DB
private class SearchAllCustomers extends AsyncTask<ConnecterClass, Long, JSONArray>
{
#Override
protected JSONArray doInBackground(ConnecterClass... params) {
// Execute in background
return params[0].searchPlayers(school_id, sFName, sLName, sIDNum);
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
if (jsonArray != null) {
setListViewArray(jsonArray);
//login(edtEmail.getText().toString(),""
/*
//Got to second screen if login is successful
Intent intent = new Intent(LoginActivity.this, AfterLogin.class);
//Pass the table name through
intent.putExtra("CLIENT_DB", clientDB);
startActivity(intent);
*/
}
}
}
}
The XML - First Activity
<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: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=".LoginAcivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/edtEmail"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:hint="email ..."
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/edtPassword"
android:layout_below="#+id/edtEmail"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:hint="password ..."
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/btnLogin"
android:layout_below="#+id/edtPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp" />
</RelativeLayout>
The XML - Second Activity
<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: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="com.rohan.rohan_pc.sportsappv001.SearchActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Search"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name"
android:ems="10"
android:id="#+id/edtFirstName"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Surname"
android:ems="10"
android:id="#+id/edtLastName"
android:layout_below="#+id/edtFirstName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/edtIDNum"
android:hint="ID Number"
android:layout_below="#+id/edtLastName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/edtLastName"
android:layout_alignEnd="#+id/edtLastName" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:id="#+id/btnSearch"
android:layout_below="#+id/edtIDNum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="42dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="#+id/listViewSearch"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/edtIDNum"
android:layout_alignTop="#+id/edtFirstName"
android:clickable="true"
/>
I just figured to re-create the app. I dont think it is a wise decision to copy the raw files to another directory and use that.

Spinner Appearing But not Disappearing

I am trying to implement a progress bar, that appears when loading the page and disappears when the page is finished loading. I have added on finish parameter but it seems that it is not being recognized.
Code
package com.test;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.PushService;
public class MainActivity extends Activity implements OnClickListener {
private Button push;
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "onReceive invoked!", Toast.LENGTH_LONG).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setContentView(R.layout.activity_main);
PushService.setDefaultPushCallback(this, MainActivity.class);
WebView webView = (WebView) findViewById(R.id.webView1);;
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.teqez.com/xx/ ");
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setBuiltInZoomControls(false);
push = (Button)findViewById(R.id.senPushB);
push.setOnClickListener(this);
}
private class HelloWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url){
webview.loadUrl(url);
setProgressBarIndeterminateVisibility(true);
return true;
}
#Override
public void onPageFinished(WebView webview, String url){
super.onPageFinished(webview, url);
setProgressBarIndeterminateVisibility(false);
}
}
private class Callback extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (true);
}
}
#Override
public void onBackPressed()
{
WebView webView = (WebView) findViewById(R.id.webView1);
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
#Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
}
#Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, new IntentFilter(MyCustomReceiver.intentAction));
}
#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 void onClick(View v) {
JSONObject obj;
try {
obj = new JSONObject();
obj.put("alert", "hello!");
obj.put("action", MyCustomReceiver.intentAction);
obj.put("customdata","My message");
ParsePush push = new ParsePush();
ParseQuery query = ParseInstallation.getQuery();
// Push the notification to Android users
query.whereEqualTo("deviceType", "android");
push.setQuery(query);
push.setData(obj);
push.sendInBackground();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Layout
<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=".MainActivity" >
<Button
android:id="#+id/senPushB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_launcher"
android:text="Check For Updates" />
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:focusable="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
You do not invoke your custom private class
Change
webView.setWebViewClient(new WebViewClient());
to
webView.setWebViewClient(new HelloWebViewClient());

Save state of edittext

I am making a simple app.
Here are the codes:
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"
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" >
<Button
android:id="#+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/layout1"
android:text="#string/go" />
<EditText
android:id="#+id/urlField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/goButton"
android:background="#drawable/layout1"
android:ems="10" android:inputType="textUri"
android:hint="#string/enter_school_name">
<requestFocus />
</EditText>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/urlField" />
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/progressBar" />
Main Java:
package com.nextgenintl.plusportals;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
#SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
private WebView webView;
private EditText urlEditText;
private ProgressBar progress;
#Override
public void onBackPressed() {
if (webView.copyBackForwardList().getCurrentIndex() > 0) {
webView.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlEditText = (EditText) findViewById(R.id.urlField);
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new MyWebViewClient());
progress = (ProgressBar) findViewById(R.id.progressBar);
progress.setMax(100);
Button openUrl = (Button) findViewById(R.id.goButton);
openUrl.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
String url = urlEditText.getText().toString();
String urlSchool = "https://www.plusportals.com/"+url;
urlSchool = urlSchool.replace(" ", "");
if (validateUrl(url)) {
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(urlSchool);
MainActivity.this.progress.setProgress(0);
}
}
private boolean validateUrl(String url) {
return true;
}
});
}
private class MyWebViewClient extends WebChromeClient {
#Override
public void onProgressChanged(WebView view, int newProgress) {
MainActivity.this.setValue(newProgress);
super.onProgressChanged(view, newProgress);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void setValue(int progress) {
this.progress.setProgress(progress);
}
}
So, I want to save the user input - the school name, so you don't have to type it every single time. But I can't really figure it out - can you please help me??? Thanks if you post me the full java file.
You can use the following:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("clever_title", urlEditText.getText().toString());
editor.commit();
Then when you open your app the next time you can get:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
urlEditText.text(sp.getString("clever_title", null);

Displaying Menu items through webview in android application

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 ...

Categories

Resources