How to highlight a button without pressing it on Android - android

How can I highlight a button for just a certain amount of time (e.g., 1 or 2 seconds) without actually pressing it?

I assume you want to set focus for a certain period of time....
here's how you can do it:
button01.setFocusableInTouchMode(true);
button01.requestFocus();
In some htc handsets this highlights the button as green in some LG phones Yellow. The focus color is basically the device property.
After this you can apply your logic to set focus to some other object after appropriate time, so button01 will lose focus and be its normal self again.
someOtherView.setFocusableInTouchMode(true)
someOtherView.RequestFocus();

I found the solution to make it works. If someone is interested in creating a sort of karaoke, here is my code:
public void playKaraoke(final FlowLayout fl) {
//KARAOKE
mTts.setLanguage(Locale.FRENCH);
// Do something long
Runnable runnable = new Runnable() {
#Override
public void run() {
for (int i = 1;i<fl.getChildCount();++i) {
final Button btn = (Button) fl.getChildAt(i);
btn.setFocusableInTouchMode(true);
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
//progress.setProgress(value);
btn.requestFocus();
mTts.speak((String) btn.getText(),
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}
});
}
}
};
new Thread(runnable).start();
}

Related

Changing an Image resource

When I'm clicking on a button, I want to change an Image view to different picture, to wait 3 seconds, and to change it again to another picture (without clicking again).
1 click -> change picture -> wait 3 seconds -> change picture.
This is my code:
northLight.setImageResource(R.drawable.red_and_yellow);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {}
northLight.setImageResource(R.drawable.green);
While I'm running the program, when I'm actually clicking the button, the program ignores the first setImage and changes it straight to the second setImage (to the green).
How can I solve this?
you can use handler for it,
northLight.setImageResource(R.drawable.red_and_yellow);
new Handler().postDelayed(new Runnable() {
public void run() {
// Actions to do after 3 seconds
northLight.setImageResource(R.drawable.green);
}
}, 3000);
You can try using a Handler to wait and change the image. When your button gets clicked, change your image and run the handler with a delay of 3 seconds.
//Call this method when your button is clicked
public void changeImage() {
northLight.setImageResource(R.drawable.red_and_yellow);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
northLight.setImageResource(R.drawable.green);
}
}, 3000);
}
Try this code
new CountDownTimer(3000,1000){
#Override
public void onTick(long l) {
northLight.setImageResource(R.drawable.red_and_yellow);
}
#Override
public void onFinish() {
northLight.setImageResource(R.drawable.green);
}
}.start();

Android Studio: Updating multiple buttons' backgrounds one by one when clicking a button

I am writing here because I have a very annoying problem in Android Studio.
My "application" is extremely simple, one page with 13 button; All I want is simple: update the first twelve buttons one by one when I click the 13th button.
I would like to see buttons updating with a little interval between each of them, but I can't understand how to do it.
I tried many tricks inside the "onClick" method but I can not figure out how to solve it; what I obtain is that after some time (the time obtained adding up the various "sleep" I put in the function) all the buttons become colored at the same time.
I put my last attempt, but if you have any other way to do that I am willing to change the way to proceed.
int[] buttonIDs = new int[] {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7,
R.id.button8, R.id.button9, R.id.button10, R.id.button11, R.id.button12 };
int currentI = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button goButton = (Button) findViewById(R.id.button13);
goButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (currentI < buttonIDs.length) {
Button b = (Button) findViewById(buttonIDs[currentI]);
b.setBackgroundColor(Color.parseColor("#FF22FF"));
currentI++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t = new Thread() {
public void run() {
goButton.performClick();
}
};
t.start();
}
}
});
}
The result of this attempt is that the first button become colored and then I get "Application has stopped" error on the Android Emulator.
Thanks in advance
Use this
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 1000ms
}
}, 1000);
The basic idea here is that you need to create a new thread which signals the UI thread to update each button. In between each update, the new thread needs to wait until the UI has a chance to perform the update. I'm not very experienced with this kind of thing. You can most likely do this with a Handler. There might be more high-level tools you can use instead, but I'm not sure.
You definitely do not want to sleep the UI thread. This will only cause the UI to become unresponsive.

Android app forced to close on background colour change?

so I am building an experiment app where the background will change colour at random intervals.
I am stuck on the background change.
I have working code that changes the background colour, but when I put it in a thread/ try and catch bracket, the application is forced to close and doesnt give me an error?
Here is the code that works when used in the oncreate method:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.RED);
But when I want to make it "sleep" for 1 second and then change it to red, it bombs out.
Please note that this method is a separate method from the oncreate and is called from within there and will not work for some reason?
public void changeBackground(final View v){
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
v.setBackgroundColor(Color.RED);
}
}
};
timer.start();
}
What am I doing wrong?
What I need:
when the app starts, it must wait for 1 second and then change the background colour without it bombing out.
Thanks in advance!
You cannot access UI thread from your custom thread. You have to run your runnable in UI thread. Change your changeBackground method to the following,
public void changeBackground(final View v) {
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
v.setBackgroundColor(Color.RED);
}
}
};
this.runOnUiThread(timer);
}
Or you can use Asynctask, this handles that issue for you. Here, and here.
Manipulate view state on UI thread:
v.post(new Runnable() {
#Override
public void run() {
v.setBackgroundColor(Color.RED);
}
});
More about it: http://developer.android.com/guide/components/processes-and-threads.html
UI operations can't be done in a Thread
v.setBackgroundColor(Color.RED);
Remove above line from finally and Use a runOnUIthread() to update UI in Finally.
and your final code will look like this
public void changeBackground(final View v){
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch (InterruptedException e) {
e.printStackTrace();
}finally{
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
v.setBackgroundColor(Color.RED);
}
});
}
}
};
timer.start();
}
Solutions to fix this issue:-
Update you App
Clear Gmail Storage Data
Clear App Cache and Data
Reset App Preferences
Reset Smartphone Factory Settings
I found these steps with the help of YouTube.
Here is that link:-
youtube.com/watch?v=fx8Fv8RXag8

Android MediaPlayer custom control panel hide

I have created a custom control panel for a video player. Now I want to give a effect like default MediaController where the panel becomes visible when the screen is touched and it becomes invisible again after the last touch time. I can use this type of code for that.
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// make the panel invisible
}
});
}
};
I can start the thread when the screen is touched and make it invisible after 60 seconds. But in my case, if the user touches the screen again in between this 60 seconds, the panel should vanish after 60 seconds from the last touch. How to consider this case also?
I would recommend using a combination of Runnables and a Handler. You can do Handler calls using postDelayed() to do something after, say, 60 seconds.
Here's an example:
private Handler mHandler = new Handler();
mHandler.post(showControls); // Call this to show the controls
private Runnable showControls = new Runnable() {
public void run() {
// Code to show controls
mHandler.removeCallbacks(showControls);
mHandler.postDelayed(hideControls, 60000);
}
};
private Runnable hideControls = new Runnable() {
public void run() {
// Code to hide the controls
}
};
Simply delete/cancel current timer.
Btw, you should not do it by Thread, but by posting message to a Handler. Such future timer task doesn't need another thread.

How to change the text color continuously in android using TextView...?

I want to change the color of a text after every few seconds. I tried generating a random number and using it for setting the color of the text view object in a loop. But the app is not responding. Can anybody help me with this please?
Try running the color-cycling part of your code in a separate thread. For example
Thread thread = new Thread( new Runnable() {
public void run() {
while(true) {
int number = // generate random number ;
runOnUiThread( new Runnable() {
public void run() {
TextView text = // get your TextView ;
text.setTextColor(number);
{
{
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {}
}
}
}
thread.start();
You will need to run the code that actually changes the TextView via the runOnUiThread because Android does not allow other threads to modify parts of an Activity.
You could use a Handler with .postDelayed() and set it up to have a recursive structure, so each time through it will change the color and then post the next runnable to fire off a few seconds later.

Categories

Resources