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.
Related
I am trying to create a listview with switches. It is working; but not as how i expected it to be. The text ON/OFF is written inside the circle(slider) and not behind it which looks awkward. I cannot find a way on how to put the text behind the slider and i tried searching but it seems that no one is experiencing this problem aside from me.
Also, is there a way where i can edit the custom switch or am i obliged to use android:background like what others are doing base on my research. (I am avoiding to use background because i am not good at Photoshop).
Thank you in advance!
You can have 9 patch images for switch
<Switch
android:id="#+id/switch_bg2"
android:layout_width="92dp"
android:layout_height="24dp"
android:checked="true"
android:focusable="false"
android:textOff="O"
android:textOn="I"
android:thumb="#drawable/switch_thumb"
android:track="#drawable/switch_bg"
android:typeface="sans" />
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.
I have a very basic widget with two buttons in a layout. They look like standard android buttons, but with the 4.4 update the buttons now appear as a dark grey instead of light grey. They remain normal in previous versions of android. Does anyone have any idea of why this is happening and how I can fix this?
Thanks!
Button xml:
<Button
android:id="#+id/button_widget_newshift"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="#string/new_shift_split"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#101010"
android:textSize="#dimen/widget_text" />
I don't have an answer (and I don't have enough of a reputation to post a comment). I have the same problem, although I'm creating my buttons in code. They work on devices prior to 4.4, but are dark grey and borderless on my Nexus with 4.4 and a 4.4 emulator.
I understand that 4.4 changed drawables to tone down touch feedback (see http://developer.android.com/design/patterns/new.html). Perhaps, in the process, a button style or drawable got misplaced. If so, a solution/kludge might be to copy working resources from the SDK into your project and call setBackgroundResource() to them. I haven't tried this; I'm hoping someone can come up with something more elegant and definitive.
Here's my XML layout example of one of my TextViews which show itself correctly in android 4.2 ... I've downgraded a Nexus S to gingerbread 2.3.6 to test out my application and debug it! Right now, each of my TextViews doesn't take any more space than one line, not even wrapping itself at the end of the first line. (On 4.2, the example below was taking 3 lines and was adding "..." at the end if there was some text missing!)
How can I make my textViews compatible with gingerbread? Thank you!
<TextView
android:id="#+id/TV_guideRow_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TV_guideRow_title"
android:layout_below="#+id/TV_guideRow_title"
android:text="blabla text that could go up to 3 lines"
android:textColor="#3BB9FF"
android:layout_alignParentRight="true"
android:layout_above="#+id/TV_guideRow_more"
android:layout_marginRight="8dp"
android:layout_marginBottom="3dp"
android:ellipsize="end"
android:maxLines="3"/>
You want:
android:inputType="text|textMultiLine"
Also, depending on the parent of that TextView, multiline may not render properly. Try manually setting the height to, say, 100dp and see if that works.
After some more investigation, I've found out that all my related textView problems were related to my custom theme, which was made for android 4.0+ (since Holo was used as the base theme)
I've set the APIs which doesn't have holo to use the "Light" theme and everything is showing up correctly without any further modification.
I have a button code, where in i dont want border for this button, so for that i set propertie "android:background="?android:attr/selectableItemBackground", but it will not work in API version 8 i.e in Froyo emulator. Please provide me the alternatives.
<Button
android:id="#+id/serviceContactNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/icon"
android:layout_alignLeft="#+id/serviceName"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:freezesText="true"
android:textSize="30dip"
android:background="?android:attr/selectableItemBackground"/>
Thanks in advance.
Borderless buttons are part of the Holo theme which is not available to earlier API versions by default. You can however use HoloEverywhere to make the theme available. At this time it does not actually support borderless buttons either but you can get the same effect by setting the buttons background to android:background="#drawable/list_selector_holo_light"
if thats too heavyweight a solution either extract the appropriate assets from holoeverywhere or from \android-sdk\platforms\android-16\data\res\drawable and put them into your own project
Check if the answer I found in this question works with you: My app force closes on setcontentview when using the holoeverywhere library
style="?borderlessButtonStyle"