I want to move to another Activity after 5 seconds.
(Step4Activity -> WeightActivity)
I found this code, and it worked.
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(Step4Activity.this, WeightActivity.class);
startActivity(intent);
}
}, 5000);
But I want to use a variable 'count' as a parameter 'delaymillis' like this, but it didn't work.
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(Step4Activity.this, WeightActivity.class);
startActivity(intent);
}
}, count * 1000); // int count = 5
Is there any way to use variable?
Here's my full code. (Step4Activity.java)
package com.example.starbucksook;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;
public class Step4Activity extends AppCompatActivity {
//TextView countdown_text;
//LinearLayout timeCountSettingLV, timeCountLV;
//TextView hourET, minuteET, secondET;
//TextView hourTV, minuteTV, secondTV, finishTV;
//Button startBtn;
//int hour, minute, second;
int count = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step4);
// Unrelated code skipped
/*Here is 'Handler-postDelayed' method.*/
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(Step4Activity.this, WeightActivity.class);
startActivity(intent);
}
}, count * 1000);
}
Related
I have an array that I want to output each index continuously and randomly
here is my code :
package com.example.testtingskripsi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public String [] ArrayA ={"80","81","82","83","84","85"};
public TextView mViewLabel;
#Override
protected void onCreate(Bundle savedInstanceState) {
int rando =(int) ((Math.random())*ArrayA.length);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewLabel = (TextView) findViewById(R.id.textChanger);
Button startHeartbeat = (Button) findViewById(R.id.start);
Button stopHeartbeat = (Button) findViewById(R.id.stop);
startHeartbeat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
for(int mIfCounter = 0;mIfCounter<ArrayA.length-1;mIfCounter++){
mViewLabel.setText(ArrayA[mIfCounter]);
}
}
}, 1000);
}
});
}
}
the output doesn't change only showing one index and doesn't change to the next index
edit:
it does work but on the output, it only shows the last index but still doesn't change
edit 2:
Okay so I fixed it with new code and try some of you guys help
this is new code
package com.example.testtingskripsi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
public String [] ArrayA ={"80","81","82","83","84","85"};
public TextView mViewLabel;
boolean continueThread=true;
int count =0;
Thread t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewLabel = (TextView) findViewById(R.id.textChanger);
List<String> list = Arrays.asList(ArrayA);
Collections.shuffle(list);
t = new Thread(){
#Override
public void run() {
while(continueThread){
try{
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
if(count<=6){
count++;
}else{
count--;
}
mViewLabel.setText(list.get(count));
}
});
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
}
public void BtnStart(View view){
t.start();;
}
public void BtnStop(View view){
continueThread=false;
}
}
it stop at end of array, so i want it to doing the loop until i pressed stop button any help
Already figured it out so okay thank you all for helling me
You probably need to add some delay inside the for loop.
Try Thread.sleep inside the loop.
The device changes 60 fps so its hard to view the changes for a small loop
I have the code for the sound and the next activity. I do not know how to merge the two codes in order to execute them at the same time or in sequence.
package com.example.testmessages;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Add sound
final MediaPlayer gunSoundMP = MediaPlayer.create(this, R.raw.gunsound);
ImageButton GunSound=(ImageButton)this.findViewById(R.id.imageButton3);
GunSound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
gunSoundMP.start();
}
});
//Image button to execute new Activity
imageButton=(ImageButton) findViewById(R.id.imageButton3);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadMain2Activity = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intentLoadMain2Activity);
gunSoundMP.start();
}
});
}
}
Inside my class I have timer which counts from 5 sec to 0 sec. I want to start new activity when counting ends. The problem is I dont know which context to put as argument in function.
Here is the code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class PlayGameActivity extends Activity {
private Intent i;
private Game igra;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
}
new CountDownTimer(5000, 1000) {
TextView mTextField = (TextView)findViewById(R.id.textView6);
public void onTick(long millisUntilFinished) {
mTextField.setText("" + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("Gotovo!");
//here is the problem!
Intent intent = new Intent( getBaseContext() , TableActivity.class);
startActivity(intent);
}
}.start();
}
You should pass PlayGameActivity.this:
Intent intent = new Intent( PlayGameActivity.this , TableActivity.class);
Im trying to make an app with buttons to call different phone numbers. I have this:
package com.BigTooth.Apps.Recromax;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.*;
import android.net.Uri;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void phoneCall1()
{
String phoneCallUri = "tel:4078421430";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
// add button listener
button.setOnClickListener(new OnClickListener() {
private void phoneCall()
{
String phoneCallUri = "tel:8889807091";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);}
}
}
In my MainActivity.java file. Its telling me that isn't correct. Please help!!!
button.setOnClickListener is in the wrong place. Also, the onClickListener is a bit wring. Should be:
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.content.*;
import android.net.Uri;
import android.view.View.OnClickListener;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.youButtonId);
Button button1 = (Button) findViewById(R.id.youButtonId1);
// add button listener
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
phoneCall();
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
phoneCall1();
}
});
}
private void phoneCall()
{
String phoneCallUri = "tel:8889807091";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
private void phoneCall1()
{
String phoneCallUri = "tel:4078421430";
Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
startActivity(phoneCallIntent);
}
}
I have an application that displays a splash screen then a 'Showonce' screen, I have used app prefs and a boolean "user_accept" to decide wether the screen gets displayed or not, trouble no matter if the boolean = true it still shows the page, please see my code for the pages, any help greatly appreciated :).
package com.overclockerz.webtest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Window;
import android.widget.Toast;
public class SplashScreen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash_layout);
final SharedPreferences settings = getSharedPreferences("APP_PREFS",
MODE_PRIVATE);
final boolean hasAgreed = settings.getBoolean("user_accepted", false);
Handler handler = new Handler();
// run a thread after 2 seconds to start the home screen
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (hasAgreed == false){
Intent intent = new Intent(SplashScreen.this, ShowOnce.class);
SplashScreen.this.startActivity(intent);
Toast.makeText(getApplicationContext(), "DEBUG: USER HAS NOT ACCEPTED",Toast.LENGTH_LONG).show();
}else if (hasAgreed == true){
Toast.makeText(getApplicationContext(), "DEBUG: USER HAS ACCEPTED",Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
finish();
}
}, 2000); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
}
package com.overclockerz.webtest;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class ShowOnce extends Activity implements OnClickListener {
Button show_options_dialog, accept_button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show_once);
show_options_dialog = (Button) findViewById(R.id.show_options_dialog);
accept_button = (Button) findViewById(R.id.accept_button);
show_options_dialog.setOnClickListener(this);
accept_button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == show_options_dialog){
Intent intent = new Intent(getApplicationContext(), SettingScreen.class);
startActivity(intent);
}
else if (v == accept_button){
SharedPreferences settings = getSharedPreferences("APP_PREFS",
MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putBoolean("user_accept", true);
Toast.makeText(getApplicationContext(), "DEBUG: USER CLICKED ACCEPT", Toast.LENGTH_LONG).show();
prefEditor.commit();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}
}
you are saving user_accept and checking for user_accepted
Please use public static final String to avoid such problems:
public static final String USER_ACCEPTED = "accepted";
.........
prefEditor.putBoolean(USER_ACCEPTED , true);
.........
settings.getBoolean(USER_ACCEPTED , false);