i have a serious problem in my application, it's about the status of activity, in every activity he has radio buttons check buttons and Edit text, i want the user when he returns to
finds all information that he selected are the same, i read a lot about the cycle of an application in android but i can't make it work, i would be very glad if someone help me.
public class ActivityDeux extends Activity {
final String PREFS_NAME = "MyPrefsFile";
final String ID = "id";
DBAdapter db = new DBAdapter(this);
public void ajouter(View v) {
db.open();
SharedPreferences prefs2 = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
long id = prefs2.getLong(ID, 0);
db.insertMENAGE2(id,a16,b17,rm_18_1ts,rm_18_2ts,c19,d20,e21);
db.close();
}
int a16;
int b17;
int c19;
int d20;
int e21;
private RadioGroup rm_16;
private RadioButton rm_16_1 ;
private RadioButton rm_16_2 ;
private RadioButton rm_16_3 ;
private RadioButton rm_16_4 ;
private RadioButton rm_16_5 ;
private RadioButton rm_16_6 ;
private RadioButton rm_16_7 ;
EditText rm_16_autre;
String rm_16_autrets = "";
private RadioGroup rm_17;
private RadioButton rm_17_1 ;
private RadioButton rm_17_2 ;
private RadioButton rm_17_3;
private RadioButton rm_17_4 ;
private RadioButton rm_17_5 ;
EditText rm_17_autre;
String rm_17_autrets = "";
EditText rm_18_1 ;
EditText rm_18_2 ;
String rm_18_1ts = "";
String rm_18_2ts = "";
private RadioGroup rm_19;
private RadioButton rm_19_1 ;
private RadioButton rm_19_2 ;
private RadioButton rm_19_3;
private RadioButton rm_19_4 ;
private RadioButton rm_19_5 ;
EditText rm_19_5_autre;
String rm_19_5_autrets = "";
private RadioGroup rm_20;
private RadioButton rm_20_1 ;
private RadioButton rm_20_2 ;
private RadioButton rm_20_3;
private RadioButton rm_20_4 ;
private RadioButton rm_20_5 ;
EditText rm_20_5_autre ;
String rm_20_5_autrets = "";
private RadioGroup rm_21;
private RadioButton rm_21_1 ;
private RadioButton rm_21_2 ;
private RadioButton rm_21_3;
private RadioButton rm_21_4 ;
private RadioButton rm_21_5 ;
EditText rm_21_5_autre ;
String rm_21_5_autrets = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deux);
rm_16_autre = (EditText)findViewById(R.id.rm_16_autre);
rm_17_autre = (EditText)findViewById(R.id.rm_17_autre);
rm_18_1 = (EditText)findViewById(R.id.rm_18_1);
rm_18_2 = (EditText)findViewById(R.id.rm_18_2);
rm_19_5_autre = (EditText)findViewById(R.id.rm_19_5_autre);
rm_20_5_autre = (EditText)findViewById(R.id.rm_20_5_autre);
rm_21_5_autre = (EditText)findViewById(R.id.rm_21_5_autre);
You can use the onSaveBundleInstance(Bundle outBundle) event, to save the state of your activity. And in onCreate of your activity, you'll get the same bundle..
It depends upon your requirement, suppose if you want to persisit user selection even when user closes app, and display next time when he returns then the fastest possible way will be
-- SharedPreferences, Write all the values user enters in radio boxes and edit boxes into shared preferences using a preferred key(I would suggest widget ids), and in on create initialize them by reading preference again using same keys. you have your solution this way.
Else, if you just want to preserve value for current session, the you can use either of following
-- Use global data holders to preserve values e.g Maps
-- Use onSaveInstanceState & onRestoreInstanceState callbacks from lifecycle, sample code below
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
outState.putBoolean("WIDGET ID as KEY", value); // Save you values from radio boxes here, whether they are checked or not
outState.putString("WIDGET ID as KEY", value); // Write values from Editboxes here
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
// Read the values from bundle, through keys you used to store, and set them to corresponding widgets
}
Look at the image above. The method onCreate is just called once and the onResume method is called when user returns to the activity (through the back button, for example). For this, you could have a variable that stores the box checked and stored when the user selects it. Then, in onResume method, you can "populate" the boxes.
Related
Am trying to get shared preferences working from here
I added the code from that thread, adapting it to my needs, it is showing no errors in the android studio until I compile it.
public class ChrStats extends Activity
{
private SharedPreferences savedFields
private Button saveButton;
private EditText editText;
private EditText editText2;
private EditText editText3;
private EditText editText4;
private EditText editText5;
private EditText editText6;
private EditText editText7;
// Add all your EditTexts...
// Upon creating your Activity, reload all the saved values.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
saveButton = (Button) findViewById (R.id.your_save_button_id);
editText = (EditText) findViewById (R.id.your_edit_text_1_id);
editText2 = (EditText) findViewById(R.id.your_edit_text_2_id);
editText3 = (EditText) findViewById(R.id.your_edit_text_3_id);
editText4 = (EditText) findViewById(R.id.your_edit_text_4_id);
editText5 = (EditText) findViewById(R.id.your_edit_text_5_id);
editText6 = (EditText) findViewById(R.id.your_edit_text_6_id);
editText7 = (EditText) findViewById(R.id.your_edit_text_7_id);
// Keep adding all your EditTexts the same way...
// "info" is just a tag name, use anything you like
savedFields = getSharedPreferences("info", MODE_PRIVATE);
// In case no value is already saved, use a Default Value
editText.setText(savednotes.getString("editText1", "Default Value 1"));
editText2.setText(savednotes.getString("editText2", "Default Value 2"));
editText3.setText(savednotes.getString("editText3", "Default Value 3"));
editText4.setText(savednotes.getString("editText4", "Default Value 4"));
editText5.setText(savednotes.getString("editText5", "Default Value 5"));
editText6.setText(savednotes.getString("editText6", "Default Value 6"));
editText7.setText(savednotes.getString("editText7", "Default Value 7"));
// Save the changes upon button click
saveButton.setOnClickListener(saveButtonListener);
}
public OnClickListener saveButtonListener = new OnClickListener () {
#Override
public void onClick(View v) {
SharedPreferences.Editor preferencesEditor = savedFields . edit ();
if (editText.getText().length() > 0) // Not empty
preferencesEditor.putString("editText", editText.getText());
if (editText2.getText().length() > 0) // Not empty
preferencesEditor.putString("editText2", editText2.getText());
if (editText3.getText().length() > 0) // Not empty
preferencesEditor.putString("editText3", editText3.getText());
if (editText4.getText().length() > 0) // Not empty
preferencesEditor.putString("editText4", editText4.getText());
if (editText5.getText().length() > 0) // Not empty
preferencesEditor.putString("editText5", editText5.getText());
if (editText6.getText().length() > 0) // Not empty
preferencesEditor.putString("editText6", editText6.getText());
if (editText7.getText().length() > 0) // Not empty
preferencesEditor.putString("editText7", editText7.getText());
// You can make a function so you woudn't have to repeat the same code for each EditText
// At the end, save (commit) all the changes
preferencesEditor.commit();
}
}
}
I expected it to throw up no errors, but I looked up the relevant references, and can't see many faults in it. Here is the screenshot of errors from the android studio.
So, i see 3 critical problems here:
no semicolon after private SharedPreferences savedFields
you have no declaration of savednotes. Maybe it should be savedFields instead?
Your java code placed in .kt file. idk how could this happend
i have a problem and i dont know how to fix it .I want to make a basic begginer calculator app.I want only one checkbox to be checkable at time but i cant figure out ,stil can check all .And how can i cast strings from edittext to doubl?.App is not finished yet .
Sorry for english.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top_fragment);
double result ;
EditText number_1 = (EditText)findViewById(R.id.number_1);
EditText number_2 = (EditText)findViewById(R.id.number_2);
CheckBox add_box = (CheckBox)findViewById(R.id.add_box);
CheckBox subtract_box = (CheckBox)findViewById(R.id.subtract_box);
CheckBox divide_box = (CheckBox)findViewById(R.id.divide_box);
CheckBox product_box = (CheckBox)findViewById(R.id.product_box);
final Button button = (Button)findViewById(R.id.button);
number_1.getText().toString();
number_2.getText().toString();
if(add_box.isChecked())
{
subtract_box.setEnabled(false);
divide_box.setEnabled(false);
product_box.setEnabled(false);
}else if(subtract_box.isChecked())
{
product_box.setEnabled(false);
divide_box.setEnabled(false);
add_box.setEnabled(false);
}else if(divide_box.isChecked())
{
subtract_box.setEnabled(false);
product_box.setEnabled(false);
add_box.setEnabled(false);
}else if(product_box.isChecked())
{
subtract_box.setEnabled(false);
divide_box.setEnabled(false);
add_box.setEnabled(false);
}
}
}
you have to set a listener.
add_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Disable or enable what you want here. it will be called when add_box is checked or unchecked.
}
});
Do the same for each checkbox, disabling or enabling what you want.
To convert to String to Double you can do that:
Double value1 = Double.valueOf(number_1.getText().toString());
Make sure its not a null value and can be cast to a double.
You can use try / catch.
You may use RadioButton and RadioGroup instead of Checkboxes.
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive.
Official doc of RadioButton:
http://developer.android.com/guide/topics/ui/controls/radiobutton.html
Update:
To convert a String to Double you can do that:
// obtain a reference
EditText number_1 = (EditText)findViewById(R.id.number_1);
// obtain the string value
String number_1String = number_1.getText().toString()
// convert the string value to double value
Double number_1Double = Double.valueOf(number_1String);
i have created a radio group which represents a question and has 4 answer choices (a , b , c , and d ) , hence i created 4 radio buttons for the radiogroup , and it worked fine , but what i want is to keep the answer checked when i leave the activity and return back to it , here is the code :
public class P2 extends Page1 {
RadioGroup rg2 ;
RadioButton r2a , r2b , r2c , r2d ;
Button b2n , b2b ;
TextView tv ;
int count ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_p2);
rg2 = (RadioGroup) findViewById(R.id.RG2);
r2a = (RadioButton) rg2.getChildAt(0);
r2b = (RadioButton) rg2.getChildAt(1);
r2c = (RadioButton) rg2.getChildAt(2);
r2d = (RadioButton) rg2.getChildAt(3);
b2n = (Button) findViewById(R.id.B2n); // next button
b2b = (Button) findViewById(R.id.B2b); // back button to page1
tv = (TextView) findViewById(R.id.res);
rg2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if (r2c.isChecked()){
count ++ ; // since c is the correct answer add 1 to count (mark)
}
}
});
}
public void move21(View view) { // called when b2b is clicked to go back to page1
Intent intent = new Intent(this , Page1.class) ;
startActivity (intent) ;
} }
BTW, i did not define id's for the radiobuttons , just the id (rg2) for the radiogroup .
how can i save the state of the radiogroup using shared preferences ?
thanks
What you can do before you leave the current activity is save the state with something like:
SharedPreferences settings = getSharedPreferences("answers", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("r2a",r2a.isChecked());
editor.putBoolean("r2b",r2b.isChecked());
editor.putBoolean("r2c",r2c.isChecked());
editor.putBoolean("r2d",r2d.isChecked());
editor.apply();
Then, when you come back to the activity, you can do:
SharedPreferences settings = getSharedPreferences("answers", MODE_PRIVATE);
boolean r2achecked = settings.getBoolean("r2a",false);
boolean r2bchecked = settings.getBoolean("r2b",false);
boolean r2cchecked = settings.getBoolean("r2c",false);
boolean r2dchecked = settings.getBoolean("r2d",false);
Then check each of those newly created bools and if one of them is true, set that button to checked.
I have been able to pass data to other activities except this one. Can anyone see what I'm doing wrong. The only error i'm getting is that my TextView showmsg is NOT showing up in the new activity. Does anyone know why?
public class MyScanActivity extends Activity
{
private static final String MY_CARDIO_APP_TOKEN = "NOT THE PROBLEM";
final String TAG = getClass().getName();
private Button scanButton;
private TextView resultTextView;
private Button buttonBack;
private TextView showmsg;
private int MY_SCAN_REQUEST_CODE = 100; // arbitrary int
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.myscan);
Intent in = getIntent();
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText(in.getCharSequenceExtra("usr"));
}
resultTextView = (TextView)findViewById(R.id.resultTextView);
scanButton = (Button)findViewById(R.id.scanButton);
buttonBack = (Button)findViewById(R.id.buttonBack);
showmsg = (TextView) findViewById(R.id.showmsg);
There are not too many options for how your text can be not shown.
You have the view itself messed up: Check this by putting some sample text in the XML file using android:text="TEST" in the TextView showmsg. Your text should appear, unless your text is the wrong color or size, or something else happens to be above it.
You aren't actually finding it with findViewById() (I hope you've double checked that in a debugger) I agree with alex that you might not want R.id.showmsg. Did you mean to put R.id.resultTextView there instead?
Your passed text is not actually coming through. You should do a log statement, like Log.v(TAG, "Passed text is " + in.getCharSequenceExtra("urs")); and make sure the text is actually coming through.
I haven't test it. but i think that is the reason.
change here:
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText(in.getCharSequenceExtra("usr"));
}
with this:
showmsg = (TextView) findViewById(R.id.showmsg);
if (in.getCharSequenceExtra("usr") != null) {
showmsg.setText(in.getCharSequenceExtra("usr"));
}
public class MCQSample extends Activity implements OnClickListener{
TextView title;
String gotBread;
RadioGroup AnswerRG;
int value ;
int value1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mcqsample);
title = (TextView) findViewById(R.id.abc);
nextP = (Button) findViewById(R.id.nextP);
backP = (Button) findViewById(R.id.backP);
AnswerRG = (RadioGroup) findViewById(R.id.AnswerRG);
Bundle b = getIntent().getExtras();
value = b.getInt("key", 0);
}
}
Hi guys, im doing the Android app and stuck on create the dynamic radio-button. Because i don't know how many button i need (which depend on the value - user input). I read some posts which can add on the Layout but i want to add into the radioGroup. Is there any ways? Thank
Step #1: Create a new RadioButton via its constructor and configure it as you see fit.
Step #2: Call addView() on your RadioGroup to add the RadioButton to it.
Step #3: There is no step #3
try this way to add radiobutton in RadioGroup:
private final int WC = RadioGroup.LayoutParams.WRAP_CONTENT;
RadioGroup.LayoutParams rParams;
AnswerRG = (RadioGroup) findViewById(R.id.AnswerRG);
RadioButton radioButton = new RadioButton(this);
radioButton.setText("Yellow");
radioButton.setId(1001);//set radiobutton id and store it somewhere
rParams = new RadioGroup.LayoutParams(WC, WC);
AnswerRG.addView(radioButton, rParams);