This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Anyone able to see whats wrong with this code? Is going to be a ticker code so when a button is clicked 1 is added and then again 1 is going to change to 2 and so on and on basically its a clicker that when closed will remember the number it was on.
To be more clear these lines are coming up as errors:
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setTextView.setText("Count: " + mCount);
All of the code:
package com.example.counter;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// Private member field to keep track of the count
private static int mCount = 0;
private TextView countTextView;
private Button countButton;
public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;
/** ADD THIS METHOD **/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countTextView = (TextView) findViewById(R.id.TextViewCount);
countButton = (Button) findViewById(R.id.ButtonCount);
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setText("Count: " + mCount);
editor = settings.edit();
editor.putInt("mCount", mCount);
editor.commit();
}
});
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
mCount = settings.getInt("mCount", 0);
countTextView.setText("Count: " + mCount);
}
}
You have switched the logic between resume and pause.
Pause should persist your number.
Resume should read the old number.
change new OnClickListener() to new View.OnClickListener() in addition to what everyone suggesting.
Do not use .setTextView.setText(..) but simply use .setText (CharSequence text).
See the Following Running Code For your requirement
public class MainActivity extends Activity {
// Private member field to keep track of the count
private static int mCount = 0;
private TextView countTextView;
private Button countButton;
public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;
/** ADD THIS METHOD **/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countTextView = (TextView) findViewById(R.id.TextViewCount);
countButton = (Button) findViewById(R.id.ButtonCount);
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setText("Count: " + mCount);
editor = settings.edit();
editor.putInt("mCount", mCount);
editor.commit();
}
});
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
mCount = settings.getInt("mCount", 0);
countTextView.setText("Count: " + mCount);
}
}
Just Copy And Paste it. thats it.
import android.content.DialogInterface.OnClickListener;
should be changed to
import android.view.View.OnClickListener;
You're importing the wrong listener, so it's expecting a different method signature for onClick()
In addition to all the other things mentioned.
Related
I want the user to have the same radio button checked which he had
previously checked before the app was closed.
This is my source code
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.R;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
textView = findViewById(R.id.text_view_selected);
Button buttonApply = findViewById(R.id.button_apply);
buttonApply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int radioId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(radioId);
textView.setText("Your choice: " + radioButton.getText());
}
});
}
public void checkButton(View v) {
int radioId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(radioId);
Toast.makeText(this, "Selected Radio Button: " + radioButton.getText(),
Toast.LENGTH_SHORT).show();
}
}
I want the radio button states to be saved for next time the app is
used.
You should take a look at sharedPreferences : https://developer.android.com/training/data-storage/shared-preferences
Save your data :
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("myRadio", radioButton.getText());
editor.commit();
Retrieve data :
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String defaultValue = "";
String radioButtonText = sharedPref.getString("myRadio", defaultValue);
For now, you can do this :
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
TextView textView;
// Declare SharedPreferences as attributes
private SharedPreferences sharedPref;
private SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
textView = findViewById(R.id.text_view_selected);
// Retrieve SharedPreferences
sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
editor = sharedPref.edit();
Button buttonApply = findViewById(R.id.button_apply);
buttonApply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int radioId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(radioId);
textView.setText("Your choice: " + radioButton.getText());
}
});
}
#Override
protected void onResume() {
int radioIdChecked = -1;
radioIdChecked = sharedPref.getInt("myRadioChecked", radioIdChecked);
if (radioIdChecked == -1) {
// ERROR, don't check anything
Log.d("TAG", "error: don't check anything");
}
else {
RadioButton radioButton = radioGroup.findViewById(radioIdChecked);
if (radioButton != null) {
radioButton.setChecked(true);
}
}
}
#Override
protected void onPause() {
editor.putInt("myRadioChecked", radioGroup.getCheckedRadioButtonId());
editor.commit();
}
Best
This question already has answers here:
Read/Write String from/to a File in Android
(11 answers)
Closed 4 years ago.
In this app, I want to save the text that ends up on the TextView view and be able to see it the next time I launch the app. I'm relatively new to Android Studio. How can I do this?
My code:
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 org.w3c.dom.Text;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
Button submit;
EditText habbit;
EditText money;
TextView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.button2);
habbit = (EditText) findViewById(R.id.habbit1);
money = (EditText)findViewById(R.id.money1);
view = (TextView)findViewById(R.id.textView3);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText((habbit.getText().toString() + " for $" + money.getText().toString()) + "/day");
}
});
}}
You can use shared preferences to save the text in a file. And when app is open again you can retrieve the text from file and put in a textView.
public class MainActivity extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences=getSharedPreferences("File_name",MODE_PRIVATE);
editor=preferences.edit();
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
view.setText((habbit.getText().toString() + " for $" + money.getText().toString()) + "/day");
editor.putString("key",habbit.getText().toString());
editor.commit();
}
});
}
And then you can retrieve data as -
public class MainActivity extends AppCompatActivity {
SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences=getSharedPreferences("File_name",MODE_PRIVATE);
String data = preferences.getString("key", "");
view = (TextView)findViewById(R.id.textView3);
view.setText(data);
}
}
You can use shared preferences to store a simple string.
static final String MY_PREF = "MY_PREF";
static final String SAVE_TEXT = "SAVE_TEXT";
SharedPreferences sharedPreferences = context.getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
//To store string use this line
sharedPreferences.edit().putString(SAVE_TEXT, textview.getText().toString()).commit();
//To retrieve the same use this line
sharedPreferences.getString(SAVE_TEXT, "default_value");
I am creating a simple clicker program. I am clicking a pear picture and then it increments by 1 and displays that. I want to save this number so I they can click and then completely exit the app and when they return the same number they left off at is still there. This is my code but yet it still does not save that pears int when I restart the app.
CODE:
package pearclicker.pearclicker;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageButton imageButtonPear;
TextView showValue;
int pears;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showValue = (TextView) findViewById(R.id.countText);
}
public void PearIncrease(View v) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("PearCount", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("pearCount", pears);
pears++;
if (pears == 1) {
showValue.setText(pears + " pear");
editor.putInt("pearCount", pears);
editor.apply();
}
else {
showValue.setText(pears + " pears");
editor.putInt("pearCount", pears);
editor.apply();
}
}
}
Your code is doing fine when clicking the button to increase the value of your pear. but you're doing nothing in your onCreate() so you think that the SharedPreference is not storing the value of pear when you restart the app..
to do that you need some modification to your code.
public class MainActivity extends AppCompatActivity {
ImageButton imageButtonPear;
TextView showValue;
int pears = 0;
SharedPreferences pref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getApplicationContext().getSharedPreferences("PearCount", MODE_PRIVATE);
showValue = (TextView) findViewById(R.id.countText);
pears = pref.getInt("pearCount", 0); // This will get the value of your pearCount, It will return Zero if its empty or null.
showValue.setText(pears + " pears");
}
public void PearIncrease(View v) {
editor = pref.edit();
pears++;
showValue.setText(pears + " pears");
editor.putInt("pearCount", pears);
editor.apply();
}
}
I have no clue how to fix this, please help!
I am creating a counter app that shows numbers every time user press a button. It starts from 0 and to a maximum of 9999. My problem is that after the user closes the app it again starts from 0 but what I want is display the last number the user pressed instead of 0 when user open app again. I tried many shared preference codes but none of them worked. Here is my code:
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int x;
Context context = this;
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView number=(TextView) findViewById(R.id.numbers);
ImageButton btn_count=(ImageButton) findViewById(R.id.btn_countn);
ImageButton btn_reset=(ImageButton) findViewById(R.id.btn_resetn);
mp = MediaPlayer.create(context, R.raw.button_press);
number.setText(String.format("%04d", x)+"");
btn_count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.button_press);
} mp.start();
} catch(Exception e) { e.printStackTrace(); }
x = x +1;
number.setText(String.format("%04d", x)+"");
}
});
btn_reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = MediaPlayer.create(context, R.raw.button_press);
} mp.start();
} catch(Exception e) { e.printStackTrace(); }
x=0;
number.setText(String.format("%04d", x)+"");
}
});
}
}
You will need to use the Android Preferences for this:
How?
define when the app is getting closed and then call a method where you can write the actual value of the counter
Example
SharedPreferences buttonCounterValue = getSharedPreferences("counter", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = buttonCounterValue.edit();
editor.putInt("myCounter", i); //
editor.commit();
and when the app is open again you will need to call another method and read it back...
Example:
SharedPreferences buttonCounterValue = getPreferences(Activity.MODE_PRIVATE);
int storedCounter = buttonCounterValue.getInt("myCounter", 0);
so validate what you store and check if the preference you get is 0, that means nothing was stored before, so you can beging the game from 0...
Using SharedPreferences you can do something like this:
public class MainActivity extends AppCompatActivity {
private int x;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
x = prefs.getInt("count", 0);
final TextView number=(TextView) findViewById(R.id.numbers);
ImageButton btn_count=(ImageButton) findViewById(R.id.btn_countn);
ImageButton btn_reset=(ImageButton) findViewById(R.id.btn_resetn);
number.setText(String.valueOf(x));
btn_count.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
x++;
prefs.edit().putInt("count", x).apply();
number.setText(String.valueOf(x));
}
});
btn_reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
x=0;
prefs.edit().putInt("count", x).apply();
number.setText(String.valueOf(x));
}
});
}
}
Ive got this button code as shown below but it only have one button and one textview my project require meant is that there needs to be two or more buttons that will count into separate systems but use the same code. Ill include the main code below but any help adding more functionality into the system is much loved!
main code
package com.example.counter;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
// Private member field to keep track of the count
private static int mCount = 0;
private TextView countTextView;
private Button countButton;
public static final String PREFS_NAME = "com.example.myApp.mCount";
private SharedPreferences settings = null;
private SharedPreferences.Editor editor = null;
/** ADD THIS METHOD **/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countTextView = (TextView) findViewById(R.id.TextViewCount);
countButton = (Button) findViewById(R.id.ButtonCount);
countButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setText("Count: " + mCount);
editor = settings.edit();
editor.putInt("mCount", mCount);
editor.commit();
}
});
settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onResume() {
super.onResume();
mCount = settings.getInt("mCount", 0);
countTextView.setText("Count: " + mCount);
}
}
Create 3 buttons in your xml, lets assume their ids are ButtonCount, ButtonCount2 and ButtonCount3 as well as the countButton2 and countButton3 being declared as well. Then initialize them as follows:
countButton = (Button) findViewById(R.id.ButtonCount);
countButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setText("Count: " + mCount);
editor = settings.edit();
editor.putInt("mCount", mCount);
editor.commit();
}
});
countButton2 = (Button) findViewById(R.id.ButtonCount2);
countButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something here
}
});
countButton3 = (Button) findViewById(R.id.ButtonCount3);
countButton3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do something else here
}
});