Text color of selected button android - android

I have a button like so:
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:textColor="#color/my_gray" />
After pressing the button, the text color turns a darker gray color (a "this has already been pressed" color). How do I prevent this? On the button press I do button.setTextColor(R.color.my_gray); to reset the color, but it has no effect.

Try using a different kind of button. CompoundButton or ImageButton might work out for you since you can control their backgrounds/images more easily.
However, the best way to do it might be to use a selector like this guide shows.

Related

Android Button changing color and incrementing size

Hi guys im new in android stuff and i have a problem with a button when color change, because when it changes the button grows a bit and i dont know why. My code of the button click is below.
produzirbt.setBackgroundColor(Color.rgb(38, 198, 219));
Toast.makeText(getApplicationContext(), "Avaria iniciada.", Toast.LENGTH_SHORT).show();
Xml
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Pausa"
android:id="#+id/Pausa"
android:textSize="50px"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_above="#+id/Avaria"
android:layout_toRightOf="#+id/terminaliniciado"
android:layout_toEndOf="#+id/terminaliniciado" />
In order to keep button size use setColorFilter() method.
In your case:
produzirbt.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.your_color), PorterDuff.Mode.SRC_ATOP);
Your button by default has a backgrond definded by some xml file. It contains backround color, corner radiuses and padding. Then in java code you change this style to plain color and padding becomes zero. To prevent size changing you should define base background with a color. Like this:
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Pausa"
android:id="#+id/Pausa"
android:textSize="32sp"
android:background="#color/my_button_color"
android:layout_above="#+id/Avaria"
android:layout_toRightOf="#+id/terminaliniciado"
android:layout_toEndOf="#+id/terminaliniciado" />
A the same time using color for button is a hacky method. It'll be better to define normal style as the documentation says.

Android SDK: Button becomes colored when changing the background color on LinearLayout

I have a LinearLayout in an Android app I am creating now which contains a default button with gradient gray color. This LinearLayout is now white but when I try to change the background color to yellow the button also becomes yellow which I don't want to happen. I also tried to use a color image to set the background color on the LinearLayout but the same thing happens. How do I solve this problem? Here is the code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:background="#color/yellow">
<Button
android:id="#+id/buttonCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
SOLVED: Wait, it actually works now. while the buttons become colored and transparent in the layout view in Eclipse the buttons is unaffected when I run the app on the phone. I thought that it would display the same result when the app is runned. Strange how it become that way in the layout view...
In that case you may set a gray background color android:background="#A4A4A4" for button also..
Just set the background on your button separately. You can use a state list drawable, i.e. a different background for each state of the button (pressed, focused, etc) to make it compeletely custom.
Why the background color changes with the layout I'm not sure. It sounds like your button is default to transparant. Check your style and theme to make sure that's not affecting what's displayed.

Making a semi-transparent button such that button color should be visible and background should be visible

I have been trying to achieve this semi-transparent button, but I always failed on making that. I have referred many like this. But still no luck. I tried with android:color="#66FF0000" too, but it doesn't make it semi-transparent. Below is my code.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="select"
android:onClick="selectClick"
android:color="#80000000"
/>
I want this type of rectangular button with semi transparent so that blue color of button should be visible and green color of the activity should also be visible. Can someone please suggest me? In the below picture I was unable to draw green color on the button to show what exactly I want. But I guess my above explanation is understandable.
Or if the blue color is not possible to be made visible, at least I want it to look like to give a user feel that there is a button and the green color should be visible.
Use
android:background="#80000000"
instead of
android:color="#80000000"

Change ToggleButton background to Clear/Dark Gray

I'm using two toggle buttons on my application, when I run it on the emulator the button has black text, and a light gray background. As we can see in the next image:
When I run it on my real device the button has another style, it has white text, and a black background. As shown in the next image.
My QUESTION IS: How to change the style of a ToggleButton to it always stay with a clear gray background, as in the first image? Is it defined by a Theme?
The xml code used for the button was:
<ToggleButton
android:id="#+id/toggleButtonSimulado"
android:layout_gravity="center|top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off" />
Style of the button or any other view changes in the real device according to the current theme selected on that device. To prevent this you need to specify background color and text color explicitly. Use a gradient as the background of the button. For example:
<ToggleButton
android:id="#+id/toggleButtonSimulado"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gray_gradient"
android:textOn="On"
android:textOff="Off"
android:textColor="#android:color/black" />
You can use your own backgrounds for the buttons.
Search btn_toggle_off.9.png/btn_toggle_on.9 on your sdk install dir(android-sdk-mac_86/platforms/android-9/data/res/drawable-hdpi).
And make sure to copy the selector located on drawable folder
Or you can try:
androiddrawables
androiddrawablesexplorer
Hope this help.

How to make a button non-transparent in Android?

when I just use the normal default button in android, depending on what colour the background of the intent is the colour of the button changes slightly to the colour of the background. I want it so that the button is fully opaque (like when the intent has a white background), how do I do this? thanks.
[it might only do it in android 3.1]
the same button on different backgrounds
You can set the background for a button to any drawable. In XML, do it using the android:background attribute:
<Button
. . .
android:background="#drawable/my_button_bg"
. . .
/>
In code, use the setBackgroundResource method:
Button button = (Button) findViewById(R.id.my_button);
button.setBackgroundResource(R.drawable.my_button_bg);
Your background drawable can be a State List drawable, which you can use to change the background appearance when the button is pressed, has focus, etc.
<Button android:background="#android:color/transparent"
android:text="#+id/Button01"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
By using android:background="#android:color/transparent" the button background will become transparent

Categories

Resources