I am currently working on an app, but there is a bug in it. Whenever a user installs the app or clears data, it should get reset. But instead the app fills in standard data for the user, instead of showing the start screen where user can input himself.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkFirstRun();
savedData();
}
public void savedData() {
showGoalTextView = (TextView) findViewById(R.id.textView3);
showBeamsTextView = (TextView) findViewById(R.id.textView2);
showGoalEditText = (EditText) findViewById(R.id.editText2);
checkBtnPressed = (ImageButton) findViewById(R.id.imageButton5);
showBeamsEditText = (EditText) findViewById(R.id.editText4);
beamsGoal = (EditText) findViewById(R.id.editText4);
//Fetch the stored data and display it upon starting.
String goalSave = showGoalTextView.getText().toString();
String beamsSave = showBeamsTextView.getText().toString();
preferences = getSharedPreferences("userData", MODE_PRIVATE);
goalSave = preferences.getString("goals", goalSave);
beamsSave = preferences.getString("beam", beamsSave);
showGoalTextView.setText(goalSave);
showBeamsTextView.setText(beamsSave);
//Hide all the first use stuff
showGoalEditText.setVisibility(View.GONE);
checkBtnPressed.setVisibility(View.GONE);
showBeamsEditText.setVisibility(View.GONE);
beamsGoal.setVisibility(View.GONE);
}
public void checkFirstRun() {
final String PREFS_NAME = "MyPrefsFile";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
//Get current version code
int currentVersionCode = BuildConfig.VERSION_CODE;
//Get saved version
SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = preferences.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
//Check for first run or upgrade
if (currentVersionCode == savedVersionCode) {
//No big deal
return;
} else if (savedVersionCode == DOESNT_EXIST) {
//TODO This is a new install
resetAll();
} else if (currentVersionCode > savedVersionCode) {
//TODO This is an upgrade
return;
}
//Update sharedPreferences with the current version code
preferences.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
public void resetAll() {
//For when resetting
TextView showGoalTextView =
(TextView) findViewById(R.id.textView3);
EditText showGoalEditText =
(EditText) findViewById(R.id.editText2);
ImageButton checkBtnPressed =
(ImageButton) findViewById(R.id.imageButton5);
EditText showBeamsEditText =
(EditText) findViewById(R.id.editText4);
EditText beamsGoal =
(EditText) findViewById(R.id.editText4);
//Clear editTexts
showGoalEditText.getText().clear();
showBeamsEditText.getText().clear();
//Show all editTexts
showGoalEditText.setVisibility(View.VISIBLE);
checkBtnPressed.setVisibility(View.VISIBLE);
showBeamsEditText.setVisibility(View.VISIBLE);
beamsGoal.setVisibility(View.VISIBLE);
//Get the text view
TextView showResetTextView =
(TextView) findViewById(R.id.textView2);
//Get the value of the text view
String resetString = showResetTextView.getText().toString();
//Convert value to a number and reset it
Integer reset = Integer.parseInt(resetString);
reset = 0;
//Display the new value in the text view.
showResetTextView.setText(reset.toString());
showGoalTextView.setText("BEAMS");
}
My question is mainly on the checkFirstRun method.
I want to input:
} else if (savedVersionCode == DOESNT_EXIST) {
//TODO This is a new install
resetAll();
Unfortunately, I can't get it to work. Can anyone point out the issue at hand?
SharedPreferences is not cleared with uninstall (at least, not since Marshmallow). This might help you solve it SharedPreferences are not being cleared when I uninstall
Related
I am completely lost and don't have enough knowledge on coding/android studio.
I have the EditTexts (FoodIncomeCounter, FoodCampX, and FoodUpgradeX) as inputs.
Then the TextView (FoodIncomeResult) as output.
The 1st button (IncomeSubmitButton) Works properly. The 2nd button (FoodCampSubmitButton) does not, I want it to take the input of the FoodCampXs and FoodUpgradeXs then do a calculation and put that into integer TotalFood, then output TotalFood to the FoodIncomeResult TextView.
How the heck do I do this? I feel like I am close but I don't know what is wrong.
Thank you for the help!!!
public class MainActivity extends AppCompatActivity {
// These are the global variables
EditText FoodIncomeCounter;
EditText FoodCamp1Counter, FoodCamp2Counter, FoodCamp3Counter, FoodUpgrade1Counter, FoodUpgrade2Counter, FoodUpgrade3Counter;
TextView FoodIncomeResult;
int FoodIncome;
int FoodCamp1, FoodCamp2, FoodCamp3, FoodUpgrade1, FoodUpgrade2, FoodUpgrade3;
int TotalFood;
Button IncomeSubmitButton, FoodCampSubmitButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//to get from user input and into variable form
FoodIncomeCounter = (EditText) findViewById(R.id.FoodIncomeCounter);
FoodIncomeResult = (TextView) findViewById(R.id.FoodIncomeResult);
IncomeSubmitButton = (Button) findViewById(R.id.IncomeSubmitButton);
FoodCamp1Counter = (EditText) findViewById(R.id.FoodCamp1Counter);
FoodCamp2Counter = (EditText) findViewById(R.id.FoodCamp2Counter);
FoodCamp3Counter = (EditText) findViewById(R.id.FoodCamp3Counter);
FoodUpgrade1Counter = (EditText) findViewById(R.id.FoodUpgrade1Counter);
FoodUpgrade2Counter = (EditText) findViewById(R.id.FoodUpgrade2Counter);
FoodUpgrade3Counter = (EditText) findViewById(R.id.FoodUpgrade3Counter);
FoodCampSubmitButton = (Button) findViewById(R.id.FoodCampSubmitButton);
//Submit button
IncomeSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//to receive the inputted values
Food = Integer.parseInt(FoodIncomeCounter.getText().toString());
//to show the inputted values into the result fields
FoodIncomeResult.setText(FoodIncomeCounter.getText().toString());
}
});
//Submit button
FoodCampSubmitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//receive the inputted values
FoodCamp1 = Integer.parseInt(FoodCamp1Counter.getText().toString());
FoodCamp2 = Integer.parseInt(FoodCamp2Counter.getText().toString());
FoodCamp3 = Integer.parseInt(FoodCamp3Counter.getText().toString());
FoodUpgrade1 = Integer.parseInt(FoodUpgrade1Counter.getText().toString());
FoodUpgrade2 = Integer.parseInt(FoodUpgrade2Counter.getText().toString());
FoodUpgrade3 = Integer.parseInt(FoodUpgrade3Counter.getText().toString());
//get food income and show
TotalFood = FoodCamp1 + (FoodCamp2 * 2) + (FoodCamp3 * 3) + (FoodUpgrade1 * 2) + (FoodUpgrade2 * 4) + (FoodUpgrade3 * 6);
FoodIncomeResult.setText(String.valueOf(TotalFood));
}
});
}
}
I would like to know how I can store a boolean with SharedPreferences and disable a button. If you restart the app, the button should remain disabled.
Here is my Code, but something is wrong.
public class Pass extends AppCompatActivity implements View.OnClickListener {
private Button btn1;
private EditText text1;
private SharedPreferences speicher;
private SharedPreferences.Editor editor;
final boolean enabled = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pass);
btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(this);
text1 = (EditText) findViewById(R.id.editText);
text1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
speicher = getApplicationContext().getSharedPreferences("Daten", 0);
editor = speicher.edit();
speicher = getSharedPreferences("Daten", 0);
speicher.getBoolean("Data1", enabled);
}
public void onClick (View view){
if (text1.getText().toString().equals("pass")){
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setMessage("Great");
ad.show();
Intent intent = new Intent(this,Popup.class);
startActivity(intent);
btn1.setEnabled(enabled);
editor.putBoolean("Data1", enabled);
editor.commit();
}else{
String message = "Wrong";
Toast.makeText(this,message, Toast.LENGTH_LONG).show();
}
}
}
I hope you can help me.
since
Strecki
You're not checking the condition to enable or disable button that you're getting from SharedPreference. Do something like this:
btn1.setEnabled(speicher.getBoolean("Data1", enabled));
Try this one it will work.
Remove this line on your code. Because initialized again here.
"speicher = getSharedPreferences("Daten", 0);"
You have to try like this
SharedPreferences speicher = getApplicationContext().getSharedPreferences("Daten", 0);
SharedPreferences.Editor editor = speicher.edit();
Store the value like this
editor.putBoolean("Data1", true); editor.commit();
Get value like this
Boolean result = speicher.getBoolean("Data1", false); //false is default value.
I have some troubles trying to put an id to a dynamic edittext and texview in Android Studio, i want to use this id to get the id's in another functions.
Note: Im not setting any id in the onCreate function.
This is my code:
for (Map.Entry<String,String> entry : getMap(newContact).entrySet()) {
total++;
TextView ProgrammaticallyTextView = new TextView(this.getActivity());
EditText ProgrammaticallyEditText = new EditText(this.getActivity());
ProgrammaticallyTextView.setId(total);
ProgrammaticallyEditText.setId(total+1);
ProgrammaticallyTextView.setText(entry.getKey());
ProgrammaticallyEditText.setText(entry.getValue());
linearLayout.addView(ProgrammaticallyTextView);
linearLayout.addView(ProgrammaticallyEditText);
total++;
}
This is the function that i use the edittext and textview id's
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_create:
String test = "";
for(int i=0; i < (this.total); i+=2){
TextView tv = (TextView) v.findViewById(i++);
EditText et = (EditText) v.findViewById(i+2);
if ((i+2) >= this.total){
test += tv.getText()+"="+et.getText();
}else
{
test += tv.getText()+"="+et.getText()+",";
}
}
mContact = getMap(test);
newContactRequest();
break;
}
}
I appreciate any help!
You need to explicitly change the data type to string. Just writing i++ tries to assign that value as an integer.
I think if you change your lines to these it should work.
TextView tv = (TextView) v.findViewById(String.valueOf(i++));
EditText et = (EditText) v.findViewById(String.valueOf(i+2));
I have an activity with 3 edittexts and a button.
I have a second activity with 2 textviews.
When button is clicked i want two random edittexts values from activity1 to replace the text on textviews on activity2.
I managed to do that, but not randomly. How can I make it random?
Here is the first activity.
final EditText et = (EditText) findViewById(R.id.editText);
final EditText et1 = (EditText) findViewById(R.id.editText2);
final EditText et2 = (EditText) findViewById(R.id.editText3);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
Intent i = new Intent(MainActivity.this, postavke.class);
i.putExtra("prvi", et.getText().toString());
i.putExtra("drugi", et1.getText().toString());
i.putExtra("treci", et2.getText().toString());
startActivity(i);
}
});
}
Here is the second activity.
TextView tv = (TextView) findViewById(R.id.asdf);
tv.setText(getIntent().getExtras().getString("prvi"));
TextView dr = (TextView) findViewById(R.id.asdg);
dr.setText(getIntent().getExtras().getString("drugi"));
In your second activity:
String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
};
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Random random = new Random();
tv.setText(texts[random.nextInt(3)]);
dr.setText(texts[random.nextInt(3)]);
Or, for unique values:
First activity:
final EditText et = (EditText) findViewById(R.id.editText);
final EditText et1 = (EditText) findViewById(R.id.editText2);
final EditText et2 = (EditText) findViewById(R.id.editText3);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
ArrayList<String> texts = new ArrayList<>();
texts.add(et.getText().toString());
texts.add(et1.getText().toString());
texts.add(et2.getText().toString());
Intent i = new Intent(MainActivity.this, postavke.class);
i.putExtra("texts", texts);
startActivity(i);
}
});
}
Second activity:
ArrayList<String> texts = getIntent().getExtras().getStringArrayList("texts");
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Collections.shuffle(texts);
tv.setText(texts.get(0));
dr.setText(texts.get(1));
Add your Strings(Eg : getIntent().getExtras().getString("prvi")) from previous Activity to a ArrayList.
Then do simply Collection.shuffle(your_list);
Get items from ArrayList & set it to your TextViews.
Declare an array that will contain the values:
public String[] randomAnswers = new String[3]; //3 because you said you have 3 random strings
In your onCreate() add the values to the array:
randomAnswers[0] = getIntent().getExtras().getString("prvi");
randomAnswers[1] = getIntent().getExtras().getString("drugi");
randomAnswers[2] = getIntent().getExtras().getString("treci");
Select two random values:
private static final Random RANDOM = new Random(); //in java.util package
Inside onCreate:
final int randomInt1 = RANDOM.nextInt(3); //nextInt(int) goes from 0 to int-1, considering arrays start at index=0 we want to get a random number from: 0, 1, 2
int randomInt2;
do {
randomInt2 = RANDOM.nextInt(3);
} while (randomInt1 == randomInt2);
final String firstRandomString = values[randomInt1];
final String secondRandomString = values[randomInt2];`
you can use some thing like this the edited form of #Joe Frostick answere:
String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
};
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Random random = new Random();
texts.remove(random.nextInt(3));
tv.setText(texts[0]);
dr.setText(texts[1]);
Thanks for the help everyone, I really didn't expect so many answers in not much time. You're the best!
I'm trying to create a calculator for Android using Eclipse. This is my code for the function 'cal', 'cal' is executed when a button is clicked, But clicking on the button closes the application, I tried an other button with different function and it works fine. Can anyone point out the mistake that I've done?
public void cal( View view ){
EditText op = (EditText) findViewById(R.id.editText2);
EditText n1 = (EditText) findViewById(R.id.editText1);
EditText n2 = (EditText) findViewById(R.id.editText3);
EditText res = (EditText) findViewById(R.id.textView1);
String sn1 = n1.getText().toString();
String sn2 = n2.getText().toString();
String sres;
String sop;
int in1 = Integer.parseInt(sn1);
int in2 = Integer.parseInt(sn2);
int ires;
sop = op.getText().toString();
if(sop == "+"){
ires = in1 + in2;
sres = Integer.toString(ires);
res.setText(sres);
}
}
There is no exception in this.
I think you forgot to set the activity layout which causes your application force close.
or
Problem here
EditText res = (EditText) findViewById(R.id.textView1);
Are you sure your editText name is textView1 in your layout.
If it textView then do like this.
TextView res = (TextView) findViewById(R.id.textView1);