I have a really weird issue, on every device that I test my application the edittext behaves as you'd expect, the text is highlighted and it is black on a white background.
Specifically on Lenovo Yoga tablets when the edittext is focused the text will become black and blend in with the background.
The layout for this is:
<RelativeLayout
android:id="#+id/ip_layout"
android:layout_below="#id/display_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<LinearLayout
android:layout_marginBottom="10dip"
android:background="#8A8A8A"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="1dp"
android:layout_width="match_parent" />
<EditText
android:layout_marginTop="10dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:hint="#string/Hostname"
android:singleLine="true"
android:imeOptions="actionNext"
android:background="#android:color/transparent"
android:id="#+id/host_address"
android:maxLength="50" />
</RelativeLayout>
If it is a problem on a particular device it may be due to the devices custom theme. Recent Android devices need to support the Holo theme and you can explicitly ask for that theme in your manifest using:
<style name="AppTheme" parent="android:Theme.Holo" />
You'll have to create your own nine patch image and use that as the background of your EditText otherwise befaviour of Edittext will chnage with device and version of android.
If you want to create more effects for edittext backgrounds like pressed, focused and default state then you need to create a selector for background image of edittext:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#android:drawable/Epressed"/>
<item
android:state_focused="true"
android:drawable="#drawable/Efocused"/>
<item
android:drawable="#android:drawable/Edefault"/>
</selector>
Related
I tested my app on my new device with android 5.1.1. In my SettingsActivity i have a switch. I already read some posts and changed it to android.support.v7.widget.SwitchCompat but the problem still is the following: On my old device the switch looks very nice. you can set two texts for textOn and textOff and it fits perfectly. but since api 21 or whatever I get this little bastard here:
It looks like ****. How can I restyle it for all devices like the lollypop switch?
Edit:
The question above is answered.
One tiny thing is missing: How to change the color of the text on the switch.(Not the tag on the left side!!!)
styles.xml
<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#3F51B5</item>
</style>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<android.support.v7.widget.SwitchCompat
android:id="#+id/mySwitch"
android:layout_width="match_parent"
android:switchMinWidth="56dp"
android:layout_height="wrap_content"
android:switchTextAppearance="#style/SwitchTextAppearance"
android:thumb="#drawable/thumb"
android:track="#drawable/track"
app:showText="true"
android:textOn="ON"
android:textOff="OFF"
android:text="Toggle Switch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#android:color/transparent"
android:button="#null"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Using SwitchCompat.setSwitchTextAppearance you can set the style for the text that appears inside the switch itself.
Add a style something like this and set it using setSwitchTextAppearance:
<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#3F51B5</item>
</style>
And you should be able to customize the size, color, etc of your "OFF" text.
I'm new to Android programming and want to create my own piano app.
I use absoluteLayout with dp - or is there a better way?
I have buttons declared like this
<Button
android:id="#+id/d3"
android:layout_width="55dp"
android:layout_height="200dp"
android:layout_x="55dp"
android:background="#drawable/selector_button"
android:scrollbars="horizontal"
android:gravity="bottom|center_horizontal"
android:paddingBottom="20dp"
android:textSize="60dp"
android:textColor="#ff4444" />
selector_button
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/white_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/key" />
</selector>
The problem is that the white key when pressed down draws over the black one
here's a screenshot
I've tried the bringToFront-method on the black key, an the elevation option on it, but haven't been able to solve the problem.
Do you have a hint?
I am trying to create an button which is attached to the TextView above the button as shown in the below image.
The above screenshot is taken from the Note 4 and the OS version is 5.0.1.
Below is the code is used to achieve the UI.
layout/xyz.xml
<Button
android:layout_width="250dp"
android:layout_height="50dp"
android:theme="#style/myButton"
android:text="Cancel"/>
values-v21/style.xml
<style name="myButton" parent="#style/Base.Widget.AppCompat.Button">
<item name="android:colorButtonNormal">#3578A9</item>
<item name="android:inset">0dp</item>
</style>
But if I run the same code in Nexus4 OS verison 5.1.1, the button is taking the margin for all the 4 sides and the screenshot looks like below.
If I remove the "android:theme" and provide the "android:background", the UI looks like the first image. But It won't give the ripple effect. So how the achieve the UI as first image with the ripple effect.
android:insetBottom="0dp"
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="Edit"
app:cornerRadius="0dp"
android:insetBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Step 1: Put the below code in styles.xml
<style name="myColoredButton">
<item name="android:textColor">#FF3E96</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#FF0000</item>
</style>
Here you can change the textColor( I have used #FF3E96 above) and background color (I have used #FF0000) for your button. You can also override textColor values from your Button related layout xml by using android:colorButtonNormal.
Step 2:Create a new XML file under drawables folder and add the following code: I named my XML file as primary.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimary">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="#8B8386" />
</shape>
</item>
</ripple>
Step 3: Use the style and drawable in your Button as follows.
<Button
style="#style/myColoredButton"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Cancel"
android:gravity="center"
android:background="#drawable/primary_round"
android:colorButtonNormal="#3578A9" />
Hope it solves your problem.
I also had issue related to top and bottom spacing for Apply button as you can see above image.
I wanted to show button with uniform background without any spacing as you can see for "Reset" button.
But I have to set both property button background as well as background Tint to same color to achieve this.
If I reuse this buttons at more than one place then I have to programmatically change 2 property to change background color of button.
You can try below solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="#+id/btnLeftOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:filterTouchesWhenObscured="true"
android:gravity="center"
android:text="#string/apply"
android:textColor="#color/white"
app:backgroundTint="#color/primary_dark_gray"
app:cornerRadius="0dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btnRightCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:filterTouchesWhenObscured="true"
android:gravity="center"
android:text="#string/reset"
android:insetTop="0dp"
android:insetBottom="0dp"
android:textColor="#color/white"
app:cornerRadius="0dp"
app:backgroundTint="#color/btn_background_gray" />
</LinearLayout>
After applying below property to "Reset" button.
android:insetTop="0dp"
android:insetBottom="0dp"
Button spacing will be removed from top and bottom. It will occupy spacing with button color. (like in Reset button)
Button background can be changed using one property app:backgroundTint.
<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false" >
</EditText>
I have the above edit text which is disabled. I only take values from somewhere else and insert it there, the user can not type inside of it. However, the color of the edit text when in disabled form is too gray and not visible to the user sometimes. I prefer it to appear black. Is there any basic functionality I am not aware than can change the color of edit text when in disabled mode ?
Any other work around works fine too.
Your layout:
<EditText
android:id="#+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:background = "#drawable/edittextdrawable.xml">
</EditText>
edittextdrawable.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/PUT DRAWABLE HERE" />
<item android:state_enabled="true"
android:drawable="#drawable/PUT DRAWABLE HERE" />
</selector>
Try to use statelist xml drawable, and set this to your EditText's background:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"
android:drawable="#drawable/bankszamlak_pressed" /> <!-- pressed --> <item android:state_focused="true"
android:drawable="#drawable/bankszamlak" /> <!-- focused --> <item android:drawable="#drawable/bankszamlak" /> <!-- default --> </selector>
for further information check this link
Just set text color to black will make it better:
android:textColor="#android:color/black"
You should use TextView if you don't want the user to type into. Then you don't need to use disable to stop user from typing into it.
How can I remove the border with appears when focusing an EditText View?
I need it cause this view has little space in the screen, but without the border it's enough. When running on Emulator an orange border appears, on Device a blue one.
Have you tried setting the background of the EditText to a transparent colour?
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello"
android:background="#00000000"
/>
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background_normal"
/>
It is possible. However I would not recommend it because users are used to certain metaphors and you should not change the general UX.
You can apply different styles to your views. In your case it sounds like you want an EditText View element which looks like a TextView element. In this case you would have to specify other backgrounds for the EditText depending on the state of the View element.
In your desired layout.xml you assign a background to your EditText:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hello" android:background="#drawable/custom"
/>
Then you create the custom.xml in your drawable folder and add the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/textfield_default" />
<item android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_focused="true" android:drawable="#drawable/textfield_disabled" />
<item android:drawable="#drawable/textfield_disabled" />
</selector>
Those are the possible states of your EditText View element. Normally you can access Android platform drawables directly by using #android:drawable/textfield_default, but in this case the textfield drawables are private so you have to copy them into your own drawable folder. The original resources can be found in your SDK installation folder at ANDROID_HOME\platforms\android-(API LEVEL)\data\res\drawable-(*dpi)\.
Once you are done you end up with an EditText which looks like a TextView but completely without those borders. Those orange borders you saw in the emulator are the default Android drawables. The blue ones are vendor specific (possibly Samsung).
Hope that helped and didn't confuse that much.
This is the fastest solution:
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
/>
You can keep the background color as transparent to remove the EditText border on focus.
Method 1
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
/>