I have implemented two chronometers in a same activity and so far they are working fine. But when I change activities and then come back to them, they are at 00:00. I need them to keep counting "forever" until I hit the stop button. How can I do that?
And is it possible to make that counting appear also in another activity, so that I can follow up the counting in either activity? It has to be the same counting. Just two different places where I can follow the up from.
I tried a solution with onRestoreInstanceState but I am really confused. I am really new to coding and simple things are still frustrating to me. I am getting something wrong at the set.Base().
Chronometer cronometro_primeira_marca, cronometro_segunda_marca;
Button btn_iniciar_cronometro_primeira_marca, btn_parar_cronometro_primeira_marca, btn_resetar_cronometro_primeira_marca;
Button btn_iniciar_cronometro_segunda_marca, btn_parar_cronometro_segunda_marca, btn_resetar_cronometro_segunda_marca;
String timer1, timer2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cronometros);
cronometro_primeira_marca = (Chronometer) findViewById(R.id.cronometro_primeira_marca);
cronometro_segunda_marca = (Chronometer) findViewById(R.id.cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_primeira_marca);
btn_parar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_parar_cronometro_primeira_marca);
btn_resetar_cronometro_primeira_marca = (Button) findViewById(R.id.btn_resetar_cronometro_primeira_marca);
btn_iniciar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_iniciar_cronometro_segunda_marca);
btn_parar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_parar_cronometro_segunda_marca);
btn_resetar_cronometro_segunda_marca = (Button) findViewById(R.id.btn_resetar_cronometro_segunda_marca);
btn_iniciar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
cronometro_primeira_marca.start();
}
});
btn_iniciar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
cronometro_segunda_marca.start();
}
});
btn_parar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.stop();
}
});
btn_parar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.stop();
}
});
btn_resetar_cronometro_primeira_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_primeira_marca.setBase(SystemClock.elapsedRealtime());
}
});
btn_resetar_cronometro_segunda_marca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cronometro_segunda_marca.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putLong("Restored_time1", SystemClock.elapsedRealtime());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if((savedInstanceState !=null)&& savedInstanceState.containsKey("Restored_time1")){
SystemClock.elapsedRealtime().**setBase**(savedInstanceState.getLong("Restored_time1"));
}
super.onRestoreInstanceState(savedInstanceState);
}
}
Related
I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
I would like to display images that are stored in an arraylist to an imageview.I am using picasso library to store the links.When i press the button next i want the picture to change to the next image.I am using a for loop but i get only the last element.Here is the code:
ImageView image1;
Button bNext,
ArrayList<String>ll=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fwtografies);
image1=(ImageView)findViewById(R.id.image1);
bNext=(Button)findViewById(R.id.bNext);
ll.add("http://i.imgur.com/QoUeA2I.jpg");
ll.add("http://i.imgur.com/21szRz9.jpg");
ll.add("https://upload.wikimedia.org/wikipedia/en/e/ec/Clip_Poster.jpg");
bBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent fwtografiesActivityIntent = new Intent(Fwtografies.this,MainActivity.class);
Fwtografies.this.startActivity(fwtografiesActivityIntent);
}
});
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for(int i=0;i<ll.size();i++){
sp(ll.get(i),image1);
}
}
});
bPrevius.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewFlipper.showPrevious();
}
});
sp("http://i.imgur.com/gijIKJO.jpg",image1);
//sp("http://i.imgur.com/QoUeA2I.jpg",image2);
//sp("http://i.imgur.com/21szRz9.jpg",image3);
}
public void sp(String a,ImageView b){
Picasso
.with(getApplicationContext())
.load(a)
.into(b);
}}
Everytime you click on Button "bnext" you are going through your whole for-loop and therefore only the last element of your list gets shown. Try:
int i = 0;
bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(i<ll.size()){
sp(ll.get(i),image1);
i++;
}
}
});
My mistake is that i used for loop where i should use if statment.
This code works for my case:
int i=0; bNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//viewFlipper.showNext();
if(i<ll.size()){
sp(ll.get(i),image1);
i++;
}`
I want to add a difficulty in my game, for example when I click a category like category "A" it will show to the next layout and the user has to pick a difficulty to play "easy, normal or hard" .
sorry for my bad sentence
here is my categories:
public class CategoryActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Button A = (Button) findViewById(R.id.btn_a);
Button B = (Button) findViewById(R.id.btn_b);
Button C = (Button) findViewById(R.id.btn_c);
Button D = (Button) findViewById(R.id.btn_d);
Button E = (Button) findViewById(R.id.btn_e);
Button F = (Button) findViewById(R.id.btn_f);
Button G = (Button) findViewById(R.id.btn_g);
Button H = (Button) findViewById(R.id.btn_h);
Button I = (Button) findViewById(R.id.btn_i);
Button J = (Button) findViewById(R.id.btn_j);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg1) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
C.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg2) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
D.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg3) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
E.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg4) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
F.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg5) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg6) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
H.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg7) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
I.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg8) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
J.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg9) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
}
You should use radio buttons, so the user can select only one option and not two or more.
This is an example taken from the official android devs website.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
PS: You're question is not really clear. It makes it difficult to help you. I hope my answer is acceptable. Refer to the manuals and do some tutorials.
It says Expression expected but no recommendations as to what symbol is missing. I'm using android studio. I suppose it's a simple fix I'm just not sure what symbol.
Heres my code:
public class CloudActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
},
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
}, // Where the problem is
));
}
#Override
public void onClick(View v) {
}
}
You are closing your anonymous inner classes (new View.OnClickListener() {) wrong. You shouldn't be separating them with a comma but closing them off with a );.
For example,
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
match up your {} and () for all of them. So you will want to do the same thing for button3.setOnClickListener(new View.OnClickListener() { and any others you might have.
You can read more about them in the Java Docs
Try This. You weren't closing your inner classes properly. I imagine you accidentally put commas in place of semi-colons?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cloud);
Button genderButton = (Button) findViewById(R.id.genderButton);
Button button3 = (Button) findViewById(R.id.button3);
genderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CloudActivity.this, LoginActivity.class);
startActivityForResult(intent, 0);
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent tnt = new Intent(CloudActivity.this, LogActivity.class);
startActivityForResult(tnt, 0);
}
});
}
I'm developing an APP for a schoolwork. When I launch my app, I configured 4 buttons, one of them is a calendar.
What I want is when I click on Calendar, this open the Google calendar; or if I don't have installed it, take you to Google Play.
The other buttons are activities in the same application, but these are solved.
The main activity:
public class MainActivity extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
entrarboton();
}
private void entrarboton() {
ImageButton accionentrar = (ImageButton) findViewById(R.id.imageButton0);
accionentrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Calendari.class));
}
});
ImageButton accionentrar2 = (ImageButton)findViewById(R.id.imageButton3);
accionentrar2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Notes.class));
}
});
ImageButton accionentrar3 = (ImageButton)findViewById(R.id.imageButton2);
accionentrar3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Apunts.class));
}
});
ImageButton accionentrar4 = (ImageButton)findViewById(R.id.imageButton4);
accionentrar4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Altres.class));
}
});
}
}