This is the write activity. In here I want to write a text and after clicking a button I want this text to appear in a new activity called read activity.
public class Write extends Activity implements OnClickListener
{
EditText text; TextView retrive1;
public static String filename="Mysharedstring" ;
SharedPreferences someData;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.write);
setupVariables();
someData = getSharedPreferences(filename, 0);
}
private void setupVariables()
{
Button sav= (Button) findViewById(R.id.save);
Button ret= (Button) findViewById(R.id.retrive);
text= (EditText) findViewById(R.id.txtText);
retrive1= (TextView) findViewById(R.id.textview);
ret.setOnClickListener(this);
sav.setOnClickListener(this);
}
public void onClick(View v)
{
String stringdata= text.getText().toString();
SharedPreferences.Editor editor = someData.edit();
editor.putString("sharedString", stringdata);
editor.commit();
}
}
I don't know what to write in the read activity.
public void onClick(View v)
{
//???
}
The most simple way that I can think of is this first Activity
Intent intent= new Intent(this, theOtherActivity.class);
intent.putExtra(Key, "Value");
startActivity(intent);
And on theOtherActivity
String receivedData=getIntent().getExtras().getKey(Key);
public void onClick(View v)
{
// get the shared string from the SharedPreference
SharedPreferences sp = getSharedPreferences("Mysharedstring",0);
String s = sp.getString("sharedString","Nothing found.");
// display the shared string
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
Sources for SharedPreferences:
How to save app settings?
How to keep information about an app in Android?
Shared Preferences | Android Developers
Related
I want to make a basic app in which I offer the user to add a trackers of various things like water etc. and then he can store how many glasses he has drank so far. However I want it to have saved the information even after the app closes. Please provide a solution.
This is what I've tried so far:-
public class MainActivity extends AppCompatActivity {
Button btnAddTracker;
//public static final String MY_PREFS_NAME = "MyCounter";
ImageView ivWater;
TextView tvWaterCount;
SharedPreferences pref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = PreferenceManager.getDefaultSharedPreferences(this);
editor = pref.edit();
if(pref.getBoolean("WaterStatus",false)){
ivWater.setVisibility(View.VISIBLE);
tvWaterCount.setVisibility(View.VISIBLE);
tvWaterCount.setText(pref.getInt("waterCount",0));
}
btnAddTracker=findViewById(R.id.btnAddTracker);
ivWater=findViewById(R.id.ivWater);
tvWaterCount=findViewById(R.id.tvWaterCount);
btnAddTracker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,TrackerListActivity.class);
startActivity(intent);
}
});
editor.putInt("waterCount", 8);
editor.apply();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(pref.getBoolean("WaterStatus",false)){
ivWater.setVisibility(View.VISIBLE);
tvWaterCount.setVisibility(View.VISIBLE);
tvWaterCount.setText(pref.getInt("waterCount",0));
}
}
}
}
public class TrackerListActivity extends AppCompatActivity {
Button btnAddWaterTracker;
ImageView ivReturn,ivWater;
TextView tvWaterStatus;
boolean WaterStatus;
SharedPreferences pref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracker_list);
ivReturn=findViewById(R.id.ivReturn);
btnAddWaterTracker=findViewById(R.id.btnAddWaterTracker);
ivWater=findViewById(R.id.ivWater);
tvWaterStatus=findViewById(R.id.tvWaterStatus);
pref = PreferenceManager.getDefaultSharedPreferences(this);
editor = pref.edit();
WaterStatus=pref.getBoolean("waterStatus",false);
ivReturn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent2=new Intent();
intent2.putExtra("waterStatus",WaterStatus);
setResult(RESULT_OK,intent2);
if(btnAddWaterTracker.getVisibility()==View.GONE) {
editor.putBoolean("waterStatus",true);
editor.apply();
}
TrackerListActivity.this.finish();
//Intent intent1=new Intent(TrackerListActivity.this,MainActivity.class);
//startActivity(intent1);
}
});
btnAddWaterTracker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
WaterStatus=true;
btnAddWaterTracker.setVisibility(View.GONE);
tvWaterStatus.setVisibility(View.VISIBLE);
//editor.putBoolean("waterStatus",true);
//editor.apply();
}
});
}
}
There are many ways of saving informations locally, you could save it on a file using the Json or XML format, you could also use SQLite which are supported by all recent smartphones.
Unfortunately you won't get anyone to code for you on this website, check this link
I've got 2 activities (well actually 4 but only 2 matter for now), the main activity and the font changing activity. The main activity sends text that the user has typed in to the font changing activity using an intent and startActivityForResult. Within the font changing activity the user can change the font of the text, as expected. What I'm trying to do is after the user has changed the font, send the text and the new font back to the main activity. But I'm not sure how to do this. I'm thinking of using some kind of bundle but I am stuck. If anyone could help me that would be greatly appreciated.
Here's my (relevant) Main Activity code:
public class MainActivity extends AppCompatActivity
{
RelativeLayout move_group;
ImageView view;
String backgroundImageName;
SeekBar seekSize;
TextView user_text;
Button button;
SeekBar tiltChange;
String temp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_text = (TextView) findViewById(R.id.user_text);
static final int REQUEST_FONT = 4;
public void FontChange(View view)
{
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}
//There's supposed to be an accompanying onActivityResult method as well
//but i haven't figured that part out yet
}
Here's my (relevant) Font Activity code:
public class Main4Activity extends AppCompatActivity
{
TextView user_font_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
user_font_text = (TextView) findViewById(R.id.user_font_text);
}
public void boldText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_bold);
user_font_text.setTypeface(custom_font);
}
public void italicText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_italic);
user_font_text.setTypeface(custom_font);
}
public void regularText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_regular);
user_font_text.setTypeface(custom_font);
}
public void thinText(View view)
{
Typeface custom_font = getResources().getFont(R.font.avenir_next_thin);
user_font_text.setTypeface(custom_font);
}
public void OK(View view)
{
//The intent and/or bundle would go here but I don't know what to do
}
}
public void FontChange(View view)
{
temp = user_text.getText().toString();
Intent fontIntent = new Intent(this, Main4Activity.class);
fontIntent.putExtra("Text", temp);
startActivityForResult(fontIntent, REQUEST_FONT);
}
this will fetch your string from textview to fontActivity.
Now in your FontActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
Intent intent = getIntent();
String text = intent.getStringExtra("temp");
user_font_text = (TextView) findViewById(R.id.user_font_text);
user_font_text.setText(text);
}
public void OK(View view)
{
Intent returnIntent = new Intent();
returnIntent.putExtra("result",
user_font_text.getText().toString());
returnIntent.putExtra("font",
ypur_font_type);
setResult(Activity.RESULT_OK, returnIntent);
}
Remaining part of setting font style can be done using SpannableStringBuilder. here you can check SpannableStringBuilder . Hope this helps.
I'm trying to display the name inputted by the user on a basic form I created using Explicit intent on a second activity on a TextView but when I click the button the app crashes with the following "Unfortunately, formIntent has stopped" but my code has no errors. I got this to work with a Toast message on new activity but with a TextView. Any help would be appreciated.
Here's my First Activity `public class MainActivity extends AppCompatActivity {
Button submit;
TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button)findViewById(R.id.btnSubmit);
name = (TextView)findViewById(R.id.editTextname);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//from sending Activity
String userName = name.getText().toString();
String value = "Thank you " + userName + "your request is being processed" ;
Intent sendIntent = new Intent(v.getContext(), Main2Activity.class);
sendIntent.putExtra("userName", value);
//Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null)
{
startActivity(sendIntent);
}
}
});
}
}
`
Here's My second Activity
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String getName = getIntent().getExtras().getString("userName");
final TextView inputMessage = (TextView)findViewById(R.id.display);
inputMessage.setText(getName);
}
}
Please change this line to your activity name
Intent sendIntent = new Intent(MainActivity.this, Main2Activity.class);
Im making a simple app, which counts the clicks of a button and saves the integer with a Shared Preferences. I tried it with the following code, but the app crashes all the time, if I try to open "Singleplayer"
public class Singleplayer extends AppCompatActivity {
private int sp1;
private int record;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singleplayer);
final Button button1 = (Button) findViewById(R.id.buttonspieler1);
final TextView lbl1 = (TextView)findViewById(R.id.lblspieler1);
final TextView lbl2 = (TextView)findViewById(R.id.lblrekord);
SharedPreferences data_record = getSharedPreferences("savegame", 0);
record = data_record.getInt("myKey1", 0);
lbl2.setText(String.valueOf(record));
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(final View v)
{
if(sp1< record) {
sp1++;
lbl1.setText(String.valueOf(sp1));
}
else if(sp1>= record)
{
sp1++;
record++;
lbl1.setText(String.valueOf(sp1));
lbl2.setText(String.valueOf(record));
}
}
});
}
#Override
protected void onStop() {
super.onStop();
SharedPreferences data_record = getSharedPreferences("savegame", 0);
SharedPreferences.Editor editor = data_record.edit();
editor.putString("myKey1", String.valueOf(record));
editor.commit();
}
}
you are using sharedPrefrence in your onStop() so you will face many issue here, i suggest to use it at the end of click method
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(final View v)
{
if(sp1< record) {
sp1++;
lbl1.setText(String.valueOf(sp1));
}
else if(sp1>= record)
{
sp1++;
record++;
lbl1.setText(String.valueOf(sp1));
lbl2.setText(String.valueOf(record));
}
SharedPreferences data_record = getSharedPreferences("savegame", 0);
SharedPreferences.Editor editor = data_record.edit();
editor.putString("myKey1", String.valueOf(record));
editor.commit();
}
});
so why the app crash, because the sharedPref dosen't carry any value it's null
please check this
I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.