transparency of button is not changed using 'setAlpha' - android

I am developing a simple application ,in that there is set of 6 buttons.
when i clicked on one of these button then the other buttons must be partially transparent. I tried to done that By setting alpha of 5 buttons
acre_button.getBackground().mutate().setAlpha(155);
Ui of the application not changed as i expected. i got only 3 out of 5 is get transparent.when clicking on that two button it is slowly changing it's transparency
Thanks in advance
regards,
kariyachan

Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.main_btn);
Drawable d = getResources().getDrawable(R.drawable.imagen);
d.setAlpha(60);
btn.setBackgroundDrawable(d);
}
This works for me :)

Find a button background in your android-sdk directory here: android-sdk\platforms\android-10\data\res\drawable-mdpi\btn_default_normal.9.png
You can modify it to make it semi transparent (Please note that this is a 9-patch and you shouldn't change the opacity of the black lines).
Once you have this changed button in your drawable directory you can add this to your code:
button.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparentImage));
to make it semi transparent and
button.setBackgroundDrawable(getResources().getDrawable(Android.R.drawable.btn_default_normal));
to change it back.

For anyone still hunting for a solution to this:
The method setBackgroundDrawable(Drawable d) is deprecated as of API 16
Assuming your button's id is buttonId and your drawable is named button_img,
handle this by using the following within the onCreate method:
((Button)(findViewById(R.id.buttonId))).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Drawable d = v.getResources().getDrawable(R.drawable.button_img);
d.setAlpha(40);
if (Build.VERSION.SDK_INT >= 16)
v.setBackground(d);
else
v.setBackgroundDrawable(d);
//Then call your next Intent or desired action.
}
});
Tested and works for me!

Try the following:
button.setBackgroundColor(android.R.color.transparent);

Related

Android compare ConstantState does not work on 4.4 KitKat

I developed an app and everything works great on my s7 and other devices, but I tested it on an older one running 4.4 kitkat and when the app compares the buttons' colours, in 4.4, it doesn't work.
This is the piece of code I'm using:
for (Button btn : selectorArrayList) {
final Button button = btn;
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*
Check if button color is white or red
if white, turn red and activate the
selector (category) and if it's red
turn white and deactivate the selector
category)
*/
// I've set the background in the XML Layout as R.color.white
Drawable pd = (Drawable) button.getBackground();
if (pd.getConstantState().equals(ContextCompat.getDrawable(getContext(), R.color.white).getConstantState())) {
...
At this very point on a newer version of the OS it decides the states are equal, and on 4.4 (haven't tried on other versions) they're not.
Does anyone know if I'm doing something wrong here? Thank you.
Try this, should work:
if (pd.getConstantState().equals(new ColorDrawable(R.color.white).getConstantState()))

change image with OnClick

I need some help,
I have one ImageButton that plays and stops a tune, I want the button to change to a stop symbol when playing and then back to a play symbol when stopped. So far I have the symbol and tune playing when the ImageButton is clicked the first time, but when it is clicked the second time, the tune stops but the image does not change, any advice?
mPlayTune.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tuneMp.isPlaying()) {
tuneMp.stop();
tuneMp.prepareAsync();
mPlayTune.setImageResource(R.drawable.ic_av_play_arrow);
}else
tuneMp.start();
mPlayTune.setImageResource(R.drawable.ic_av_stop);
}
});
Couldn't you just make 2 objects and hide or show them by clicking them? It's not a clean implementation but for sure a workaround.
I solved it, it was simply changing
tuneMp.start();
mPlayTune.setImageResource(R.drawable.ic_av_stop);
to
mPlayTune.setImageResource(R.drawable.ic_av_stop);
tuneMp.start();
this way it sets the icon before hitting tuneMp.start();

Android Button color not changing programatically

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);

What makes a button switch and keep color on click?

I want to create a Button that behaves like a switch.
It should change its color when the user clicks it, and keep the color.
So the button is white at first and when the user clicks it, the color changes to black. When the user clicks it again it switches back to white and so on.
I tried it with a simple if else construct but only managed to get the button to be white at first and when being clicked change to black, but it won´t change back to white when clicked again.
Here´s the code so far. I guess it´s a dumb simply mistake but can´t seem to get through with it. "changecolor" is a variable I declared myself.
// Select Button Safe or At-Risk
final Button button7 = (Button) findViewById(R.id.SafeBT);
button7.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks, change color
if (changecolor == 0) {
button7.setBackgroundColor(color.black);
changecolor = 1;
} else {
button7.setBackgroundColor(color.white);
changecolor = 0;
}
}
});
Tanks for advice and help in advance.
where you have declared your variable changecolor?? .
Second thingis that you can simply an UI Element which call it : ToggleButton , it is like a Switch Button ON/OFF . is that what you want ? see this link : http://developer.android.com/reference/android/widget/ToggleButton.html

ImageView onClickListener changing the image source

Im working on changing the image being shown when I have my ImageView Clicked. Im trying to use a similar code that I used for accomplishing this with a TextView but I can't seem to find the right terms to get it to work. Here is my current code. Thanks
electronconfiguration.setOnClickListener(new View.OnClickListener() {
public void onClick(View drawable) {
if (drawable.equals(R.drawable.element_el))
electronconfiguration.setImageDrawable(R.drawable.aluminum_el);
else if (drawable.equals(R.drawable.aluminum_el))
electronconfiguration.setImageDrawable(R.drawable.element_el);
}
});
Why don't you use a ViewSwitcher, it's designed to switch between two views
drawable probably doesn't equal R.drawable.element_el. R.drawable.element_el is probably some random implementation of the image. Try drawable.getId().equals(R.drawable.element_el). I've never tried this so I have no idea

Categories

Resources