I'm trying to make a progress bar where i can increase/reset the value and i did that so far, but i want to be able to somehow save the state of where I left off after I leave the application, I need the progress bar's percentage to stay the same even when i quit it, I've read something about on resume methods and shared preferences but I don't really get how to do it and apply it on my own code, any help will be really appreciated, this is my activity's code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import me.itangqi.waveloadingview.WaveLoadingView;
public class MainActivity extends AppCompatActivity {
WaveLoadingView waveLoadingView;
SeekBar seekBar;
Button increase,restart;
private int prog = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar=findViewById(R.id.seekbar);
waveLoadingView=findViewById(R.id.waveloading);
waveLoadingView.setProgressValue(0);
increase=findViewById(R.id.increase);
restart=findViewById(R.id.restart);
increase.setOnClickListener(v -> {
if(prog<=100){
prog+=10;
updateProgressBar();
}
if(prog<50){
waveLoadingView.setBottomTitle(String.format("%d%%",prog)+"you're drinking too little");
waveLoadingView.setCenterTitle("");
waveLoadingView.setTopTitle("");
}else if(prog<80){
waveLoadingView.setBottomTitle("");
waveLoadingView.setCenterTitle(String.format("%d%%",prog)+"You Are Almost There!");
waveLoadingView.setTopTitle("");
}else{
waveLoadingView.setBottomTitle("");
waveLoadingView.setCenterTitle("");
waveLoadingView.setTopTitle(String.format("%d%%",prog)+"\r\n You've Made It!");
}
if(prog==110){
waveLoadingView.setProgressValue(0);
}
});
restart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
waveLoadingView.setProgressValue(0);
waveLoadingView.setBottomTitle("");
waveLoadingView.setCenterTitle("");
waveLoadingView.setTopTitle("");
prog=0;
}
});
}
private void updateProgressBar() {
waveLoadingView.setProgressValue(prog);
}
}
Use the shared preferences file to save the value and you can read it when you reopen the app.
private void updateProgressBar() {
waveLoadingView.setProgressValue(prog);
getSharedPreferences("nameyourfile" , MODE_PRIVATE). edit(). putInt("progress', prog). apply() ;
}
OnResume
int prog = getSharedPreferences("nameyourfile" , MODE_PRIVATE).getInt("progress', yourdefaultvalueprogress) ;
updateProgressBar(prog) ;
Related
We are working on creating a basic app in my online class and I keep running into this issue where the Instructions have us type a method with an #Override for an onClick. I was able to get one of the methods to work with an autofill help from A.S. itself but this next one won't fix itself out. I have run into other issues over this class as well and I know the Instructions are a few years old. Possibly out of date now?
I'm pretty new to Android Java and coding overall. It seems that the app is trying to use the onClick method incorrectly. I will post the whole class I'm working with because I'm not sure where the problem is exactly.
package com.test.firstandroidapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
public class InformationPageActivity extends AppCompatActivity {
Button btnSave;
Spinner spnSeason;
SeekBar skbTemp;
TextView lblSeekValue;
Switch swchAllergy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information_page);
spnSeason = (Spinner) findViewById(R.id.spnSeason);
skbTemp = (SeekBar) findViewById(R.id.skbTemp);
swchAllergy = (Switch) findViewById(R.id.swchAllergy);
lblSeekValue = (TextView) findViewById(R.id.lblSeekValue);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener((View.OnClickListener) this);
skbTemp.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String display = String.valueOf(progress);
lblSeekValue.setText(display);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
#Override
public void onClick(View v) {
String season, allergies;
int temperature;
season = spnSeason.getSelectedItem().toString();
temperature=skbTemp.getProgress();
allergies = (String) (swchAllergy.isChecked() ? swchAllergy.getTextOn() : swchAllergy.getTextOff()); //if the switch is on then the user has allergies
Intent intent = new Intent(InformationPageActivity.this, InformationResultsActivity.class); //pass information to the results activity
intent.putExtra("season", season);
intent.putExtra("temperature", temperature);
intent.putExtra("allergies", allergies);
this.startActivity(intent);
}
}
And here is the Main Menu Class parts that go to the view i'm trying to have opened, The onclicklistener is inside the onCreate method and the goInfo is it's own method
Button btnInfo = (Button) findViewById(R.id.btnInfo);
btnInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goInfo();
}
}
);
}
private void goInfo() {
Intent intent = new Intent(MainMenuActivity.this, InformationPageActivity.class);
this.startActivity(intent);
}
I was able to find the issue. I needed to add an implements statement to the class for the onclicklistener. I'm answering my own question in case someone in the future has the same error. Here is what I did.
My problematic class was
public class InformationPageActivity extends AppCompatActivity {
This was causing the error "java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.firstandroidapp/com.test.firstandroidapp.InformationPageActivity}: java.lang.ClassCastException: com.test.firstandroidapp.InformationPageActivity cannot be cast to android.view.View$OnClickListener"
I solved the issue by fixing the class adding the implements
public class InformationPageActivity extends AppCompatActivity implements View.OnClickListener {
I am a beginner in android.
My Scenario
I have a screen A which has 2 buttons
Button A
Button B.
When I open my application screen A opens up with the above 2 buttons, when I click on Button B a textview and Edittext is displayed.
What I want ?
When the back button is pressed the textview and edittext should hide and when I press back again, I should exit out of Screen A.
What have I tried so far ?
Is my below code correct for what I want ?
Main Activity.xml
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView title;
EditText userinput;
Button buttonA,buttonB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
userinput = (EditText)findViewById(R.id.userinput);
title = (TextView)findViewById(R.id.title);
buttonA = (Button)findViewById(R.id.buttonA);
buttonB = (Button)findViewById(R.id.buttonB);
buttonA.setOnClickListener(this);
buttonB.setOnClickListener(this);
}
#Override
public void onBackPressed() {
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.buttonA:
break;
case R.id.buttonB:
title.setVisibility(View.VISIBLE);
userinput.setVisibility(View.VISIBLE);
break;
}
}
}
I referred this and this link but did not understand. If some one could help me in achieving me want I want 1
When the back button is pressed the textview and edittext should hide
and when I press back again,
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE) {
super.onBackPressed();
return;
}
title.setText(null);
userinput.setText(null);
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
Do like this.
#Override
public void onBackPressed() {
if(title.getVisibility()==View.VISIBLE)
{
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
else
{
finish();
}
}
Hop it will do what you want.
Change the code to below
#Override
public void onBackPressed() {
if (title.getVisibility() != View.VISIBLE &&
userInput.getVisibility() != View.VISIBLE){
title.setVisibility(View.INVISIBLE);
userinput.setVisibility(View.INVISIBLE);
}
super.onBackPressed();
}
I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. Here is my activity:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class test extends Activity {
private Button btnChangeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salesticketoilui);
mainProgram();
}
public void mainProgram() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
} // end onClick
}); // end setOnClickListener
Button buttonExit = (Button)findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitActivity();
} // end onClick
}); // end OnClickListener
// setup button listener for saving data and exit to main
Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
buttonSaveExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveExit();
} // end onClick
}); // end OnClickListener
} // end MainProgram ()
public void saveExit() {
// does stuff
}
public void exitActivity () {
// does stuff
}
} // end class
any thoughts?
Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. Either call mainProgram from onCreate or just put that code directly into onCreate.
I believe the onClickListeners need to go inside of your onCreate method.
Listen to Scott
and looks like your missing the #Override
new View.OnClickListener() {
#Override
public void onClick(View view) {
exitActivity();
} // end onClick
}
Make sure your java settings are to 1.6 to avoid code completion missing this.
I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste.
To make it work, I had to create new buttons in my layout and declare the events manually. Weird!
Hello StackOverflow Users,
I am new to android and trying to develop a game in which I use a
1) Main class to redirect (like a menu.. new game, options, help, exit etc..)
2) A surfaceview class
3) A thread to handle drawing on canvas.
I have added an exit button on the main class.
However after playing the game i.e. drawing the objects and using them, when i redirect to my Main class and try to exit; the main screen disappears but the view and threads aren't destroyed.
This is the main class.
package com.tgm.welcome;
import com.tgm.R;
import com.tgm.main.GThread;
import com.tgm.main.TGMActivity;
import com.tgm.options.OptionsMain;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Welcome_Act extends Activity {
ImageView game, exit, options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
game = (ImageView) findViewById(R.id.newGame);
options = (ImageView) findViewById(R.id.options);
exit = (ImageView) findViewById(R.id.exit);
game.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gotogame();
}
});
options.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goto_opt();
}
});
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
exit_game();
}
});
}
public void gotogame() {
Intent game = new Intent(Welcome_Act.this, TGMActivity.class);
startActivity(game);
}
public void goto_opt() {
Intent opt = new Intent(Welcome_Act.this, OptionsMain.class);
startActivity(opt);
}
public void exit_game() {
System.exit(0);
}
}
PLEASE HELP TO REMOVE THE GAMESCREEN FROM THE STACK THAT ANDROID MAINTAINS.
Thanks..
Using System.exit(0) is not advised in android. It doesnt guarantee finishing the Activity.
Instead of
public void exit_game() {
System.exit(0);
}
Use:
public void exit_game() {
Welcome_Act.finish();
}
Just call finish on the activity you want to remove from the stack.. it does the work you want..
I'd like to play a sound as I tap a button like hitting a drum, to have the sound playback as quick as I'm hitting the button. Right now I have to wait for the sound to finish before it will play again. Here's how I've got the sound set up.
MediaPlayer snareMP;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playdrums);
final MediaPlayer snareMP = MediaPlayer.create(this, R.raw.snare);
ImageView snareDrum = (ImageView) findViewById(R.id.snare);
snareDrum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
snareMP.reset();
snareMP.prepare();
snareMP.start();
}
});
Any help is greatly appreciated.
Thanks!
Try this:
public void onClick(View v) {
if (snareMP.isPlaying()){
snareMP.stop();
snareMP.reset();
}
snareMP.prepare();
snareMP.start();
}
I'm going to share a code that I've written a while back which I think does the exact same thing you're looking for; I hope it helps and I'd appreciate some rep points if it does :)
Here it is:
import com.myProject.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class Camera extends Activity {
private MediaPlayer snapMP;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.camera);
Button snap = (Button)findViewById(R.id.snapButton);
snap.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
freeSnapMP();
snapMP = MediaPlayer.create(Camera.this, R.raw.camera_snap2);
snapMP.start();
}
});
}
protected void onDestroy() {
super.onDestroy();
freeSnapMP();
}
private void freeSnapMP(){
if (snapMP != null){ snapMP.stop(); snapMP.release(); snapMP = null; }
}
}