Android:- change the text on buttons sequentially using animation - android

I have a sequence of buttons in android and I need to change the text of those buttons sequentially (slowly one by one) How can I achieve this? I cant apply any animation here?

Its really hard to tell exactly
what you want to do here but one option could be to use a TimerTask You could repeat changing the text of the buttons at whatever intervals you want. You could use a for loop to iterate over the buttons

Handler myHandler = new Handler();
// your buttons something like this
int[] ButtonArray = {R.id.button1,R.id.button2.....};
String[] stringArray = {"Hi","Hello","oi"....};
// get all the button
private Button[] myButtons=new Button[buttonArray.length];
for(int i = 0; i < buttonArray.length ; i++){
myButtons[i] = (Button) findViewById(mAlphabetsId[i]);
}
//Handler to do repetitive task
.................
Start The repetitive task
counterValue = 0;
Size =buttonArray.length; //Number of buttons
myHandler.postDelayed(mMyRunnable, speed);
.............................
private Runnable mMyRunnable = new Runnable()
{
public void run()
{
if(counterValue<Size){
myButtons[counterValue].setText(stringArray[CounterValue]);
myHandler.postDelayed(mMyRunnable, 1000); //Call again with 1 sec delay
counterValue++;
}else{
myHandler.removeCallbacks(mMyRunnable);
counterValue=0;
}
}
};
This code may contain errors since I did it in a hurry. Try it first.
Let me know if you get stuck.

Related

How to display three list view in loop one by one automatic?

I want to display three ListView in loop one by one, with different data after 30 sec interval change to next list view and auto scroll in each and every list view.
Any help regarding this would be really appriciated!
You firstly need to loop statement in which you'll loop through the handler. Then you'll set a delay using the handler (note I've set the delay for 30000ms which is equivalent to 30 seconds; therefore, adjust the figure to your requirement).
for (int i = 0; i < 3; i++) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i == 0){
//This will be the first loop
}else if(i == 1){
//This will be your second loop
} else{
//This will be the third and final loop
}
}, 30000);
}
You should write a method to set your listview and trigger that method within the run() function. But you can customize that functionality based on your own requirements/how you best understand it. Additionally, because you are going to use different parameters for the listview, you'll have to go through if statements to determine at which part of the loop you are as this will determine which listview is going to be set now.
For multiple view types you can use RecyclerView with multiple view types.
Recycler view has more uses over ListView.
Please check example https://www.journaldev.com/12372/android-recyclerview-example

How to add pause when adding item to adapter

I`m new here. Here is my question. I have an array list and adapter (RecyclerView). I want to make pause after inserting one item after another. If I add one item, animation goes fine. And even when I send multiple items to adapter it is not look terrible, but to the usability concerns, I want to add them one by one with pause. What I did. If I do this code in my fragment, no pause at all. Items appear in one moment.
for( int i = 0; i<filteredList.size();i++){
try {
mAdapter.add(filteredList.get(i));
Thread.sleep(350);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
If I add similar code to adapter, it stacks delay and then all items appear same way. May be I missed something?
It seems that handlder is the solution.
int dlay;
Handler handler = new Handler();
for( int i = 0; i<filteredList.size();i++){
final int finalI = i;
handler.postDelayed(new Runnable() {
#Override
public void run() {
mAdapter.add(filteredList.get(finalI));
}
}, dlay);
dlay = dlay + 350;

Not able to start multiple threads in my application

I am trying to create a simple wack-a-mole in android. I created an array of buttons. When users clicks "start" I want to start a thread for each button . This thread will toggle the button in on and off state. I coded the application in swing(java) it works well. I am trying the same in android but it is creating problems. Please suggest what is wrong. I am starting a thread for each button but the application terminates. If I just start only one thread for any particular button then it works fine.
EDIT: Even after hard coding the buttons thread it is crashing on the click. Nothing is displayed in the logcat.
Creating the buttons : all buttons are intialized I am able to click on them and their action perform is working. Code written in oncreate method.
int j=0;
for (int k = 0; k <ROWS; k++) {
TableRow row = new TableRow(this);
//inner loop:
for (int l = 0; l < COLS; l++) {
btn[j] = new Button(this);
TableRow.LayoutParams tr = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
layout.setWeightSum(12.0f);
tr.setMargins(left, top, right, bottom);
btn[j].setLayoutParams(tr);
row.addView(btn[j]);
j++;
}
EDIT: COde written on onclick() listener of start(some other button from above created buttons) button. If I start only one thread the application works, but crashes if I ret to create mutiple thread. Is it not the correct way of creating thread? I sont think ASYNCTASK is useful here but comments are welcome.
start.setOnClickListener(new Button.OnClickListener() {
EditText timer = (EditText) findViewById(R.id.time);
// Called when user clicks the start button
public void onClick(View v) {
Log.i("thread started for ", btn[0]+"");
ButtonsThread bt2 = new ButtonsThread(btn[0]);
bt2.start();
ButtonsThread bt3 = new ButtonsThread(btn[1]);
bt3.start();
ButtonsThread bt4 = new ButtonsThread(btn[2]);
bt4.start();
ButtonsThread bt5 = new ButtonsThread(btn[3]);
bt5.start();
ButtonsThread bt6 = new ButtonsThread(btn[4]);
bt6.start();
}});
Please suggest why this for loop is crashing the application.
Question 2: When I am creating the buttons in oncreate() method I want to attach a listener to them.
I created a class implementing onClickListener() and attached its object on all the buttons :
// this was the wrong code which I corrected now.
for (int k = 1; k < 10; k++) {
TableRow row = new TableRow(this);
//innerloop:
for (int l = 1,j=0; l < 4; j++,l++) {
btn[j] = new Button(this);
btn[j].setEnabled(true);
//listner class
Score scoreListener = new Score();
btn[j].setOnClickListener(scoreListener);
row.addView(btn[j]);
this is also causing the application to fail if I click any button.
This is the code of Thread class:
class ButtonsThread extends Thread {
Button moleButton;
ButtonsThread(Button b) {
this.moleButton = b;
Log.i(TAG,"buttone thread created"+b);
}
public void run() {
while (timerInLong > 0) {
Log.i("while","while loop ");
try {
MapLocation.this.runOnUiThread(new Runnable() {
#Override
public void run() {
moleButton.setEnabled(true);
moleButton.setText(":-)");
}
});
moleButton.runOnUiThread(new Runnable() {
#Override
public void run() {
moleButton.setEnabled(true);
moleButton.setText(":-)");
moleButton.setBackgroundColor(Color.BLUE);
}
});
Thread.sleep(1000);
moleButton.post(new Runnable() {
#Override
public void run() {
moleButton.setEnabled(false);
moleButton.setText(":-(");
moleButton.setBackgroundColor(Color.RED);
}
});
Thread.sleep(1000);
You're not consistent in your treatment of how many buttons are in your array:
In the "working" code you use btn[0].
In the "problem" loop you use btn[1], btn[2], btn[3], btn[4].
In the button creation loop you initialize btn[0], btn[1], btn[2] only, but you repeatedly reinitialize those elements with 9 different buttons each.
It's hard to make sense of this but it seems you're leaving btn[3] and btn[4] uninitialized, probably causing a NullPointerException when you start using them. (You've neglected to say what exception you're getting...)
Be careful with your loop conditions. Unless you want to treat specially the first element of an array you shouldn't write a loop of the form:
for (int k = 1; k < N; k++)
Much more often you'd want to start at 0:
for (int k = 0; k < N; k++)
or for some uses, start at 1 and use <= as the boundary condition:
for (int k = 1; k <= N; k++)
I can't answer your question 2 because you say you've post the score listener code and you haven't.

How to display 3 2 1 text in the center of the screen in transparency mode

First of all, I am new to android development and searched everywhere but didn't get any solution to my problem. So please don't rate this post negative.
I am making a small game application in which I want to display 3 2 1 at the center of the screen in bixtext, and it should come in transparency mode such that the activity below the
3 2 1 text should be visible and when it reaches 1 then the activity should start like in any game.
Add a TextView to your xml layout (textView123 here) and that wouldn't be so hard to go:
final Handler handler = new Handler();
final TextView textView = (TextView) findViewById(R.id.textView123);
final java.util.concurrent.atomic.AtomicInteger n = new AtomicInteger(3);
final Runnable counter = new Runnable() {
#Override
public void run() {
textView.setText(Integer.toString(n.get()));
if(n.getAndDecrement() >= 1 )
handler.postDelayed(this, 1000);
else {
textView.setVisibility(View.GONE);
// start the game
}
}
};
handler.postDelayed(counter, 1000);
As for making the Countdown activity transparent - getWindow().setBackgroundDrawable(null).
What you can do is when you launch the timer to countdown, when it finishes the 1 countdown, create a transition to the next activity with a fade-in transition.
Though i don't understand why you don't use a frameLayout and the countdown timer as just a view on the same activity. That would certainly be a better approach.

Animating LinearLayout background to blink in Android

The question is very simple but I can't manage to find a good solution to it. I have a LinearLayout in my activity. Depending on what the user does I need to make the background of my Layout blink 3 times. This means it will change the Background Color from transparent to Red and backwards for 3 times. Let me give you an example:
the user receives a question and 2 buttons with answers
the user presses the wrong answer. The layout containing the button will change it's background (Transparent - Red, Transparent - Red, Transparent - Red - Transparen) three times.
How can I make this in Android ? Thank you.
You could use a Handler with the postDelayed method. That would look something like this:
Handler h = new Handler();
int count = 0;
Runnable r=new Runnable()
{
public void run()
{
if(count < 6){
if(count % 2 == 0){
count++;
layout.setBackground(RED);
h.postDelayed(r,500);
}else{
count++;
layout.setBackground(TRANSPARENT);
h.postDelayed(r,500);
}
}
}
};
h.post(r);
#vidstige: I have tried your suggestions but the background changing wasn't fluent, sometimes it worked sometimes only half sometimes it didn't
#Tim: your solution seems very good but I had some issues with it making it work and as I'm not really good at java I gave up.
The solution that works is a combination of principles of both answers. I'm not sure if it's the best one but works very good in my case
Handler blinkHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
lay1.setBackgroundColor(Color.RED);
break;
case 1:
lay1.setBackgroundColor(Color.TRANSPARENT);
break;
}
super.handleMessage(msg);
}
};
for (int i=0; i<6; i++)
{
Message msg = new Message();
if(i % 2 == 0){
msg.what = 0;
}
else{
msg.what=1;
}
blinkHandler.sendMessageDelayed(msg, i*300);
}
Thank you all for your time.
Use a ScheduledThreadPoolExecutor to schedule an action to change the background color using View.setBackgroundColor(int color) to red/transparent.
Using the schedule(Runnable command, long delay, TimeUnit unit) method you can schedule all the color changes at once and they will be executed later in the correct order.
To make a color blink I remember there was a specific bit we used to set in C++ graphics library, in University days. I'm also trying to blink the background but I don't want to dedicate a thread for that purpose.
And if there's no alternate at all, I'll try to put a .gif image with alternating colors in the background.

Categories

Resources