Android Switch widget textOn and textOff not working in Lollipop - android

The behavior of the switch widget changed in Lollipop (5.0).
<Switch
android:id="#+id/switcher"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:layout_toEndOf="#id/another_view"
android:layout_toRightOf="#id/another_view"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:textOff="#string/disabled"
android:textOn="#string/enabled"
android:fontFamily="sans-serif-condensed"
/>
Rendered switch when targetSdkVersion=19:
Rendered switch when targetSdkVersion=21:
Note that preview rendering in Android Studio still produces a switch with text, but the switch loses it's text when an apk built with targetSdkVersion=21 is run on a device with Lollipop (Nexus 5). Running an apk built with targetSdkVersion=19 on the same Lollipop device renders the switch properly with text as expected.
Why? Any suggested workarounds?

Text is not shown by default under Material theme since the switch widget assets don't work well with text. Any text that you do set will be used to describe the content to accessibility services.
You can change this using the android:showText property or Switch.setShowText(boolean) method.
<Switch
...
android:showText="true" />
If you are using AppCompat switches, use app:showText instead.

Related

How do I scale my text size for multi-device compatibility on Android Studio?

I am having trouble scaling my text size based on the user's device. I use constraint layout to constrain the TextViews. I found that autoTextSize could be used to automatically fit my text in the TextViews. However, when I run my code on an Emulator like BlueStacks, some texts are not scaled properly. Weirdly, some work but some do not, even though I am implementing it the same way.
This is an example of what it looks like: The register button is in a big font but other texts aren't
This is what it looks like on my layout Editor:
My code for the register button:
<TextView
android:id="#+id/register"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="REGISTER"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="5:1"
app:layout_constraintEnd_toEndOf="#id/guideline3"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.541" />
my code for the "up to 8 characters" text.
<TextView
android:id="#+id/req"
android:layout_width="0dp"
android:layout_height="0dp"
android:autoSizeTextType="uniform"
android:text="*up to 8 characters"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="7:1"
app:layout_constraintEnd_toEndOf="#+id/guideline5"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/guideline4"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.538" />
Am I misusing autoSizeTextType somehow?
Also, my "Enter Username" text is very small. I use EditText and implement it the same way I did for the other two texts. If there is anyway I can make the font size bigger to fit the layout size, please let me know.
your <TextView android:id="#+id/register" is using app:autoSizeTextType="uniform" attr, <TextView android:id="#+id/req" have android:autoSizeTextType="uniform" - note the difference app/android. try to consolidate this param, you should probably use app in both/all TextViews
note that setAutoSizeTextTypeWithDefaults was introduced in API 26, so when you use android: prefix then this attr will be respected only on 26+. using custom app: makes this attr usable for TextView (in fact TextViewCompat) on any OS version
in Android Studio above layout preview you have an option to switch "Device to preview" and also API version - try to change to some older (in this sample 25 at most, preferably lowest supported minSdkVersion) or newest OS version, IDE should catch and show differences in look
Use TextView params like this
android:autoSizeTextType="uniform"
android:autoSizeMinTextSize="12sp"
android:autoSizeMaxTextSize="100sp"
android:autoSizeStepGranularity="2sp"
You can check the official documentation for more info.

Drawable alignment with text in Android

I have problem with this:
As you can see the back icon is not correctly with the back text.
It needs to be little bit downwards.
I've tried:
to change the drawable image, resizing it.
padding but it is not useful for this condition
to switch to Button,Material button but nothing works fine.
Here is my code(Design code):
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:drawableStart="#drawable/ic_back"
android:text="Back"
android:textColor="#color/SkyBlue"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I need like this:(See that mailbox button)
Note 1: This is just because my client have this kind of specific design.(IOS inspired).
Note 2: I know IOS design standards and android design are way different; also this design wont fit in android deign guidelines. But I can't do anything.

Downloadable font not displaying for Switch TextView

I'm using downloadable fonts for an app, and it's working just fine, however when I've tried to apply the font to the TextView of a Switch, my device won't display it and resorts to the default font - it displays the font correctly for TextViews in the same activity outside of the Switch component.
Strangely, the Android Studio layout design window shows the correct font, but when running on device, it doesn't seem to work correctly.
<Switch
android:id="#+id/switch1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:fontFamily="#font/baloo_bhai"
android:text="This is a switch"
android:theme="#style/AppTheme.Switch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
This is the display in Android Studio:
This is from my device:
It transpires this is a known issue Google Issue #63250768
A fix was located in another StackOverflow question however downloadable fonts will not work with this method - until a resolution, font files must be embedded into the application, as .XML fonts will not work.
Great job, Google!

Switch widget not drawing on Android v5

I have a basic switch widget that displays just fine on Android 4.0.3, but doesn't render at all on Android 5.
<Switch
android:id="#+id/ModeToggle"
android:layout_marginRight="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:textOn="On"
android:textOff="Off" />
Everything inflates successfully, and I even successfully add an event listener to check changed, it just doesn't draw to the screen on v5. On v4.0.3 everything works as expected.
Any ideas what could be going wrong here?
The issue here was that I needed to change that activity's theme to Theme.AppCompat in order to get it to draw.
Either way, I wouldn't recommend using a switch with text anymore, since Android 5+ makes them really ugly. We decided to use ToggleButton instead.

Android elevation shadow appearing in Android Studio preview but not on my KitKat device

I have the follow TextView tag in my Activity layout
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="25dp"
android:text="TEST TEST TEST"
android:padding="15dp"
android:layout_margin="120dp"
android:translationZ="5dp"
android:background="#FFFFFF" />
The shadow from the elevation is appearing in Android Studio's render of the activity, but when I run the app on my KitKat phone (Galaxy S4), the shadow is nonexistent. Is this a common problem?
The elevation property is only supported on Android 5.0+. android:elevation will be ignored on older devices.
Since your device does not support elevation, you can add your own drop shadows to your textview. Take a look at https://stackoverflow.com/a/10373264/4739608 , this should help.

Categories

Resources