I need to see if there is a way where I can test for what background a button has. For example, here is my pseudo code:
if (button background is `R.drawable.black`) {
button.setBackground(to R.drawable.white)
}
Is there a way that this if statement can be done? I know how to SET backgrounds, just curious on the test portion.
In your case, I believe that tags would be useful. Try this:
//When you set the button:
btn.setBackgroundResource(R.drawable.black);
btn.setTag(R.drawable.black);
//When you re-set the button:
if(btn.getTag().equals(R.drawable.black)) {
btn.setBackgroundResource(R.drawable.white);
btn.setTag(R.drawable.white);
}
There is a getBackgroud method
Drawable buttonBackground = button.getBackground();
Related
everyone. After clicking on an ImageView, I would like to change it depending on what kind of drawable is currently displayed. To do this, I compare the currently set image in the ImageView with one from the drawable folder. Unfortunately, it is currently the case that only the else branch is executed. The image changes after the first click, but then it no longer switches. What am I doing wrong?
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
var draw = bookmark_site.drawable.constantState
if(draw == ContextCompat.getDrawable(this,R.drawable.ic_baseline_bookmark_filled_24)?.constantState)
{
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_outline_bookmark_border_outline_24))
}
else{
//Only this two is ever executed
bookmark_site.setImageDrawable(resources.getDrawable(R.drawable.ic_baseline_bookmark_filled_24))
}
}
you can't base on constantState, its not desired to compare drawables as there is a different instance for every drawable, thus your if isn't true never ever. in fact there is no way for getting resource of drawable already set for ImageView. most convenient way would be to keep some boolean flag outside onClick method informing that image is switched or not. optionally you may keep this flag "inside" ImageView using setTag(...) method
bookmark_site.setOnClickListener{
vibrator.vibrate(50);
val switched = bookmark_site.tag != null
bookmark_site.setImageResource(
if (switched) R.drawable.ic_outline_bookmark_border_outline_24
else R.drawable.ic_baseline_bookmark_filled_24
)
bookmark_site.tag = if (switched) null else "SWITCHED"
}
you should keep category or tag along with the image in the model class and then change the image on the basis of that tag or category.
I am new to Android development. I am currently building an application that must flash different colors when a button is clicked. When the button is clicked I call a function. This function loops through a list of items and basically at the moment attempts to show blue and green after each other a couple of times. Problem is that it only shows the last color. And this only happens when the thread has exited the method. In the example below it is blue. I have noticed with some of the work I have done that screen changes reflects once the program have left the current method where the loop occurs and sometimes even longer after that. Below is the code :
//Method that sets the color
public void SetVisualLayoutColor(int Color)
{
linearLayout.setBackgroundColor(Color);
linearLayout.invalidate();
linearLayout.refreshDrawableState();
}
//Method that loops and calls above method to set color
public boolean ShowRepititive()
{
boolean successfull = false;
try
{
boolean isGreen = true;
//for (TimingItem timingItem : items) {
for (int i=0;i<=10;i++) {
if (isGreen) {
SetVisualLayoutColor(Color.BLUE);
isGreen = false;
} else
{
SetVisualLayoutColor(Color.BLUE);
isGreen = true;
}
}
successfull = true;
} catch (Exception e)
{
Log.e("Repeating Flash", "showFlashRepititive: ", e);
successfull = false;
}
return successfull;
}
Is there some way to get past this or to force the changes to occur timeously ?
Thank you in advance.
Unless you explicitly run your code on a different thread, the code you write in an Activity, Fragment, or View will run on the UI thread.
When you are performing work on the UI thread, the UI cannot update. The UI only updates after your code has finished executing. Thus your for loop blocks any UI updates until it completes, showing the last color you set.
A more appropriate pattern to use would be to use an animation to change the color
Here is an example of a very basic animation that does a cross-fade between two background colors:
ObjectAnimator.ofObject(view, "backgroundColor", new ArgbEvaluator(), 0xFF0000, 0x00FF00)
.start();
Obviously this won't achieve the flashing effect you are looking for, but you can define your own animations in a similar manner to achieve the desired effect.
As Tanis.7x said, you can't create the desired animation while your code is executed in the main thread. All potential solutions have to have a background Thread and a way for it to communicate with your UI thread. (see AsyncTasks,Threads,Handlers)
An alternative to ObjectAnimator that can work in your specific case is TransitionDrawable. It's not that powerful but if it suits your needs, it's preferable since it's API 1 compatible:
You need to store a transitioncolors.xml in your drawables folder with your transition definition:
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green"/>
<item android:drawable="#color/blue"/>
</transition>
and then call:
ImageView view = (ImageView) findViewById(R.id.transitionId);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TransitionDrawable transition = (TransitionDrawable) v.getBackground();
transition.startTransition(3000);
});
where R.id.transitionId is:
<ImageView
android:id="#+id/transitionId"
android:background="#drawable/transitioncolors"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
I did some more research after I read Tanis.7x and Sevle's post, which put me in the right direction. For some reason the Object animator did not worked when I ran it and the Transition does not give me the flexibility I wanted. So I came across the ValueAnimator, which worked perfectly for me. I created my own class that extended the ValueAnimator and within the class I control how long each animation must be and also other aspects like running indefinitely until the user interrupts it on the screen. Thanks a lot guys, I really appreciate the help.
How would I model a follow/unfollow button in Android? Is State List Drawable what I am looking for? How can define the two different states?
This is highly dependant on the visual effect you want to achieve.
First, the basics (I assume you already know this): you need a Button, setOnClickListener() for a listener that toggles the state, and someplace to save this (either a local database, your own server, or whatever).
The easiest, least attractive way, would be to just switch the text on the button whenever the "follow" state changes (with setText() in the click listener).
A possible improvement would be to have different button backgrounds, so that the appearance also changes (say, from a grey star to a yellow one). For this, you just need to call setBackground() in the click listener.
For a fancier effect, you can use a TransitionDrawable to crossfade this change. startTransition() and reverseTransition() would then be used for the two state changes. For example:
Drawable d1 = getResources().getDrawable(R.drawable.follow_button);
Drawable d2 = getResources().getDrawable(R.drawable.unfollow_button);
final TransitionDrawable followDrawable = new TransitionDrawable(new Drawable[] { d1, d2 });
final int transitionDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
button.setBackground(followDrawable);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (!mFollowing)
{
mFollowing = true;
followDrawable.startTransition(transitionDuration);
}
else
{
mFollowing = false;
followDrawable.reverseTransition(transitionDuration);
}
}
});
First, you need to save your state. Whether it is "follow" or "unfollow". Where to save depends on your application logic.
Then, according to the current state, you can update the appearance of the button. What StateListDrawable does is, it update the background of your button based on the state whether it is "focused", "selected", etc.
You can use State List Drawable as a background of your button. Then, if the current state is "follow", you set your button as selected (vis Button.setSelected(true)). If the current state is "unfollow", it will be normal. (Button.setSelected(false).
<item android:drawable="#drawable/bg_for_follow" android:state_selected="true" />
<item android:drawable="#drawable/bg_for_unfollow" />
Something like this. In above example, if you set that drawable file as the background of your button, it would use "bg_for_follow" when the button state is "selected" and it would use "bg_for_unfollow" when the butotn state is normal.
Hope that could help.
I have this piece of code, which is expected to change the color of the button to BLUE, but it does not have any effect.
The code goes into this if statement but does not change the color. On the other hand , the same statement when used earlier does actually change the color of the button. Why is this so?
if(t.equals("a"))
{
Toast toast5=Toast.makeText(getApplicationContext(),"a found", Toast.LENGTH_SHORT);
toast5.show();
btn6.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
}
// Get Handle for the Tab buttons
Button btnTab1 = (Button) findViewById(R.id.button_tab1);
Button btnTab2 = (Button) findViewById(R.id.button_tab1);
// set the colors correctly
btnTab1.setBackgroundResource(R.color.lightblue);
btnTab2.setBackgroundResource(R.color.darkblue);
Use
btn6.setBackgroundColor(Color.BLUE);
You implement for this code,
btn6.setBackgroundColor(Color.colorChoose);
btn6.setBackgroundDrawable(Drawable drawable);
btn6.setBackgroundResource(int resid);
In my application, I have recording button. I want when user clicks on it each one second i change the background in order to simulate blinking. I created a handler and set it to 1 second therefore each one second this handler runs. Here i change the background. this my code:
mUpdateUITimerTask = new Runnable() {
public void run() {
// Simulating blinking for capture button
if(bolToggle) {
bolToggle = false;
captureButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_record_blink));
} else {
bolToggle = true;
captureButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_record));
}
mHandler.postDelayed(mUpdateUITimerTask, 1000);
}
};
When I run the app i see the changes but its not clear. buttons are like this:
When i run the application, red image is showing ok but for white image, it shows red image with a little white halo around it.
I tried to put captureButton.setBackgroundColor(Color.TRANSPARENT); before setting background but result was same.
any suggestion would be appreciated. Thank you.
Found the answer you need: https://stackoverflow.com/a/4852468/1352556
Basically you want an alpha animation. I believe this will make the entire button flash however, do you only want the red dot flashing?