reset progressbar to 0 on backpressed - java

I am trying to use progressbar like a 30 secs timer. It works well but when i click back button the progressbar progress continues. I am unable to reset the progressbar progress to 0 when i click the backbutton.
Here is my code:
MainAcitivty:
public class MainActivity extends AppCompatActivity {
public Button btnA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnA = (Button)findViewById(R.id.btnA);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentA = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intentA);
}
});
}
}
SecondActivity:
public class SecondActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Context context;
float from;
float to;
final TextView textV;
final ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
final int[] i = {0};
textV = (TextView)findViewById(R.id.tvFinal);
mProgressBar = (ProgressBar)findViewById(R.id.progressbar);
mProgressBar.setProgress(i[0]);
mCountDownTimer=new CountDownTimer(30000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress"+ i[0] + millisUntilFinished);
i[0]++;
mProgressBar.setProgress((int) i[0] *100/(30000/1000));
}
#Override
public void onFinish() {
i[0]++;
mProgressBar.setProgress(100);
textV.setText("finish");
Intent intent = new Intent(SecondActivity.this, SecondActivity.class);
startActivity(intent);
}
};
mCountDownTimer.start();
}
}

in order to implement behaviour of back button you need to override onBackPressed() and then implement method to reset progressbar result.
Example:
#Override
public void onBackPressed() {
super.onBackPressed();
progressBar.setProgress(0);
}

Related

Android: How can I use ringtone.stop in activity by click button

I have Mybroadcastreceiver and when rintone is play stopAlarm.java page is shown If button is pressed i want to use stop ringtone. How can I use r.stop() in my stopAlarm.java
public class MyBroadcastReceiver extends BroadcastReceiver {
public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
#Override
public void onReceive(Context context, Intent intent) {
Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(context,notificationsound);
r.play();
Intent i = new Intent(context,stopAlarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
public class stopAlarm extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_alarm);
Button stop = (Button)findViewById(R.id.stopAlarm);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//////////How can I get r
}
});
}
}
public class stopAlarm extends AppCompatActivity {
Ringtone r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_alarm);
Button stop = (Button)findViewById(R.id.stopAlarm);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
r.stop();
}
});
}

Passing EditText integer to TextView via Intent app crashes after clicking (button) three

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"));
}

How to start an Activity task using Threads?

I want to start the Main2Activity's task(Loading the WebView) when button is pressed but not to show the Main2Activity's Screen until onPageFinished() is called. i had some ideas to get this by Intents however it seems doesn't work.
Here my code :
MainActivity.java :
public class MainActivity extends AppCompatActivity {
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button)findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Runnable runnable = new Runnable() {
#Override
public void run() {
handler.sendEmptyMessage(0);
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
);
}
}
Main2Activity.java :
public class Main2Activity extends AppCompatActivity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final WebView webView = (WebView)findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webView, url);
intent.putExtra("DONE",1);
// Toast.makeText(getApplicationContext(), "Done!",
//Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("file:///android_asset/2.2.html");
}
}
is this possible using Intents or i have to do this with some other techniques?
How can i Achieve this goal ?
Thanks All.
I think you should directly go to Main2Activity on button click, without calling any thread in MainActivity.
Then in Main2Activity show an image or something until onPageFinished() is called.

Android QR reader with Zxing

When I read Qr code, textView doesn't change.(Question 1) What is the problem ?
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private Button buton;
private TextView textView;
private ZXingScannerView myview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buton = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
buton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myview = new ZXingScannerView(MainActivity.this);
myview.setResultHandler(MainActivity.this);
setContentView(myview);
myview.startCamera();
}
});
}
#Override
protected void onPause() {
super.onPause();
myview.stopCamera();
}
#Override
public void handleResult(Result result) {
setContentView(R.layout.activity_main);
textView.setText(result.getText().toString());
myview.stopCamera();
}
}
And when I finished reading Qr code,I want to start new activty with result.
(Question 2) How I do it ? Will this code work ?
#Override
public void handleResult(Result result) {
myview.stopCamera();
Intent intent = new Intent(getApplicationContext(),SecondActivty.class);
intent.putExtra("Result",result);
startActivity(intent);
}
If it doesn't, how should I fix it ?
Remove setContentView(R.layout.activity_main); from handleResult. You're replacing the view that you have references to with a new view.
As for communicating the Result over intent, as is, what you have will not work. Result does not inherit from Parcelable, and you can't just stick it in an intent and expect it to work. Better would be to get all relevant info from the Result and put it in the Intent as a String.

Passing data which is constantly updated between two activities

I have two activities, my MainActivity runs TimerTask which increments "counter" value.
public class MainActivity extends AppCompatActivity {
public static int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
counter++;
}
});
}
};
timer.schedule(timerTask, 0, 1000);
Intent intent = new Intent(this, MapActivity.class);
startActivity(intent);
}
}
My MapActivity is supposed to read this value upon button click.
public class MapActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
public void readCounter(View view){
Log.d("counter:", Integer.toString(MainActivity.counter));
}
}
Currently counter is declared as public static variable but since static variables are bad practice, what would be proper and elegant way to do it?
There's nothing wrong with static values. In face if you want to make it MVC and use model, you should probably wrap this value with a Singleton Model class.
But to simplify things I would only recommend to make sure you reset the value on MainActivity onCreate.
The android way to do it would be using services or startActivityForResult.
Bundles are used to add extra parameters to pass to the called activity. So you can get and use this extra parameter as you wish.
Here is how to do it:
in MainActivity:
Intent intent = new Intent(this, MapActivity.class);
intent.putExtra("counter", counter);
startActivity(intent);
in MapActivity:
Bundle extras = getIntent().getExtras();
int counter = extras.getIntExtra("counter");
EDIT 1: For continuous update use custom listener interface
public class MainActivity extends AppCompatActivity {
public static int counter = 0;
private MyCounterUtil util;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
util = MyCounterUtil.getInstance();
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
counter++;
util.notifyUpdate(counter);
}
});
}
};
timer.schedule(timerTask, 0, 1000);
Intent intent = new Intent(this, MapActivity.class);
startActivity(intent);
}
}
Create this class
public class MyCounterUtil {
private static MyCounterUtil mInstance;
private MyCounterListener mListener;
private MyCounterUtil() {}
public static MyCounterUtil getInstance() {
if(mInstance == null) {
mInstance = new MyCounterUtil();
}
return mInstance;
}
public void setListener(MyCounterListener listener) {
mListener = listener;
}
private void notifyUpdate(int count) {
if(mListener != null)
mListener.onUpdate(count);
}
public interface MyCounterListener{
public void onUpdate(int count);
}
}
Implement listener interface in MapActivity
public class MapActivity extends AppCompatActivity implements MyCounterListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
MyCounterUtil.getInstance().setListener(this);
}
/*
public void readCounter(View view){
Log.d("counter:", Integer.toString(MainActivity.counter));
}
*/
#Override
public void onUpdate(int count){
Log.d("counter:", Integer.toString(count));
}
}
EDIT 2:
Another clean and simple approach without TimerTask is using system time as counter and pass its first value with intent or sharedpreferences.
in MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("startTime", System.currentTimeMillis());
editor.apply();
*/
Intent intent = new Intent(this, MapActivity.class);
intent.putExtra("startTime", System.currentTimeMillis());
startActivity(intent);
}
}
in MapActivity:
public class MapActivity extends AppCompatActivity {
private long startTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
/* in case use of sharedpref
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
startTime = preferences.getLong("startTime", 0);
*/
//in case use of bundle
startTime = getIntent().getExtras().getLongExtra("startTime", 0);
}
public void readCounter(View view){
long counter = (System.currentTimeMillis() - startTime)/1000;
Log.d("counter:", "" + counter);
}
}

Categories