Android: EditText text alignment on Fragment not working (API 16) - android

I have some EditText and TextView inside a Fragment. On the XML layout of the fragment I have added the following in EditText and TextView:
android:textAlignment="center"
Then I have tested the app in the emulator, on virtual device with API 25, and all seems to work fine, the fragment is displayed as it looks in the design window, with the text centered in both the EditText and the TextView.
However, I have launched the app in my own phone Android 4.1.2 API 16 and also in another virtual device that I have also created with API 16 and now none of the 'EditText' or 'TextView' are respecting the textAlignment property, the text is just displayed aligned to the left side.
Any help?, I am looking around but I cannot find anything related to API 16 and problems like that with alignments.

You should use
android:gravity="center"
Place the object in the center of its container in both the vertical
and horizontal axis, not changing its size.

You can easily do that with below. As i mentioned in comment also.
<EditText
android:id="#+id/et_bio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:gravity="center"
android:hint="Bio DAte aaaaaaaa"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="3"
tools:ignore="RtlCompat" />

Check official documentation.
android:textAlignment="center" or its setter were introduced in API 17. So it won't work on earlier devices i.e. API 16.
Instead use android:gravity which should provide the same functionality both in EditText and TextView.

Related

Accessible star rating for Android

I recently included the RatingBar view into a layout and realized it is not compatible with Talkback (it is not accessible). How can I have a star rating view that takes into account Talkback users?
Here are some ideas for anyone else who ends up here. I'm using targetSdk=28 and the androidx libraries.
Problem 1: Talkback skips the RatingBar when announcing the content in a ViewGroup.
Solution: Add a TextView with the desired announcement. Make it 0dp, so it won't affect your layout. INVISIBLE or GONE won't work.
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:text="4.1 of 5 stars"/>
Solution: Display the number of ratings, and add the stars to the contentDescription. (do it programmatically, though)
<TextView
android:id="#+id/ratingCountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23"
android:contentDescription="4.1 of 5 stars from 23 users"/>
Problem 2: Tapping on a RatingBar to hear the value. contentDescription works in this case, and you can set it in code.
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="3.5"
android:stepSize="1"
android:contentDescription="4.1 of 5 stars"/>
Tapping and dragging on a RatingBar to change the value seems to work. It says 20%, 40%, 60%.
I found an example of what I need here:
https://github.com/google/iosched/blob/2017/lib/src/main/java/com/google/samples/apps/iosched/ui/widget/CustomRatingBar.java

Checkbox drawable with extra drawableLeft overlapping one another

When using a checkbox with extra drawable (besides the one used for the checkbox) using drawableLeft, the two drawables overlapp one another.
There is nothing special about the checkbox settings, here:
<CheckBox android:id="#+id/low_priority_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/low_priority"
android:textColor="#color/primary_text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:drawableLeft="#drawable/ic_primary_priority_flag_low_medium"/>
Actual result:
Expected result:
This problem occurs with compileSdkVersion 22 on devices with API <= 16
By the way, using drawableRight works as it should.
Is this a bug in the framework?
Any workaround?
Add style (style="#android:style/Widget.Holo.Light.CompoundButton.CheckBox") for the checkbox, which can move the drawable to correct position. The only issue is the distance between drawable and text is bigger than normal case for devices with API <= 16.
One of my CheckBox xml is listed as the following:
<CheckBox
android:id="#+id/male_toilet_checkbox"
style="#android:style/Widget.Holo.Light.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:drawableLeft="#drawable/m"
android:drawableStart="#drawable/m"
android:text="#string/male_toilet"
android:textSize="14sp"
android:textColor="#ff6c51ff"
app:layout_constraintTop_toBottomOf="#+id/split_line1" />

Android RTL layout direction align center issue

I'm developing an Android App that needs to be support Arabic language. (Which should be read from Right To Left). After quick googled the solutions, I figure out android fully support Arabic language natively in API level 17 with the declaration of
android:supportsRtl="true"
in the application tag inside of the AndroidManifest so that I can use the layout mirroring to automatically flip the layout for better right to left reading experience. However, I've noticed there is an issue happening while I use centerInParent in a view that inside of a sub RelativeLayout during the layout mirroring. Below are my codes and expected layout.
<RelativeLayout
android:background="#color/white"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="20dp">
<RelativeLayout
android:background="#drawable/shape_flag_imageview_boarder"
android:id="#+id/imageLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_height="100dp"
android:layout_width="100dp"
android:visibility="invisible" />
<ProgressBar
android:id="#+id/progressbar"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</RelativeLayout>
<TextView
android:id="#+id/text"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_toEndOf="#id/imageLayout"
android:layout_width="wrap_content"
android:text="Some text here bla bla bla"
android:textColor="#color/black" />
</RelativeLayout>
Image above showing the expected result in normal layout direction which is Left to Right. The purpose I wrap the ImageView and ProgressBar together in a sub view is because I want the ProgressBar showing in the middle of the ImageView while the image is loading from the internet. After the I've changed Locale to Arabic, it becomes like
As I've try and error and figure out that this is causing by the centerInParent of the ProgressBar. It instead of centering inside the sub view, it align center to the root parent view which is the most outer RelativeLayout. Below is the screen shot of removing centerInParent code from the ProgressBar.
It clearly shows the layout mirroring works good, but the ProgressBar position is not what I'm expected. So I've try to work on centerVertical and centerHorizontal, the result are shown in images below respectively.
None of the solutions works, and none of the topic I've searched related to this issue. So I guess this might be a bug from Android library? If anyone knows the issues or solutions, please share to me. Thanks
I fixed it by adding android:layoutDirection="ltr" into the child RelativeLayout. Basically, it deactivates the RTL formatting for this particular RelativeLayout, and the android:layout_centerInParent="true" behaves correctly again. It solves our particular issue as our particular RelativeLayout contains only centred elements. But this trick should not be used if the Layout contains other elements which have to support correctly RTL, like text views for example. Hope it helps.
This is an RTL layout bug in the Android framework, which only affects Android 4.2 specifically (API 17) and when android:supportsRtl="true" is enabled in AndroidManifest.xml.
It happens when you use a RelativeLayout that contains items positioned with android:layout_centerVertical="true" or android:layout_centerInParent="true".
You can fix it in Java code like this:
View relativeLayout = layoutInflater.inflate(R.layout.my_relative_layout, parent, false);
// Fix RTL layout bug on Android 4.2 (for Arabic and Hebrew mode)
if (Build.VERSION.SDK_INT == 17 &&
getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
// Force a left-to-right layout
relativeLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
let me tell you correct answer, look your RelativeLayout(id:imageLayout),it's width is wrap_content, and your ProgressBar(id:progressbar) add an attribute android:layout_centerInParent="true".It means parent not limit witdh,and child also want to center,so parent will be stretched.

textview doesn't go multiline

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.

Meaning of "No label views point to this text field" warning message

What is the meaning of this warning?
No label views point to this text field with an android:labelFor="#
id/# id/editText1" attribute
Note that the double id (#id/#id) is a problem with the error message text and does not reflect the XML content (which is the correct syntax).
The labelFor is an attribute for accessibility options. You assign this to a label so that if, on a form , user clicks a textedit field , android can know what to read (TalkBack) to user.
The id you assigned to it doesn't seem to be a valid one. why there are two #id in the id? Use ids like this: #id/editText1
I've had the same warning message. It disappeared, when I added a hint to my EditText
android:hint="Some explanation about the input..."
Although I am not familiar with the exact error you have posted. But it definitely sounds like you have done something wrong with the id in the textView. Use id like following in your textView.
android:id="#+id/editText1"
And if you want to set labelFor then use :
android:labelFor="#+id/editText1"
It means that you probably should define a label for this edit text and link them using a labelFor inside that labels definition.
example code:
<TextView
android:id="#+id/my_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:labelFor="#+id/my_editText" <!--the plus sign goes first in the code-->
android:text="I'm a label" />
<EditText
android:id="#id/my_editText" <!--no plus sign if not the first-->
android:layout_width="wrap_content"
android:inputType="text"
android:layout_height="wrap_content" />
and it's not only for text views.
Remove th first '#id/' , use like
android:id="#+id/editText1"
which is the correct format. Keep going.. Best wishes.. :)
I solved it by writing both attributes:
android:id="#+id/editText1"
android:labelFor="#+id/editText1"
Select the editText, go to Properties, then Label for and enter #id/EditText1
If the XML looks correct and you're in a Graphical Layout mode then it's probably using a later version of the Android rendering layout that doesn't support EditText.
In Eclipse and Android Studio there should be a green Android icon with what API version is rendering the layout. Make sure you're using a non W or Wearable API as Android W APIs don't support the EditText element. (EditText is most likely not supported because virtual keyboard space is limited on those devices).
The rendered preview should support EditText in any API 4.X version without a trailing W.

Categories

Resources