Android java - Move from field to submit button using keyboard [duplicate] - java

This question already has answers here:
Multi-line EditText with Done action button
(17 answers)
Closed 5 years ago.
I have a simple contact form on my Android app, it has a Name and Message field and a submit button.
The form works fine, my issue is with the layout/behaviour of the keyboard.
When the user clicks on the Name field, the keyboard pops up and they enter their name and the keyboard then displays 'Next' button, so they can move to the Message field, but then the keyboard changes to 'Enter' for a carriage return, because the android:inputType is textMultiLine
The issue is that now the 'Submit' button is hidden behind the keyboard and the user has to hit the 'back' button to then see the submit button to submit the form.
If I change the android:inputType from textMultiLine to text, the keyboard displays 'Done' which works well, but now all the text entered in the Message field is all on one line and this isn't ideal either.
The ideal solution would be for the keyboard to display the 'Next' so that I can jump to the 'Submit' button (which I do not think is possible) or for the screen to scroll up to see the 'Submit' button (or at least be scrollable).
Any ideas on how I can do this? Thank you.
Thank you.
Here is my activity_contact.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context="com.mycompany.myapp.fragments.HomeFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/bgplain"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/toplinear">
<LinearLayout
android:id="#+id/linearmenu"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/menu"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/menu"/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/linearmenu"
android:layout_width="match_parent"
android:layout_above="#+id/tvvolume"
android:layout_height="130dp"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/lineartitle"
android:layout_below="#+id/linearmenu"
android:layout_width="match_parent"
android:layout_weight="4"
android:layout_height="120dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/toplinear"
android:layout_width="match_parent"
android:paddingBottom="20dp"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:layout_height="match_parent">
<TextView
android:text="Name"
android:textColor="#000000"
android:textSize="16sp"
android:textAllCaps="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/edtname"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="16sp"
android:singleLine="true"
android:inputType="textPersonName"
android:paddingLeft="10dp"
android:hint="Enter name here"
android:background="#drawable/edtback"/>
<TextView
android:text="Message"
android:textAllCaps="true"
android:textColor="#000000"
android:textSize="16sp"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/edtmessage"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="top"
android:inputType="textMultiLine"
android:padding="10dp"
android:textSize="16sp"
android:hint="Enter message here"
android:background="#drawable/edtback"/>
<Button
android:id="#+id/btnsubmit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Submit"
android:textAllCaps="true"
android:textSize="18sp"
android:textColor="#FFFFFF"
android:background="#drawable/btnback"
android:layout_marginTop="20dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
Update : I've tried adding a android:imeOptions="actionNext" to the Message field, but it won't override it, presumably because it is a textMultiLine type field(?)

How about adding
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
to your activity in AndroidManifest? Also you can try another options which probably will suits better https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

This is an example of my second offer. To hide keyboard on EditText focus lose.
First you should create a onFocusChangeListener class.
public class KeyboardCloseListener implements View.OnFocusChangeListener {
private Activity activity;
public KeyboardCloseListener(Activity context) {
this.activity = context;
}
#Override
public void onFocusChange(View view, boolean b) {
if (!b)
closeKeyboard(view);
}
public void closeKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Later you can use this class on all of your EditText fields.
In your activity you can use it like this:
KeyboardCloseListener keyboardCloseListener = new KeyboardCloseListener(this);
edittext.setOnFocusChangeListener(keyboardCloseListener);
Now it should work. If not, add these lines to your parent View:
android:focusable="true"
android:clickable="true"

Related

How can i make a Button in android appear in a fixed position and always in front of all other views?

I know the code might not be well formatted but I want the button (last tag) to always appear in front
of all other views if the description is long the button disappears.
I'm using scrollView and if any long text appears but this makes the button go down the screen.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical">
<Button
android:layout_gravity="bottom|end"
android:id="#+id/cart_button"
android:layout_width="250dp"
android:layout_height="55dp"
android:layout_marginBottom="10dp"
android:iconTint="#color/black"
android:elevation="6dp"
android:text="ADD TO CART"
android:textAppearance="?attr/textAppearanceButton"
android:textColor="#28022E"
android:textSize="20sp"
app:backgroundTint="#F6F2FA"
app:elevation="10dp"
app:rippleColor="#FFF"
app:shapeAppearance="?attr/shapeAppearanceSmallComponent"
app:strokeColor="#0000"
app:strokeWidth="10dp" />
</RelativeLayout>
Take your button out of scrollview.
something like :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
// content that you wants to scroll
</ScrollView>
<Button alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>

ScrollView doesn't work when keyboard shows up

I'm trying to implement a simple activity where the user can rate something and put a comment.
My view is this :
with the code :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#color/background_material_light"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:isScrollContainer="true"
>
<ImageView
android:id="#+id/img_journey"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:src="#drawable/pirate"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="#+id/ratingLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/img_journey"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/title_journey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:layout_gravity="center"
android:textSize="30sp"
android:textColor="#color/niceblue" />
<RatingBar
android:id="#+id/framework_normal_ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:maxHeight="48dp"
android:rating="5"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/textbox_rate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/button_layout"
android:layout_below="#+id/ratingLayout"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp">
<EditText
android:padding="10dp"
android:gravity="start"
android:background="#drawable/textbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/hint_comment"
android:inputType="text"
android:autofillHints="" />
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:orientation="horizontal"
android:weightSum="8"
android:layout_marginBottom="40dp">
<Button
android:id="#+id/btn_launch"
android:text="SEND RATE"
style="#style/button_classic"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
The fact is, when I show the keyboard on clicking in the editText, the view is not scrollable and the result is this :
I search like 3 hours on the internet but I found nothing, the only result that I found was to put this on the onCreate of my activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
But it doesn't work either. (I've tried with and without each flag)
The second thing I've tried was to put the ScrollView into the RelativeLayout, but sameresult, doesn't work.
The thing I would like is that the keyboard appears below the edit text and that we do not see the button SEND RATE (except if the user scroll) and, finally, the user should be able to scroll even when the keyboard is displayed.
Am I doing something wrong or what ?
Thanks in advance !!

How do I change alignment of a button in java?

So, i want to make an adding activity which is used for 3 different tables.
In the last one i want the date EditText to be gone.
I know how to make it disappear but i want the layout o change too and this is how it looks when date EditText is present
3 EditTexts and Button with alignbottom set to date EditText
And this is how it looks when I set date EditText to gone
2 EditTexts and Button with alignbottom set to the gone EditText
Try layout like that:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="visible"/>
</LinearLayout>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
The thing is, that you need another supporting layout, which is the number two in my hierarchy, which will have android:layout_height="wrap_content", so when you set the visibility to gone to one of the EditTexts, it is gonna change the height of the LinearLayout according to the other two EditTexts height, where Button's height is always android:layout_height="match_parent".
LinearLayout with orientation of vertical containing the EditText, wrapped in a horizontal LinearLayout containing the Button too. If both the LinearLayout and the Button have a height of wrap content, they will adapt regardless of how many EditText there are
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="New Button"
android:id="#+id/button"
android:layout_weight="1"
android:background="#3e3e3e" />
</LinearLayout>

Cannot get AutoCompleteTextView to show with other components

I am trying to build a simple Android app one of whose activities will have a Google Places AutoCompleteTextView along with a Button and several other components. However, it seems to be the case that the AutoCompleteTextView component does not want to play friendly with anything else. Currently, I can only get one (the AutoCompleteTextView component) or the others rendering at a time. Here are the relevant portions of my code:
activity_main.xml
<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="horizontal" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
list_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
GooglePlacesAutocompleteActivity.java
public class GooglePlacesAutocompleteActivity extends Activity implements OnItemClickListener {
// class fields are here ...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCurrentDateOnView(); // sets up the date picker
addListenerOnButton(); // adds a listener to a Button
// next three lines configure the Google Place autocomplete
AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.list_item));
autoCompView.setOnItemClickListener(this);
}
// more methods...
}
If you need more information to render an answer here, please feel free to ask me and I will post as soon as I can.
Hi You have a problem of orientation in your LinearLayout. I guess you want your AutocompleteTextView in top of the rest. If that is the case you should probably add a nested LinearLayout.
<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" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="Please enter your place" >
<requestFocus />
</AutoCompleteTextView>
<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="wrap_content"
android:orientation="horizontal" >
[your other items]
</LinearLayout>
</LinearLayout>
If this is not what you are looking for just change match_parent to wrap_content for thw width of your AutocompleteTextView.

How to design buttons?

Does anyone know if there is a possible way to design the android buttons like this:
I'd say these are regular buttons with transparent background and icons set through drawableTop attribute. So an xml for such a button would look like:
<Button android:drawableTop="#drawable/icon" android:background="#android:color/transparent"/>
Try this
<Button android:drawableTop="#drawable/icon" android:background="#android:color/transparent"/>
ALso you can design this type of bottom bar using Tab in android
Click here
Also you can design this type of button using radio button
Click here
For that i think need to create the custom view for tab and call in tabhost
http://joshclemm.com/blog/?p=136
Thank you
okay
--take one layout and aligng it ALIGNPARENTBOTTOM = TRUE
now set background of that Layout to THIS BLACK
-- now take five button in that Layout Horizontal
now take images like these Transparent and set it to the buttons
-- also get Images for HOVER action
and set it on click of that particular button
(use layout_weight for setting equall sizes of button)
Have Fun!!
I have done same thing in one of my app. Here I am giving the xml code for this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="#000"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/reviewLayout"
android:gravity="center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/reviews"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Review"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/nearbyLayout"
android:layout_toRightOf="#id/reviewLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/nearby"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nearby"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/searchLayout"
android:layout_toRightOf="#id/nearbyLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/search"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="75dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/bookmarkLayout"
android:layout_toRightOf="#id/searchLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/bookmarks"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bookmarks"
android:textColor="#FFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/settingsLayout"
android:layout_toRightOf="#id/bookmarkLayout"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:src="#drawable/settings"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFF"/>
</LinearLayout>
</RelativeLayout>
Use this layout where you want using include tag, and call it in your Activity.
Here I am giving the code to include this in Activity.
private LinearLayout reviewLay,nearbyLay, searchLay, bookmarkLay, settingLay;
reviewLay = (LinearLayout)findViewById(R.id.reviewLayout);
nearbyLay = (LinearLayout)findViewById(R.id.nearbyLayout);
searchLay = (LinearLayout)findViewById(R.id.searchLayout);
bookmarkLay = (LinearLayout)findViewById(R.id.bookmarkLayout);
settingLay = (LinearLayout)findViewById(R.id.settingsLayout);
reviewLay.setOnClickListener(this);
nearbyLay.setOnClickListener(this);
searchLay.setOnClickListener(this);
bookmarkLay.setOnClickListener(this);
settingLay.setOnClickListener(this);
btEdit.setOnClickListener(this);
Here I have used a layout instead of button and I am setting onclicklistener for each layout. Now override onclick method and do what you want with each layout

Categories

Resources