This is my code:
AlphaAnimation anim_fadeIn;
Button button, button2;
TextView t, e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
t = findViewById(R.id.text_Splash_t);
e = findViewById(R.id.text_Splash_e);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
anim_fadeIn = new AlphaAnimation(0.0f, 1.0f);
anim_fadeIn.setDuration(1000);
anim_fadeIn.setFillAfter(true);
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
Scenario:
press button1 and text1 will animate (even if you do it some times). Then pressing button2 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate
Scenario 2:
press button2 and text2 will animate (even if you do it some times). Then pressing button1 will add the view somewhere so no matter if you press the button1 or 2, both Texts will animate.
How can I avoid this problem
Remove new Thread wrap, just call mAnimate() or mAnimate2() in main thread like this:
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAnimate2();
}
});
...
Also mAnimate, mAnimate2 can be optimized like this:
private void mAnimate() {
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
e.startAnimation(anim_fadeIn);
}
What you actually do is you register the same animation to two Views. If you want to only animate one view at the same time you have to clear the Animation for the other View at first otherwise both will start. E.g.
private void mAnimate() {
e.clearAnimation();
t.startAnimation(anim_fadeIn);
}
private void mAnimate2() {
t.clearAnimation();
e.startAnimation(anim_fadeIn);
}
Related
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);
}
}
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 have a counter program for number. I define 3 buttons for Plus, Minus and Clear.
When I use clear button for clear TextView it's good. But after using Plus and Minus it it is Continuation last counter. that is my code.
please Help me.
public class CounterActivity extends Activity {
private Button btnPlus;
private Button btnMinus;
private Button btnClear;
private TextView txtCounter;
int counter = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counter_menu);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnMinus = (Button) findViewById(R.id.btnMinus);
btnClear = (Button) findViewById(R.id.btnClear);
txtCounter = (TextView) findViewById(R.id.txtCounter);
btnPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter++;
txtCounter.setText(counter + "");
}
});
btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
counter--;
txtCounter.setText(counter + "");
}
});
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
txtCounter.setText(0 + "");
}
});
}
}
Change your clear button onClick method as follows:
btnClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
counter = 0;
txtCounter.setText(0 + "");
}
});
Im a Beginner in Java and Android Studio. With this Code I try to change the layout in this activity. The current layout is "marcelscorpion_1".
Only the Buttons "weiter_1" and "zurück_1" are working and I don't know why...
public void SwitchLayout()
{
Button weiter_1 = (Button) findViewById(R.id.marcelscorpion_weiter1);
Button zurück_1 = (Button) findViewById(R.id.marcelscorpion_zurück1);
View marcelscorpion_2 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_2, null);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
View marcelscorpion_3 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_3, null);
Button weiter_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_weiter3);
Button zurück_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_zurück3);
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
weiter_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
weiter_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_3);
}
});
zurück_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_1);
}
});
zurück_3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
}
});
}
Need Help ;) Thank you!
Every time you call setContentView() you should find and setOnClickListener(..) again to all buttons contained in the new layout.
A better way is define onclick attribute in the layouts xml file.
For instance if you have this:
<Button
android:id="#+id/w2"
android:onClick="getms3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="w2" />
then you should define
public void getms3(View v)
{
setContentView(R.layout.ms3);
}
Just add your buttons of that layout when you are doing setContentView() in onClick() methods like this :
weiter_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.marcelscorpion_2);
Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);
}
});
I am writing my first android application, and am trying to write an app where you simply click a button to display a message, and can do so as many times as you want. So far I have:
public class MyProject extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv = new TextView(this);
Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv.setText("Hello World!");
setContentView(tv);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {setContentView(R.layout.main);}}, 2000);
}
});
}
}
However doing it this way, when I get back to my main screen and click on the button again...nothing happens. How can I get repeated button clicks to repeat the behaviour?
Your example is very simple and, from what I understand, no multithreading is needed.
You just have to initialize your layout one time, after that you can update the content of the single views.
So... this will set "hello world" string on every button click, you will not notice any difference because the string is always the same :D
public class MyProject extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// this is needed one time only
setContentView(R.layout.main);
// add your textview in xml
final TextView tv = (TextView) findViewById(R.id.textView);
final Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv.setText("Hello World!");
}
}
}
}
To do something more fun you can set a counter to update on every click, this way the textview change will be noticeable are more fun!!
int i = 0;
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv.setText("you have reached " + i);
i++;
}
}
Here, you are calling setContentView to replace the entire current view with a TextView containing "Hello World!" Then after two seconds you setContentView again to set it back to the main layout. Since OnCreate is only called once when the activity is created, the OnClickListener is never set again.
This code will do what you are looking for.
public class MyActivity extends Activity
implements View.OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
setContentView(R.layout.main);
Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
init();
}
}, 2000);
}
}
You may notice that OnClick is a separate method in my example. This is because I implemented the interface View.OnClickListener. I was able to write the implemented method as part of my class and then pass this as an argument to setOnClickListener. I'm explaining this because you mentioned you are new to Java.
TextView txt;
// our handler
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
//display each item in a single line
txt.setText(txt.getText()+"Item "+System.getProperty("line.separator"));
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt=(TextView)findViewById(R.id.txt);
}
#Override
protected void onStart() {
super.onStart();
// create a new thread
Thread background=new Thread(new Runnable() {
#Override
public void run() {
for(int i=0;i<10;i++)
{
try {
Thread.sleep(1000);
b.putString("My Key", "My Value: "+String.valueOf(i));
// send message to the handler with the current message handler
handler.sendMessage(handler.obtainMessage());
} catch (Exception e) {
Log.v("Error", e.toString());
}
}
}
});
background.start();
}
}
Note:Handler one type of thread not udpate any UI here
Take an example from here