Set Keyboard for Action in Android - java

I am new to Android development and need some pointers around the input. I am familiar with the use of Android and I would like to know how to complete general actions around the keyboard.
I know how to set it to things such as phone, email, decimal, and so on.
But how do you change it to move to the next field or fire an action?
Thank you for your insights.

You need to add this attribute in editext xml android:imeOptions="actionDone"
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="text"/>

Related

What is an Alternative to the UIPickerView from IOS in android?

I am trying to reproduce the IOS UIPickerView functionality but in android.
In IOS you can set this UIPickerView as an input type of the keyboard. I need this same type of interaction but in android.
UPDATE:
When I say input type I mean the UIPickerView simply replaces the keyboard view. So if a textfield were clicked the UIPickerView would open instead of the Keyboard. Hope that clears stuff up!
not sure exactly what you mean by
as an inputtype of the keyboard
but the android equivalent to that image would be the TimePickerDialog
Update
If you want that picker type of UI you have 3 options for pickers, the TimePicker, DatePicker or the NumberPicker obviously the first 2 are out so you are left with the NumberPicker with the number picker you can populate it with the numbers you want (inches) and then another for feet
I suggest you take a quicl look at the Pickers article
https://developer.android.com/guide/topics/ui/controls/pickers.html
I would suggest making a layout with multiple pickers, that way you can have as many as you need and initialise them with any values you require.
The below layout will give you two spinners side by side similar to the iOS UIPickerView.
I've posted a link to my github repo at the bottom with a simple example showing how to use 3 spinners, one for minutes, hours and days that should help you (Or anyone else coming from iOS with the same question) can use as a base to get started.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/multispinner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Spinner
android:id="#+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
<Spinner
android:id="#+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
My simple multiple picker example
You can use a number picker, if you're looking for a similar feel. Just make sure to add a personalized style as theme (or else it will take your app theme as default) and add a formatter to the number picker. You'll run into some trouble on the first render (it won't show the first value until you scroll), just use the solution provided here:
Android NumberPicker with Formatter doesn't format on first rendering
You can also put it into a cardview like in the image, it looks tidy enough.
formatted number picker screenshot
Ps.: Actually you can modify several fields, but you have to acces the class fields/methods. To do so, do as this exmaple code.
public void setNumberPickerDivider (NumberPicker picker) {
Field[] fields = NumberPicker.class.getDeclaredFields();
for(Field f:fields){
if(f.getName().equals("mSelectionDividerHeight")){
f.setAccessible(true);
try {
f.set(picker,1);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}

Android Search icon on keyboard WITHOUT moving to next element

This may seem like a dumb question, but I want the magnifying glass on the keyboard, but I don't want the focus to go right to the next element.
I've got an EditText that populates a ListView with possible matches to the user's query. The EditText's XML includes
"android:imeOptions="actionSearch"
which adds the search icon. But it also takes the user to the first element of the ListView which is really annoying.
Is there a way to include the search icon AND not force focus to the ListView?
EDIT:
By request, my EditText xml. Sorry in advance if I mess up the formatting somehow.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editSearchWord"
android:imeOptions="actionSearch|normal"
android:inputType="text"
android:drawableLeft="#android:drawable/ic_menu_search"
android:layout_alignParentStart="true"/>

Android: TextInputLayout won't center the hint

I have an edittext enclosed within a textinputlayout from the support library and I'm finding it impossible to make the hint appear in the center of the field. I've tried all the usual tricks to be found in other stackoverflow discussions, as you can see from the code sample, but the hint still stubbornly appears on the left of the edittext. How can I solve this?
Please don't suggest using paddingLeft or paddingStart to do this - I want something that's going to work cleanly across different devices so it has to be a straight solution rather than a workaround.
Just to be clear: If I remove the textinputlayout then the hint is properly centered. It's the textinputlayout that's causing the problem here.
<android.support.design.widget.TextInputLayout
android:id="#+id/someId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:ellipsize="start"
android:maxLines="1"
android:inputType="textPersonName"
>
<EditText
android:id="#+id/someId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="#string/somehint"
android:textColorHint="#color/somecolor"
android:textSize="20sp"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:cursorVisible="true"
android:textCursorDrawable="#null"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:inputType="textPersonName"
android:maxLines="1"
android:ellipsize="start"
/>
</android.support.design.widget.TextInputLayout>
Using the latest version of the support library:
compile 'com.android.support:design:22.2.1'
Edit - things I've tried to resolve this:
1) replacing Edittext with android.support.v7.widget.AppCompatEditText - doesn't work
2) using app:hintTextAppearance="#style/TextAppearance.AppCompat" inside the textinputlayout - doesn't work
3) Instead of defining the hint in the edittext, setting the hint programmatically using textinputlayout.sethint inside the activity - doesn't work: the hint appears on the left.
4) Setting the hint on the edittext in onActivityCreated. This appeared to work initially, but I discovered that although the hint appears centered the functionality of inputtextlayout becomes broken and clicking on the edittext no longer performs the hint animation.
To center the hint text in your EditText just add the following two attributes in your EditText. This will do the trick.
android:ellipsize="start"
android:gravity="center_horizontal"
There doesn't seem to be a way to do this (at least not that I can find). As a workaround I tried using the MaterialEdittext 3rd party library - also not ideal since although the small animated hint is centered, it appears inside the text box instead of outside it:
https://github.com/rengwuxian/MaterialEditText

Android EditText issue

I have an editText field in my app and the keyboard pops up when I focus the text field on lower APIs than 10. For some reson, the keyboard doesnt show when the editText is focused in APIs higher than 10. Also in API higher than 10, in the logcat it says eglSurfaceAttribute not implemented whenever I focus the EditText field. Thanks in advance.
One way to bring up the soft keyboard when EditText is focused is to add <requestFocus/> to the EditText in the XML layout.
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus />
</EditText>
Let me know if this helps.
Just put this line on your onCreate()
editText.requestFocus();

android:gravity="right" not working on all devices for Arabic and other RTL languages

I've got an Arabic Android application, and here is the XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/greygradientbackground">
<ImageView android:id="#+id/logo"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:layout_margin="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true" />
<TextView android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:layout_gravity="center_vertical|right"
android:gravity="right"
android:layout_toLeftOf="#id/logo"/>
</RelativeLayout>
The problem is that android:gravity works on some Android models and on others don't.
To be more specific, I've tested the app on many 2.3.3 android devices and the Arabic text is aligned right. However on other 2.3.3 devices the Arabic text is aligned left (which is wrong).
When I changed android:gravity="right" to android:gravity="left" the problem shifted from the second group of devices to the first.
So my question is how can I solve this issue especially that as far as I know there aren't a way to localize layouts based on a device model number.
Thanks in advance for any guidance because am totally lost. :(
UPDATE:
I searched about "How to align Arabic correctly on all Android versions?" but found nothing working on all my testing devices. Any suggestions please? I am sure there is a best practice approach for aligning Arabic text on Android.
UPDATE 2:
I tried to use a WebView instead of a TextView to align Arabic correctly using CSS styles. However, the Arabic text is showing in the WebView as strange characters.
Here is the Code:
mWebView.loadData("<html dir=\"rtl\">الأسم<body></body></html>", "text/html", "UTF-8");
The strange thing is that Arabic websites text is displayed correctly. So, what's the problem? :(
This is an expected behaviour. You may not use gravity="right", but gravity="end" instead, the same idea you apply to gravity="left" which you may use gravity="start", as well as layout_marginStart instead of layout_marginLeft
This way, Android will put the text to the "start" orientation, depending on the location. (for us, americans, we start to write from the left and we end on the right, but arabians start from the right and end on the left).
You can get the layout/text directions with the method getLayoutDirectionFromLocale(null), this will return View.LAYOUT_DIRECTION_LTR (left-to-right) or View.LAYOUT_DIRECTION_RTL (right-to-left, like arabic).
More info you can check here
change your textveiw in the layout to this....
<TextView android:id="#+id/title"
change your layout_width to "fill_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:layout_gravity="center_vertical|right"
android:gravity="right"
android:layout_toLeftOf="#id/logo"
/>
You can change the thing in another way also....You can make changes in the activity class..as follows...let your TextView with id title is mapped in the activity class with a TextView object named "tv" the you can make it right aligned by writing this.
tv.setGravity(Gravity.RIGHT);
but in both the cases the width layout width should be "fill_parent"..
If you do not do this and keep it "wrap_content" then there is no space for the text to move... all the alignments (center,left,right...)remain the same.....
If both of these donot work out... then I would prefer you to use table layout and keep the text view in it....
try putting your textview inside a separate LinearLayout ,and set that layouts properties likethis:
android:layout_gravity="center_vertical" and android:orientation="vertical"
android:gravity="right"
note: dont specify layout_gravity of the textview.
hope this helps! it worked fine for me.
also you can try the solution provided here: Android Arabic text aligment
Another solution to achieve such behavior:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="ltr"
android:layout_weight="1"
android:gravity="end"
android:padding="16dp"
android:text="#string/locale_switch_btn" />
</LinearLayout>
Note that, for correct gravity="end" alignment, in rtl locales you should set property:
android:layoutDirection="ltr"

Categories

Resources