I have code in which it has an editText box and a button. I want to save the text entered in the textbox and recall it the next time the app is opened.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText txtLink;
Button btnOpenLink;
String defaultLink;
String secondLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
defaultLink = "http://";
secondLink = ".whatver.com";
txtLink = (EditText) findViewById(R.id.editText);
btnOpenLink = (Button) findViewById(R.id.button);
btnOpenLink.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String server = txtLink.getText().toString();
if(!TextUtils.isEmpty(server)){
Intent intent=new Intent(MainActivity.this,webactivity.class);
intent.setData(Uri.parse(defaultLink+server+secondLink));
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "Please enter your server name.", Toast.LENGTH_LONG).show();
}
}
});
}
}
How can this be accomplished? I have tried numerous things that I have found through google but none have seemed to work right, I know it is something that I am doing most likely, I just can not make it work right.
Use the preferences....
Example
SharedPreferences spref = getSharedPreferences("your_prefs_name", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = spref.edit();
editor.putString("myTextViewValue", prefVal); //
editor.commit();
and somewhen latter when the app starts again read it back:
Example:
SharedPreferences preferences = getPreferences(Activity.MODE_PRIVATE);
String storedPreference = preferences.getStr("myTextViewValue", null);
so validate what you store and check if the preference you get is null, that means nothing was stored before...
Related
I am creating an Android app and I want to be able to give 100 points to user after successful login daily, I am using shared preferences to store user password and login, my login code is attached..
package com.hayroyal.tom.diabetial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//created by Folalu Timothy 02/02/2019
public class LoginActivityUser extends AppCompatActivity {
EditText editText;
Button button;
String password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SharedPreferences settings = getSharedPreferences("PREFS",0);
password = settings.getString("password", "");
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = editText.getText().toString();
if(text.equals(password)){
// login to the app
Intent intent = new Intent(getApplicationContext(), HomePage.class);
startActivity(intent);
finish();
}else {
Toast.makeText(LoginActivityUser.this, "Wrong password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Were I you, I would do the following,
Store a long variable named last_log_in and an int for points in SharedPreferences.
In successful log-in check the time and then determine if it's a new day.
You can check if it is a new day by using the Calendar class' methods.Documentation
//Take calendar object which represents now(this moment)
Calendar nowCal = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeInMillis());
//Take calendar object which represents last log in
SharedPreferences shared = getSharedPreferences("file_pref", MODE_PRIVATE);
Long value_long = shared.getLong("last_log_in",0);
Calendar lastLogInCal = Calendar.getInstance();
calendar.setTimeInMillis(value_long);
//Then write some code with if statements and make sure the conditions you want are met!
//If conditions are met, don't forget to update the long value in SharedPreferences.
Hi I'm currently trying to save a simple integer value from an EditText to sharedPreferences so that I can access it within any activity. I've tried following the google tutorials but I was unable to make it work. I ended up getting the error message "Cannot resolve method 'getPreferences' ". I realize that there have been other threads about sharedPreferences but I cannot seem to make sense of them.
Also, if you can help me find a better way than my catch statement to avoid the NumberFormatException, please let me know. Thanks again!Any help is much appreciated!
Java
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import static com.managergmail.time.finite.finitemanager02.R.id.textViewTest;
public class ExamPrepHome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam_prep_home);
Button buttonSaveNumberOfExams = (Button) findViewById(R.id.buttonSaveNumberOfExams);
try{
final EditText numberOfExamsInput = (EditText) findViewById(R.id.numberOfExamsInput);
final int numberOfExamsValue = Integer.valueOf(numberOfExamsInput.getText().toString());
buttonSaveNumberOfExams.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_number_of_exams), numberOfExamsValue);
editor.commit();
}
});
}catch(NumberFormatException ex){
System.out.println("Value at TextView is not a valid integer");
}
}
}
Instead of
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
use SharedPreferences sharedPref = ExamPrepHome.this.getPreferences(Context.MODE_PRIVATE);
Reason for this is because inside the anonymous class, 'this' represents that inner class and not the Activity object. And you have to use number format exception to check to avoid crashes when you retrieve the object.
I am working on an application called Android Glossary. In my second activity I have created a TextView which has a link embedded in it. Everything seems fine; the link is highlighted in blue. The problem is that when I click on the link, my application crashes down. I don't know what is wrong. Please help.
Code in my second activity :
package com.mavenmaverick.androidglossary;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class CActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
Intent browser= new Intent(Intent.ACTION_VIEW, adress);
startActivity(browser);
}
});
}
see your code
you have enter url like
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
replace with this
Uri adress= Uri.parse("http://www.cyanogenmod.com");
problm is ; in Http; replace with http:
you should use setData()
String url = "http://www.cynogenmod.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browser = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cyanogenmod.com"));
startActivity(browser);
}
});
So I'm fairly new to Java coding and I'm trying to create a simple code to have an edit text value act as a url for an intent internet command. I'm using this in Eclipse and ADT. The following is my java code.
import android.widget.EditText;
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.content.Intent;
import android.net.Uri;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText UR_L=(EditText) findViewById(R.id.ur_l);
String sUR_L= new String(UR_L.getText().toString());
Intent brwsrintnt = new Intent(Intent.ACTION_VIEW, Uri.parse(sUR_L));
startActivity(brwsrintnt);
}
});
In my layout I have a 2 buttons and an edit box but the edit box reflects a missing input type error. I have no idea how to fix this and I've looked many places.
Thanks,
David
I think your problem might be that the EditText is never intialized until you have already hit the button, so when you go to get text from it, it doesn't know the user has already entered something. Try moving
EditText UR_L=(EditText) findViewById(R.id.ur_l);
to just before you set the OnClickListener and see if anything changes
I'm working on an app and would like to save the state of obe of my checkbox options, but when I try using the SharedPreferences class (first time using it) I get a nullpointer exception. I have been troubleshooting for a couple hours and cant find a solution. Could anyone look at this and tell me what's wrong? The full code is alot longer but this is the park giving me the null pointer exception. I know for a fact it has to do with SharedPreferences.
package us.mattmccoy.meanfind;
//import java.util.Arrays;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit();
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
//normal private data
final Context con1 = this;
int dividend;String dataFixed;boolean tb;
private CheckBox myCBox, acBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
}
//check box listeners
public void addListenerBoxes(){
//instantiate boxes
acBox = (CheckBox) findViewById(R.id.chex2);
acBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//is acBox checked?
if (((CheckBox) v).isChecked()) {
edit.putBoolean(autoclearKEY, true).commit();
}else{
edit.putBoolean(autoclearKEY, false).commit();
}
}
});
myCBox = (CheckBox) findViewById(R.id.chex);
}
error message:
http://pastebin.com/5P2Mfwik
Change this line
SharedPreferences preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
to
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
and put
preferences = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
in onCreate()
You are trying to use Context before establishing it with onCreate() so you are getting NPE on context. You will also want to move these lines
Editor edit = preferences.edit();
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
into onCreate() after you initialize preferences or you will get another NPE since preferences will be null until you initialize it. So it should be something like
public class MainActivity extends Activity {
//saved data stuff needed
SharedPreferences preferences; //declare them
Editor edit;
//private data using saved data
String autoclearKEY = "us.mattmccoy.meanfind.AUTOCLEAR";
boolean autoclear;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences preferences; = this.getSharedPreferences("us.mattmccoy.meanfind", Context.MODE_PRIVATE);
Editor edit = preferences.edit(); //initialize them
boolean autoclear = preferences.getBoolean(autoclearKEY, false);
addListenerBoxes();
if(autoclear == true){
acBox.setChecked(true);
}else{
acBox.setChecked(false);
}
If this doesn't solve your problem then please post full logcat