Autoinsert right-to-left attributes Android Studio - android

I don't know all features of Android Studio, I have tried to search,but found nothing.
I wonder if it is possible to make Android Studio autoinsert attributes required for right-to-left support.
For example I have typed following line
android:layout_marginLeft="10dp"
Is it possible to make Android Studio insert marginStart attribute automatically ?
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
And vice-versa(for padding too).
This would save some time.
Maybe someone knows how to get such behavior, I will be grateful for any help. Thx.

Apparently, you can't do that. You have to write them yourself.
The difference between layout_marginLeft and layout_marginStart for example is that layout_marginLeft is executed for left to right languages like English, while `layout_marginStart' is only executed for right to left languages like Arabic.
So if you are going to add string translations to your app including arabic or any other right to left languages, you will need to write marginStart or marginEnd attributes... So anyway, if you didn't write layout_marginStart for example and your app doesn't support Arabic language or any other right to left languages, no error will occur to the user; it is just a warning that Android Studio tells you.
Definitely the same concept is applied to padding attributes. Hope that helps you.
Edit:
If you don't like to see Android Studio warning you these warnings, you can simply disable that by clicking on the yellow light bulb beside the yellow highlighted warning and selecting Edit 'Using left/right instead of start/end attributes' inspection settings, then uncheck it from the list.
But if you don't want to change the inspection settings, you can just add the following to your View that you don't want to use start/end attributes in it:
tools:ignore="RtlHardcoded"
and add that to your parent layout that contains that View:
xmlns:tools="http://schemas.android.com/tools"

Related

Android - textview doesn't show up

For some reason, my textview Add a base doesn't show up when I launch the app on my phone. I have no idea why, 'cause it is displayed on Android studio :O
Replace tools:text="#string/add_base" with android:text="#string/add_base"
Attributes in the tools namespace are shown only when designing layouts. When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.
Reference: https://developer.android.com/studio/write/tool-attributes
tools:text="YOUR TEXT" can be used for your design and preview purpose.
If you add any text as a tools:text app will not use this text as that is not the actual text that should be rendered on the app
So instead of this, you should use android:text="YOUR TEXT"

Ltr for Persian locale in android

I am developing an app which supports two languages: English("en") and Persian("fa"). I have set android:supportsRtl="false" in AndroidManifest.xml since I need everything to be from left to right. I set margins for all views that I have but for the ones containing a string, it is not working right and it still seems like it is setting the directions from right to left. How can I fix that? I also tried changing the layoutDirection manually to left to right but that did not work either.
You must handle your locale in all activities. Android by default uses the locale of the device to select the appropriate language dependent resources.
Also you must consider project minimum sdk. I recommended to you change it to 16 or higher: minSdkVersion 16
Maybe this link help you.
I set margins for all views that I have but for the ones containing a string, it is not working right
For this for example you must use from android:layout_marginEnd instead of android:layout_marginRight. Also in default create your layout for en locale and then handle and change it by app locale to fa or another locale.
You have to use instead of the right from the end and instead of the left from the start
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="تست"
android:gravity="start"/>

Can you set graphical layout preview-only text on a TextView?

I have a styled TextView whose real text is populated dynamically at runtime. The Graphical Layout view is very useful for getting a feel on how this component works with others in terms of look and feel, etc. There is no sensible default to this text field and I wish it to be blank before being populated. If I don't specify any text in the TextView declaration then the TextView is blank. I can set the text manually using:
<TextView
...
android:text="Preview text"/>
and then switch to the Graphical Layout. However, I must remember to remove this or risk it being shipped in my production version.
Is there a way to specify text which is only seen in the Graphical Layout preview but not applicable at runtime?
EDIT: I'm using Eclipse ADT.
Yes you can with the design tools extension attributes in Android Studio.
See this page https://developer.android.com/studio/write/tool-attributes.html
Basically you define the tools namespace
xmlns:tools="http://schemas.android.com/tools"
Then use it to set your placeholder text.
<EditText
tools:text="John Doe"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This actually works with most (if not all xml attributes).
e.g
tools:visibility="gone"
would set the preview visibility to "gone" but the runtime visibility would be unchanged.
I don't believe there is, the only possible way is when you declare your TextView, you say after, tv.setText(""); this way you will always find it blank at runtime

Android TextView won't show right number of lines in Android 2.3

I have a couple of TextViews in my app, which should have exactly 3 lines. This works on Android 3+, but on Android 2.3, which I'd also like to support, these fields have exactly 2 lines. I've also tried TextView.setLines(3); in code, but that does not seem to help. This is my TextView:
<TextView
d1p1:lines="3"
d1p1:maxLines="3"
d1p1:minLines="3"
d1p1:ellipsize="end"
d1p1:text="Some Text"
d1p1:textAppearance="?android:attr/textAppearanceMedium"
d1p1:layout_width="fill_parent"
d1p1:layout_height="55dp"
d1p1:id="#+id/detailsTextView"
d1p1:layout_below="#+id/locationTextView"
d1p1:textSize="13dp"
d1p1:textColor="#color/app_darkgray"
d1p1:background="#color/app_dark_background"
d1p1:layout_marginLeft="#dimen/sr_horizontal_padding"
d1p1:layout_marginRight="#dimen/sr_horizontal_padding" />
Setting the height to wrap_content also does not work.
After some research it looks like my suspicion is correct. As discussed here (ellipsize multiline textview), Ellipsize causes issues with the min/max lines attributes. Because of this you may want to consider avoiding the Ellipsize otherwise, if that is not an option, you can follow the instructions in the other thread to use a custom TextView class to work around the problem.

How to make the text direction from right to left

I want to write text in (right to left language i.e. Arabic) in a TextView. But I want to make the text writing direction from right to left. gravity:rightwill align the text to right only. I want to justify the text from right to left ( to make the words and numbers appear in he entered order in the line ) . how ?
Another clever way which can be used in older versions of android and can be used anywhere in string so it's handier even in latest version of android, is to include the right-to-left mark or even left-to-right mark in the string when it's needed:
left-to-right mark: ‎ or ‎ (U+200E)
right-to-left mark: ‏ or ‏ (U+200F)
This is how it's down:
String rtl = "Hello \u200F(سلام)";
The good thing about this method is you can even use it after punctuation like (,{,[,! which are positioned right incorrectly! examples:
سلام! // wrong position
سلام!‏ // right position by adding RLM after !
look how ! is positioned correctly in second line, actually there is an RLM after it, and you can copy paste it even if it's invisible.
The second good thing as you saw it is that you can use this method (these char codes) in any system that supports RTL like browsers, word and so on.
Just put this property in your TextView and everything will workout just fine it will automatically detect your text and change accordingly
android:textDirection="anyRtl"
Too late , but the android:gravity="right" worked.
set this line in xml for textview :
android:textDirection="locale"
Try using
textview.setTextDirection(View.TEXT_DIRECTION_RTL);
or
textview.setTextDirection(View.TEXT_DIRECTION_ANY_RTL);
In case of normal edit text this will work
android:textDirection="rtl"
But in case of password fields this is not enough then follow this;
android:textDirection="rtl"
android:gravity="right"
Use android:layoutDirection="rtl" and android:textAlignment="viewStart" to make the text view right-to-left.
try this as it worked for me android:textDirection="rtl"
Add these to your editText:
android:gravity="right"
android:textDirection="rtl"
PS: android:textDirection requires API level 17
just adding my own experience with such issues. in my case in addition to suggested solutions i also had to replace English " character with the RTL (in my case hebrew) representation of such character ״
By using attributes as below in my xml code strangely I got a right to left text with correct punctuation. But this is not a perfect solution for multi language text:
android:textDirection="ltr"
android:gravity="right"
setting ltr corrects punctuation problems like having dot on right side instead of left.
gravity attribute makes text flow from right to left.
best way textDirection for TextView, Don't use layoutDirection
<TextView
android:id="#+id/viewCountTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textDirection="anyRtl"
android:gravity="center"/>

Categories

Resources