If I am adding a number in my app let's say upto 15, I want to make sure the next time I open my app no matter if it crashed, phones was restarted, or app was exited; it should continue from number 15 rather than the 0. How can I work on that?
I tried something like this. This is probably wrong not sure how to fix it.
public class MainActivity extends Activity {
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.but);
final TextView txt = (TextView) findViewById(R.id.txt);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = app_preferences.edit();
i++;
txt.setText(" " + i);
i++;
editor.putInt("Scores: ", i);
editor.commit();
}
});
}
}
public class MainActivity extends Activity {
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// let i be previously saved value or default to zero
i = app_preferences.getInt("Scores: ",0);
Button btn = (Button) findViewById(R.id.but);
final TextView txt = (TextView) findViewById(R.id.txt);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = app_preferences.edit();
i++;
txt.setText(" " + i);
//i++; maybe DONT really increment twice (unless thats what you intended)
editor.putInt("Scores: ", i);
editor.commit();
}
});
}
}
Related
I am doing this project, can anyone help me to delete individual elements from the text view?enter image description here
here's i implemented the 'button' to insert and 'button2' to delete, and here's the code down bellow
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String TAG = "MainActivity";
// To hold all data in different mode : Portrait and landscape
private final String text_context = "TX";
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: in");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById((R.id.textView));
textView.setText(""); // make it no text at runtime, but text at view
textView.setMovementMethod(new ScrollingMovementMethod()); // make it scrolling
editText.setText("");
final View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String result = editText.getText().toString();
result += "\n";
textView.append(result);
editText.setText(""); // text on EDITTEXT will disappear as soon as we click the button
}
};
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
if(editText != null)
button.setOnClickListener(onClickListener);
Log.d(TAG, "onCreate:out");
}
}
Please help me TO IMPLEMENT DELETE ACTION VIA BUTTON2....
Right now your onclick listeners are generically listening for clicks on any view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button events
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button2 events
}
});
Or better yet implement View.OnClickListener() on your activity
Use regex
String input = editText.getText().toString();
String regex = "\\b" + input + "\\b";
String tvText = textView.getText().toString();
tvText = tvText.replaceAll(regex, "");
textView.setText(tvText);
Put this code in
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
//code goes here
}
};
if(editText != null)
// add this onclick to your button2
button2.setOnClickListener(onClickListener1);
Log.d(TAG, "onCreate:out");
}
I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}
i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
I am trying to change the value of circuitLength when fiveMinuteButton is clicked, however, the value is not being changed.
private Button fiveMinuteButton;
int circuitLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_settings_screen);
fiveMinuteButton = (Button) findViewById(R.id.fiveminutesbuttonid);
fiveMinuteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
circuitLength = 5;
}
});
XML:
<Button
android:id="#+id/fiveminutesbuttonid"
android:background="#drawable/whitebutton_shape"
android:layout_below="#+id/workoutlengthtitleId"
android:layout_marginTop="8dp"
android:text="#string/fiveminutes_text"
android:layout_width="90dp"
android:layout_height="60dp" />
Try this code then let me know the out then we further investigate.
private Button fiveMinuteButton;
int circuitLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_settings_screen);
Toast.makeText(this,"circuitLength value ="+circuitLength, Toast.LENGTH_LONG).show();
fiveMinuteButton = (Button) findViewById(R.id.fiveminutesbuttonid);
fiveMinuteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
circuitLength = 5;
Toast.makeText(ACtivityName.this,"circuitLength value ="+circuitLength, Toast.LENGTH_LONG).show();
}
});
Yes, it will not be updated as you are initializing value 5 every time when you click on fiveMinuteButton (i.e. circuitLength = 5 instead write circuitLength = circuitLength + 5).
Change your code to following.
private Button fiveMinuteButton;
int circuitLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_settings_screen);
fiveMinuteButton = (Button) findViewById(R.id.fiveminutesbuttonid);
circuitLength = 5;
InitialzeUIButtons();
fiveMinuteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
circuitLength = circuitLength + 5;
}
});
Your code is correct make sure onclick the button call or not
i think, your onclick button is not working
Your code is correct for sure. if you change any variable inside Activity it remains changed, there are three possibility it changes to initial value.
Activity stopped.
Activity Recreated.
variable changed manually.
as you said here
I put a log. It only stores the value temporarily, then after I call
log again outside of the OnClickListener, the value is null.
I believe you are doing something like this.
private Button fiveMinuteButton;
int circuitLength;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout_settings_screen);
fiveMinuteButton = (Button) findViewById(R.id.fiveminutesbuttonid);
fiveMinuteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
circuitLength = 5;
Log.e("count", ""+circuitLength);
}
});
Log.e("count", ""+circuitLength);
}
Now when you app starts onCreate will called by default circuitLength is null so it is obvious you are getting null in log.
I hope it is clear. if you have any query feel free to ask.
I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage