If you own Android phone you are no doubt have noticed how in the certain apps the keyboard layout can change from the standard issue to digits-only or to have .com or .net special buttons based on the text field input type (e.g. phone number). So I have 2 questions:
how to trigger this customization? I suspect it has to do with EditText format
Can this be taken even further if I want to add some custom buttons to inject a specific pattern? Say I would have an AND button which when pressed will add all uppercase " AND " surrounded by spaces to the text field. Can this be done?
What I'm not asking is how to capture some key combination in onKeyPress event and then populate text field with a pattern - I pretty much know how to do that already.
It is controlled by the android:inputType XML attribute (or the setInputType() method).
For info on the available options see the pages for the XML attribute or the object's method.
As an example, the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:text="example text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
will give you this layout:
whereas changing the inputType to textEmailAddress will give you this:
You can customize the "action" button as explained here, but I don't believe there's any way to do full customization of keyboards at this time, but I could be wrong.
The thing that concerns me is that "inputType" is listed as a deprecated property, meaning it may work for a while, but - eventually - Android will stop supporting it. Is there another alternative?
UPDATED: My bad - I'm confusing with inputMethod.
Related
I have an Edit Text in one of my App layouts, and I want this EditText to only open the keyboard (I believe this is called being focused on?) when it is actually touched.
As of now, the keyboard opens with the EditText whenever the app opens, which isn't what I want.
I have tried many different XML tags to fix this:
android:focusable="false" <--- Prevents keyboard from opening at all.
android:focusable="true"
android:focusableInTouchMode = "true" <--- These tags give me the same result as no tags (keybaord will open on activity start)
android:focusedByDefault = "true" <--- Only available in API >= 23
What I am asking is, why is it so hard to disable default focus on an EditText? Surely I am missing an easy way to do this.
EDIT: Adding this line to my AndroidManifest fixed the issue:
android:windowSoftInputMode="stateHidden"
However, I don't like this solution. It seems like since this is in the Manifest, it will affect more UI elements than the single EditText I need to change.
Alternatively you can set the focus to the root layout element:
android:focusable="true"
android:focusableInTouchMode="true"
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Use android:windowSoftInputMode="stateHidden"
If you dig deep into the Theme you are using for your Activity, you will find that the default value of windowSoftInputMode is stateUnspecified|adjustPan. And from the documentation:
stateUnspecified: Not specified, use what the system thinks is best. This is the default.
So depending on the the android device you run, your results will vary. I tried reproducing your case in API-26 emulator and the keyboard doesn't show up.You can use stateHidden to ensure that when an activity starts, the soft keyboard doesn't show up when the EditText gets focused on itself.
The other way to solve this is to requestFocus to some other element in the UI, making sure the EditText is not the first UI element to get focused. In my experience this is kind of a hack and it messes up the accessibility. The safest and clean way to accomplish is actually to use stateHidden.
stateHidden: Make the soft input area hidden when normally appropriate (when the user is navigating forward to your window).
Note that this will not affect any other UI elements. You can use adjustPan also to this, based on the screen background.
I'd like to do textual back-and-forth interaction in an Android control. The idea is to have something like this:
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning of the last line). The user is then free to enter text (using whatever Android input method, keyboard, etc.) but isn't allowed to change any of the output so far. Ideally, the user's input would be styled differently.
Then, as soon as newline is entered, I want the program to be notified and editing to stopped:
This is some text output by the program.
What is your name? Foo Bar
Hello, Foo Bar!
Note that this needs to be a proper control, i.e. one I can compose with other controls to make it just one part of the app's main layout.
Make a TextView and the EditText next to each other then your problem is solved and add the following line of code in EditText.
android:singleLine= 'true';
It allow only one line to be entered to the EditText. let me know whether this is what your expecting.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="What is your Name?"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textView" />
</RelativeLayout>
This is some text output by the program.
What is your name? |
with the cursor at | (note that editing doesn't start at the beginning
of the last line). The user is then free to enter text (using whatever
Android input method, keyboard, etc.) but isn't allowed to change any
of the output so far. Ideally, the user's input would be styled
differently.
I would strongly recommend to rethink about your design as the same thing can be done with the help of LinearLayout,Editext,TextView with very simple and more manageable way.
I would suggest you to create a new LinearLayout(TextView + EditText) and assign the background of layout like EditText and edittext's no background.
Upon editText done, you could show a new TextView in the bottom
You need a ListView at top, to show your conversation & then below it, needs a horizontal view with a TextView (to show question) and EditText(with background transparent - to ask user to fill an answer).
I have been messing around with custom keyboards but cannot achieve what I want.
I tried to follow various tutorials (like this one How can I implement special soft keyboard) but could not build my own keyboard since there is always missing some part.
I also tried the sample SoftKeyboard that comes with the API 10 samples but the layout did not suit my app.
Now, all I want to have is a the default android 4.3 softkeyboard, that instead of giving the option to switch to the numeric view in the bottom left corner (I am referring to the ?123button) , adds another row of keys to the top. That row should hold the keys '0' to '9'.
Can someone help me out here? I would really appreciate it!
Adding extra row to the android default keyboard is not possible for this you have to go to the custom keyboard..Custom keyboard is not much difficult to understand here is the best example of android custom keyboards..
1.Custom keyboard example-1
2.Custom keyboard example-2
Once go to the xml file in the res/xml folder and define keys whatever you want..and no of rows and columns for keyboard..
In your layout definig the keyboardview set android:keyBackground="#drawable/keyboardview_selector" color whatever you want..
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/keyboardview_selector" />
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.
I'm creating an IM client/server application for the Android OS, and am currently putting together the user interface. Right now, my interface consists of a EditText element, and a Button element. When I tap on the EditText element, a keyboard pops up and allows you to type.
What I would like to have is something like the text entry area and send button in the default Android SMS app. Something like this:
The text input field and Send button would stay at the bottom of the screen, and when tapped on, the keyboard would push the text field and button up above it.
Is this possible using only EditText and Button elements?
Thank you for any suggestions or advice!
Try setting android:windowSoftInputMode=adjustResize for the activity in AndroidManifest.xml.
You can find details here.
Is this possible using only EditText and Button elements?
Answer-This type of functionality is possible in any type of view
I give just short tutorial on your question
Generally we use only linearlayout in xml file.But at view level android gives many more feature like Relative layout and much more.At this time we just discuss about the relative layout because it can solve your purpose.
In Relative layout it not use the android:orientation feature like linear layout it used another feature.In relative layout take some points in your mind...
we always give id to every view using android:id="#+id/GiveName"
for alignment of any view we used android:layout_toLeftOf="#id/givesname" same for
right,above and below where givesname=id of that view from which this view is align.
Ex. is gives example with screen shot
After this i give the sample xml file in which you get the above type of feature in your question
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llWriteCommentWall" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:background="#ffffff">
<Button android:id="#+id/btButtonComments"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Comments"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<EditText android:id="#+id/etEdittext"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:hint="Write a comment.... "
android:layout_marginLeft="2dip" android:layout_marginRight="2dip"
android:layout_toLeftOf="#id/btButtonComments"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
ScreenShot of above example
In this Example we used android:layout_alignParentBottom="true" - this attribute is the main reason for view like this,it always align any view in bottom even softkeyboard is shown.it contain boolean value,true gives always align bottom and false nothing.
the other relative attribute is android:layout_alignParentRight="true",android:layout_alignParentLeft="true" ,android:layout_alignParentTop="true"-all attribute give feature as written.
Lastly you include this xml file at any java file through setContentView(xmlFileName)