I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#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);
}
}
activity_login.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:gravity="center_horizontal"
android:orientation="vertical"
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="com.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
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=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Its because haven't declared "client_subdomain" as a variable within scope of onCreate
why don't you try changing displayData to
public String displayData () {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
return sharedPref.getString("name", "");
}
and in onCreate of the Main Activity
...
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+displayData()+".domain.co.uk/texts");
...
Also, I don't see you ever calling "saveInfo" anywhere in your Login Activity. As well, you should probably change the function too. You aren't grabbing the text from the EditText correctly (whatever YourSchool) is
public void saveInfo() {
SharedPreferences sharedPref = getSharedPreferences("spfile", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", subdomain.getText().toString());
editor.commit();
}
And this will be what your onCreate should have,
protected void onCreate(Bundle savedInstanceState) {
...
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
saveInfo();
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
...
}
Related
I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.
Here's what I have so far;
LoginActivity.java
package com.example.app;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
public class LoginActivity extends Activity {
EditText subdomain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subdomain = (EditText) findViewById(R.id.subdomain);
Button btn=(Button)findViewById(R.id.sign_in_button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(LoginActivity.this,
MainActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
public void saveInfo (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("name", YourSchool.getText().toString());
editor.commit();
}
}
MainActivity.java
package com.example.app;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a
browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use remote resource
mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");
// Stop local links and redirects from opening in browser instead
of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
public void displayData (View view) {
SharedPreferences sharedPref = getSharedPreferences("spfile",
Activity.MODE_PRIVATE);
String client_subdomain = sharedPref.getString("name", "");
}
// Prevent the back-button from closing the app
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
#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);
}
}
activity_login.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:gravity="center_horizontal"
android:orientation="vertical"
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="com.example.app.LoginActivity">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/YourSchool"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Your school"
android:maxLines="1"
android:singleLine="true" />
<Button
android:id="#+id/sign_in_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="webView"
android:text="SIGN IN"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
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=".MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:label="#string/app_name"></activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain
I think it's probably something small that I've missed out but any help would be greatly appreciated.
Many thanks,
Sam
Store the variable using sharedPrefeence as below,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("your_string_key", yourStringValue);
editor.commit();
Then retrieve the value as,
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
String myStringValue = sp.getInt("your_string_key", -1);
Use the variable in the loadUrl as
mWebView.loadUrl("https://"+myStringValue+".mydomain.co.uk");
I'd seen this asked in many, many places and have tried to follow the instructions given to no avail. I dont know if the questions are old, i'm doing things incorrectly or if my android studio program isnt working. What i want to do is to only open a new activity when a button is clicked. I'm very new to developing android applications.
I've recently tried to follow the answer provided here: How do I get a button to open another activity in Android Studio?
OnClick on the button is called "goTutorials"
My original activity is called home (Not mainActivity)
The new one is called tutorials.
This is what i added from trying to follow the link above:
In home's java file:
btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(home.this, tutorials.class));
}
});
In manifest file:
<activity
android:name="tutorials"
android:label="#string/app_name">
</activity>
Method 1
Use the onclick attribute in XML (so that whenever you click the button, defined method will trigger)
Step 1. - go to the XML where you button is(activity_home) & add
<Button
............
android:onClick="gotoTutorial"/>
Step 2. - then go to the home.Java & add following
public void gotoTutorial(View v){
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
Method 2
Use the setOnClickListener
Step 1. -
//create the link to the button in the interface
btn_tutorial = (Button)findViewById(R.id.tutorial_button);
btn_tutorial.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorialPage = new Intent (this, tutorials.class);
startActivity(tutorialPage);
}
}
I have created a sample app please see the code below.
You need to specify information of all activities in Manifest file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nextech.startnewactivity">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TutorialsActivity"
android:label="#string/title_activity_tutorials"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
My Launcher activity is MainActivity.java
package com.nextech.startnewactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startTutorials = (Button)findViewById(R.id.startTutorials);
startTutorials.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tutorialsActivityIntent = new Intent(MainActivity.this,TutorialsActivity.class);
MainActivity.this.startActivity(tutorialsActivityIntent);
}
});
}
#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);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.MainActivity"
tools:showIn="#layout/activity_main">
<TextView android:id="#+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_centerHorizontal="true"
android:text="Hello World! This is main activity!" />
<Button android:id="#+id/startTutorials"
android:layout_below="#+id/welcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start Tutorials"/>
</RelativeLayout>
TutorialsActivity.java
package com.nextech.startnewactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class TutorialsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorials);
}
}
activity_tutorials.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.nextech.startnewactivity.TutorialsActivity"
tools:showIn="#layout/activity_tutorials">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is tutorial activity"
android:layout_centerInParent="true"/>
</RelativeLayout>
I hope it helps.
Your manifest file is not displaying, but make sure you add the activity in your manifest AndroidManifest.xml in the application field. Here's an example from Google: Link
<application ... >
...
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>
Otherwise, the method you're using should be correct. You create an intent to the class of the Activity you want to start.
Intent intent = new Intent(this, tutorials.class);
startActivity(intent);
Make sure you have the necessary methods such as onCreate(Bundle savedInstance) Overrided.
Note that in the sample above android:name="" is your activity's full path name to the class (without .class/.java) and label is going to be the action toolbar string you'll see. In the <meta-data/> section you can alter the android:value="" to the starting activity's name so that will need to change based on what you call it.
Finally, make sure it has an XML view to go with it, as it gets inflated in the onCreate(Bundle savedInstance) call.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_here);
}
inside your activity file write this
Button btn = (Button)findViewById(R.id.open_activity_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(home.this, tutorials.class));
}
});
XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
.Java class
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, YourActivityYouWantToOpen.class);
startActivity(intent);
}
I'm fresh for Android App. I done my first Webview app. Now want to add Loading Image(.jpg), before the website load. I don't know anything. Just explain clearly. Image may add in app or image from another source.
My activity_main.xml
<FrameLayout 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=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
AndroidManifest.xml is below,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.steptoinstall.steptoinstall" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
MainActivity.java is below,
package com.steptoinstall.steptoinstall;
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 MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://www.steptoinstall.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
}
#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();
return super.onOptionsItemSelected(item);
}
}
I think the easiest way to do it is to place an imageView over the webView and then when the site loaded, hide it.
First of all u must change your xml like this:
<FrameLayout 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">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/activity_main_imageview"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</FrameLayout>
and add this line to your main activity
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// hide your imageView
}
});
just let me know if u wanted more details.
Update 2:
In your MainActivity, after setContentView, add this:
final ImageView mImageView = (ImageView)findViewById(R.id.activity_main_imageview);
after that u can set image to this imageView in xml or in code:
in xml change your ImageView:
<ImageView
android:id="#+id/activity_main_imageview"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:src="#drawable/img" />
and your MainActivity should be this:
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://www.steptoinstall.com");
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
mImageView.setVisibility(View.GONE);
}
});
To add Splash screen before loading main activity. then add this
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.example.tabs.R;
public class Splash extends Activity implements Runnable
{
Thread mThread;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mThread = new Thread(this);
mThread.start();
}
#Override
public void run()
{
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}
}
and xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/splash" >
</LinearLayout>
I have an app with many activities using intent. I have a sharedPreferences method for saving values.
I have a welcome screen that I set to display when the app launches (onCreate is called). In the same activity, I delete the sharePreferences method so when I reopen the app the welcome screen will launch again.
The problem is, when I change to another activity and back again to the main activity and press the exit button. When I launch the app again, it does not show the welcome screen. I think, the main activity is still running so, onPause(), onStop() and onDestroy() are never called. When I stay in the same activity (main) and press exit, it will call onDestroy() (that contains the erase sharedPreferences method).
I didn't set the finish() method. If I set that, every moved activity will call onDestroy in Main activity so the welcome screen will launch every move to that activity.
My mainactivity.java
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class Main extends AppCompatActivity {
RelativeLayout PopupScreen, layoutAsli;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onDestroy(){
super.onDestroy();
SharedPreferences settings = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE);
settings.edit().remove("ditekan").apply();
}
#Override
public void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.main);
Button btn1 =(Button)findViewById(R.id.button1);
final Button btn2 =(Button)findViewById(R.id.button2);
PopupScreen = (RelativeLayout)findViewById(R.id.PopupScreen);
layoutAsli = (RelativeLayout)findViewById(R.id.layoutAsli);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(Main.this, Intent2.class);
startActivity(intent2);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
moveTaskToBack(true);
finish();
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Isi dewek cuk!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
EditText tvText = (EditText) findViewById(R.id.editText1);
SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE);
if (prefs.contains("text")){
tvText .setText(prefs.getString("text", ""));
}
SharedPreferences prefs2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE);
if (prefs2.contains("ditekan")){
PopupScreen.setVisibility(View.INVISIBLE);
layoutAsli.setVisibility(View.VISIBLE);
}
}
public void dismisWelcomeMessageBox(View view) {
PopupScreen.setVisibility(View.INVISIBLE);
layoutAsli.setVisibility(View.VISIBLE);
}
#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);
}
#Override
public void onPause() {
super.onPause();
EditText tvText = (EditText) findViewById(R.id.editText1);
Button btWelcome = (Button) findViewById(R.id.button_welcome);
SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit();
SharedPreferences.Editor prefEditor2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE).edit();
prefEditor.putString("text", tvText.getText().toString());
prefEditor2.putBoolean("ditekan", btWelcome.isPressed());
prefEditor.apply();
prefEditor2.apply();
}
}
My another activity code:
package com.bani.latihan;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
/**
* Created by Bani Burhanuddin on 21/02/2016.
*/
public class Intent2 extends AppCompatActivity {
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.intent);
Button btnnext = (Button) findViewById(R.id.button5);
Button btnhome = (Button) findViewById(R.id.button6);
Button btnback = (Button) findViewById(R.id.button7);
btnnext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Intent3.class));
finish();
}
});
btnhome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Main.class));
finish();
}
});
btnback.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent2.this, Main.class));
finish();
}
});
CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1);
final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView2.setVisibility(View.GONE);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
imageView2.setVisibility(View.VISIBLE);
} else {
imageView2.setVisibility(View.GONE);
}
}
});
SharedPreferences prefs = getSharedPreferences("Preferences2", Context.MODE_PRIVATE);
if (prefs.contains("text2")) {
toggle.setChecked(prefs.getBoolean("text2", true));
}
}
#Override
public void onPause() {
super.onPause();
CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1);
if (toggle.isChecked()) {
SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit();
editor.putBoolean("text2", true);
editor.apply();
} else {
SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit();
editor.putBoolean("text2", false);
editor.apply();
}
}
}
My Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="com.bani.latihan.splashscreen"
android:label="#string/app_name"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="com.bani.latihan.Main"
android:screenOrientation="sensor"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent2 activity -->
<activity
android:name=".Intent2"
android:screenOrientation="sensor"
android:noHistory="true"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent3 activity -->
<activity
android:name=".Intent3"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
<!-- Intent4 activity -->
<activity
android:name=".Intent4"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation|screenSize">
</activity>
</application>
</manifest>
My main layout
<RelativeLayout
android:id="#+id/PopupScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#123456"
android:orientation="vertical"
android:layout_margin="16dp"
android:padding="16dp"
android:visibility="visible">
<TextView
android:id="#+id/welcome_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:text="#string/welcome"
android:textColor="#ddd333"
android:textSize="28sp"
android:textStyle="bold" />
<TextView
android:id="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome_title"
android:text="#string/welcome_message"
android:textColor="#0dff00"
android:textSize="18sp" />
<Button
android:layout_width="wrap_content"
android:id="#+id/button_welcome"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom"
android:background="#3b978d"
android:onClick="dismisWelcomeMessageBox"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="#string/ok"
android:textColor="#fff" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layoutAsli"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/hello_world"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_below="#+id/hello_world"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:src="#drawable/imageview"
android:layout_gravity="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button1"
android:layout_marginTop="111dp"
android:layout_centerHorizontal="true"
android:layout_gravity="left|bottom" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"
android:id="#+id/button2"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:layout_gravity="right|bottom" />
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
I got ur problem. Currently u are using the MoveTaskToBack . It will not destroy the activity ,it just move back in ativity stack after that u finish
activity. So its not calling other callback methods. It always call onDestroy
Use either moveTasktoBack or finish. Dont use both, it will confusing.
I am new to android, Eclipse and Java.
I started these tutorials from here: http://developer.android.com/training/basics/firstapp/index.html
I did all the steps from the link above...my problem is when I run this app and press the 'Send' button...nothing happens, there are no errors in my Console or LogCat.
Here is my code I ended up with:
activity_main.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="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyFirstApp</string>
<string name="action_settings">Settings</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
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);
}
}
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#SuppressLint("NewApi")
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#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);
}
}
and activity_display_message.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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
Has anyone else ran into the same issue as me? Any help would be appreciated.
MyFirstApp Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
You need to call startActivity
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); // missing
}
Also make sure you have made an entry for the activities in manifest file.