Cannot lower case button text in android studio - android

I have a trivial question that has been bothering me for a while. I tried to google this but no one seems to have the same problem as me or doesn't see it as an issue. When I make a button in activity_my.xml under layout
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_1_name"
android:id="#+id/button2"
android:layout_marginTop="140dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
I get a button that looks like
even though my strings code is:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="button_1_name">BuTtOn 1</string>
I know I am definitely missing something small here, but how do I get lower case/upper case working in the button text?
Thanks!

You could add android:textAllCaps="false" to the button.
The button text might be transformed to uppercase by your app's theme that applies to all buttons. Check themes / styles files for setting the attribute android:textAllCaps.

<style name="AppTheme" parent="AppBaseTheme">
<item name="android:buttonStyle">#style/Button</item>
</style>
<style name="Button" parent="Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
</style>

This is fixable in the application code by setting the button's TransformationMethod null, e.g.
mButton.setTransformationMethod(null);

It can be done easily by using the following code inside your view in activity_main.xml:
android:textAllCaps="false"
In your case it would be as follows:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_1_name"
android:id="#+id/button2"
android:layout_marginTop="140dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:textAllCaps="false" />
You can also change it for all the buttons in your app by adding this to your styles/themes.xml:
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:buttonStyle">#style/Button</item>
</style>
<style name="Button" parent="Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
</style>
It can also be set Programatically by using the following in mainActivity.kt or mainActivity.java:
Button btn = (Button) findViewById(R.id.button2);
btn.setAllCaps(false);

add android:textAllCaps="false" in xml button
It's true with this issue.

there are 3 ways to do it.
1.Add the following line on style.xml to change entire application
<item name="android:textAllCaps">false</item>
2.Use
android:textAllCaps="false"
in your layout-v21
mButton.setTransformationMethod(null);
add this line under the element(button or edit text) in xml
android:textAllCaps="false"
regards

You could set like this
button.setAllCaps(false);
programmatically

In your .xml file within Button add this line--
android:textAllCaps="false"

There is a property in <Button> that is android:textAllCaps="false" that make characters in which you want in your own Small and caps. By Default its became True so write this code and make textAllCaps=false then you can write text on button in small and Caps letter as per your requirement.
Complete code for a Button which allow use to write letters as per our requirement.
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnLogin"
android:text="Login for Chat"
android:textAllCaps="false"/>

in XML Code
add this line android:textAllCaps="false"
like bellow code
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_1_name"
android:id="#+id/button2"
android:layout_marginTop="140dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
** android:textAllCaps="false" ** />
or
in Java code (programmatically)
add this line to your button setAllCaps(false)
Button btn = (Button) findViewById(R.id.button2);
btn.setAllCaps(false);

I have faced this issue in TextView. I have tried
android:textAllCaps="false",
mbutton.setAllCaps(false);
<item name="android:textAllCaps">false</item>
none of that worked for me. Finally I fed up and I have hard coded text, it is working.
tv.setText("Mytext");
tv is object of TextView. But as per coding standards, it is bad practice.

in XML:
android:textAllCaps="false"
Programmatically:
mbutton.setAllCaps(false);

Related

Setting default background of ImageButton to transparent (using AndroidX)

I'm using the AndroidX support libraries. In my manifest I declare:
<application
...
android:theme="#style/AppTheme">
In values.styles.xml I declare:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
<item name="android:imageButtonStyle">#style/MyImageButton</item>
</style>
<style name="MyImageButton" parent="Widget.AppCompat.ImageButton">
<item name="android:background">#android:color/transparent</item>
</style>
Then I use the ImageButton via:
<ImageButton
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
Unfortunately, my ImageButton shows a grey background instead of the transparent one. What do I have to do to fix it?
You need to replace this line
<item name="android:imageButtonStyle">#style/MyImageButton</item>
with
<item name="imageButtonStyle">#style/MyImageButton</item>
And This is also another way given by #prince
Add this line in your code to
style="#style/MyImageButton"
So,
<ImageButton
style="#style/MyImageButton"
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp" />
or you don't want to add style in your all button tag you can follow this answer to change the style of the default button. Hope it will help you. Try and let me know it works or not.
Thanks.
Happy coding :)
You can simply set background of ImageButton to null as below :
<ImageButton
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#null" />
This will make your ImageButton transparent.

What is the difference between the two syntaxes?

android:textAppearance="#android:style/TextAppearance.Small"
android:textAppearance="?android:textAppearanceSmall"
Both are giving the same result. Is there any difference in there compilation efficiency.
android:textAppearance="#android:style/TextAppearance.Small"
is text style setting directly.
android:textAppearance="?android:textAppearanceSmall"
is the refrence text style that set value on this Activity or this TextView's Theme . Like this:
<style name="MyStyle">
<item name="textAppearanceSmall">#style/textStyle</item>
</style>

How can I change the colour of a Button without changing its size?

I'm quite new to android development and I am having a problem changing the colour of my buttons.
<Button
android:id="#+id/SignUp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textColor="#ff0000" />
<Button
android:id="#+id/SignIn"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#00ff00"
android:layout_height="wrap_content"
android:text="Sign In"
android:textColor="#ff0000" />
This is the button I'm having trouble with. I've changed the background colour but it changes the size of my button.
Just for reference the left button is what I would like my button to look like but after changing the colour the right button is what I get.
I've had a quick look to try find a solution but what I've found just tells me to change the background colour which I've already done.
So how can I get it looking like the left button? I'm assuming its something to do with the drawables, but I honestly have no idea what I'm looking for. Any guides or anything to point me in the right direction would be great.
Thanks!
The most simple answer, use backgroundTint insted background
This code will work:
<Button
android:id="#+id/SignIn"
android:layout_width="0dp"
android:layout_weight="1"
android:backgroundTint="#00ff00"
android:layout_height="wrap_content"
android:text="Sign In"
android:textColor="#ff0000" />
The background attribute can be more than just a color. You're replacing the default image with a solid color. Options are to make your own image, or adjust the height and width attributes.
https://developer.android.com/guide/topics/ui/controls/button.html#Style
You can put your buttons into a LinearLayout view which orientation is horizaional,then set buttons' weight is 1,width is 0dp,linearLayout's height is 40dp.
What you are doing here is replacing an image background with a solid color. You should use styles instead.
Please try this style
<style name="SpecialButton" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#00ff00</item>
<item name="android:textColorPrimary">#ff0000</item>
</style>
in your styles.xml and
<style name="SpecialButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:colorButtonNormal">#00ff00</item>
<item name="android:textColorPrimary">#ff0000</item>
</style>
in your v21/styles.xml and
and apply this style to the button. Please comment if this worked for you, as I have not tested it myself.

Referencing specific attribute in another style

I'm trying to find the best way to match the text size of a label next to an EditText using xml.
I would prefer not to inherit the EditText style directly, since that would bring in a bunch of attributes I'm not interested in.
<EditText
android:id="#+id/editQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textUnit"
style="#style/FontMatchingEditText"
android:text="units"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Is there some way to refer to a specific attribute in another style?
Otherwise, would it make more sense make my own style for EditText to make sure it stays consistent with the label?
Edit
I was hoping there might be a theme independent way of doing it, semantically something like this:
<item name="android:textSize">#android:style/Widget.EditText.textAppearance.textSize</item>
But I'm probably overcomplicating it.
Thanks!
Assuming your parent theme is android:Theme.Holo.Light.
From \android-sdk\platforms\android-X\data\res\values\themes.xml:
<style name="Theme.Holo.Light" parent="Theme.Light">
...
<!-- Widget styles -->
<item name="editTextStyle">#android:style/Widget.Holo.EditText</item>
From \android-sdk\platforms\android-X\data\res\values\styles.xml:
<style name="Widget.Holo.EditText" parent="Widget.EditText">
</style>
<style name="Widget.EditText">
...
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<item name="android:textColor">?android:attr/editTextColor</item>
</style>
Now you can just reuse theme attributes used for EditText for your TextView:
<TextView
android:id="#+id/textUnit"
android:textColor="?android:attr/editTextColor"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:text="units"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Problem with Android togglebutton

I tried to set up my togglebutton properties layout using style.xml.
Here's my code:
<ToggleButton android:id="#+id/button_toggle_1" style="button_test_1"/>
<ToggleButton android:id="#+id/button_toggle_2"
android:textOff="test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ToggleButton android:id="#+id/button_toggle_3" style="button_test_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
For ID=button_toggle_1, I apply a style (style.xml), here's the code:
<style name="button_test_1">
<item name="android:textOff">test1</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="button_test_3">
<item name="android:textOff">test3</item>
</style>
I can see button 2 and 3 but the text for button 3 is different from what I set it to be. button 3 text = "APAGADO". From this test, I conclude that togglebutton simply cannot set its properties using style.xml. Is this true? or did I do something wrong?
If toggle button is "special", anyone knows the reason why? is this "special" condition applies to other things as well?
Thanks!
You are not referencing the styles correctly. It is style="#style/style_name". See documentation.

Categories

Resources