EditText position within a Layout shifting - android

I have the following requirement:
i> Consider a layout tab_container of height 40 dp.
ii> The layout is divided into two equal individual parts in a
horizontal manner.
iii>Within each individual layout , there is a TextView and EditText
The issue , for example , I enter a value 3 on edit_text_1 and then tap on edit_text_2 and enter 222, both the layouts shifts a bit (right or left) .
This issue occurs when value is entered on edit_text_2 and edit_text_1 is tapped.
<LinearLayout
android:id="#+id/tab_container"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/txt_view_1"
style="#style/button_text_style_white_hdpi"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.6"
android:gravity="center"
android:text="Sample"
android:textSize="14sp"
continental:typeface="roboto_bold" />
<EditText
android:id="#+id/edit_text_1"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_weight="0.4"
android:background="#color/White"
android:gravity="center_vertical"
android:inputType="number"
android:maxLength="3"
android:textColor="#android:color/black"
android:textIsSelectable="false"
android:textSize="14sp" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="#+id/txt_view_2"
style="#style/button_text_style_white_hdpi"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.6"
android:gravity="center"
android:text="Sample2"
android:textSize="14sp"
/>
<EditText
android:id="#+id/edit_text_2"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_weight="0.4"
android:background="#color/White"
android:gravity="center_vertical"
android:inputType="number"
android:maxLength="3"
android:textColor="#android:color/black"
android:textIsSelectable="false"
android:textSize="14sp" >
<requestFocus />
</EditText>
</LinearLayout>
</LinearLayout>
Any suggestions will be helpful.

Related

Android: Ensuring EditText boxes fit horizontally across screen?

I am trying to allign 3 EditText boxes across the screen horizontally in my activity so that the user is able to enter data.
Currently the code that I am implementing doesnt allow the 3 boxes to fit on the screen. Only 1 box and half of the other are visible
How can I adjust my current code so that all 3 Edittext boxes are visible to the user?
Note: The button and textView at the bottom of the layout are also currently not visible, eventhough they should be under the 3 horizontally alligned Edittext boxes.
Current XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSearchDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Enter Date to search..."
android:textSize="15dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/etEnterSpecificyear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="" >
</EditText>
<TextView
android:layout_width="3dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<EditText
android:id="#+id/etEnterSpecificMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="" >
</EditText>
<TextView
android:layout_width="3dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<EditText
android:id="#+id/etEnterSpecificDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="" >
</EditText>
</LinearLayout>
<Button
android:id="#+id/btnSearchDate"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Submit"
/>
<TextView
android:id="#+id/tvSearchResultsDate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="30"
android:minLines="5"
android:scrollbars="vertical"
android:text=" Results..."
android:textSize="15dp" />
</LinearLayout>
For each of your edittext make this:
<EditText
android:id="#+id/etEnterSpecificyear"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:hint="" >
</EditText>
And delete the TextView's that are in between the EditText's
Your issue is that you are using android:ems="10" in EditText properties. This one makes the minimum width of the EditText to be exact ten "M" characters. If this is your requirement I would suggest to use android:orientation="vertical" in the parent LinearLayout. So your final layout will look like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tvSearchDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Enter Date to search..."
android:textSize="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/etEnterSpecificyear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="">
</EditText>
<EditText
android:id="#+id/etEnterSpecificMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="">
</EditText>
<EditText
android:id="#+id/etEnterSpecificDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="">
</EditText>
</LinearLayout>
<Button
android:id="#+id/btnSearchDate"
android:layout_width="85dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="Submit"
/>
<TextView
android:id="#+id/tvSearchResultsDate"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="30"
android:minLines="5"
android:scrollbars="vertical"
android:text=" Results..."
android:textSize="15dp"/>
</LinearLayout>

auto grow edittext below buttons above keybord

Yet another same problem for android which drive developers crazy!
I want to make buttons above keyboard...
Here is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/home"
android:layout_width="910dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:keepScreenOn="true"
android:orientation="vertical"
android:padding="15dip" >
<com.custom.webview
android:id="#+id/webView1"
android:layout_width="900dp"
android:layout_height="350dp"
android:visibility="gone" >
</com.custom.webview>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:paddingBottom="5dip"
android:paddingTop="20dip"
android:text="#string/choose_the_outcome"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:entries="#array/call_outcomes" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:paddingBottom="5dip"
android:paddingTop="20dip"
android:text="#string/notes"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="sans-serif-light"
android:inputType="textMultiLine" >
</EditText>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="25dip" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:onClick="onCancel"
android:text="#android:string/cancel" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:onClick="onSave"
android:text="#string/save" />
</LinearLayout>
</LinearLayout>
just move all your layoouts as they are in the RelativeLayout. It might fix an issue.
Add this line
android:windowSoftInputMode="adjustResize|stateHidden"
to AndroidManifest.xml <activity> tag
You need to to do the following:
Just Make the text view child of a spinner
or put text view height = wrap_content.

Layout TextView Hint not always showing

I have 4 textview, all numbers. The first one "#" is the hint and it shows up on my 4.2 and 2.2. The next three are "Hours, Minutes, Seconds" respectively and the hint shows up on 4.2 but not 2.2. Here is my xml for the them.
First Textview has # and the next three will say Hour, Minute, Second.
<ScrollView
android:id="#+id/mainBodyScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/calculateButton"
android:layout_alignParentLeft="true"
android:layout_below="#+id/perDistanceLayout"
android:fadeScrollbars="false"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<LinearLayout
android:id="#+id/mainBodyLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_distance"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/distanceEntryLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" >
<EditText
android:id="#+id/distanceText"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/default_number_hint"
android:inputType="number"
>
</EditText>
<Spinner
android:id="#+id/spinnerInput"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:entries="#array/spinner2_array" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_in"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="#+id/timeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<EditText
android:id="#+id/hourText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="#string/default_hours"
android:inputType="number" />
<EditText
android:id="#+id/minuteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="#string/default_minutes"
android:inputType="number" />
<EditText
android:id="#+id/secondText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="#string/default_seconds"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/paceclearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="5dp"
android:minHeight="32dip"
android:text="#string/default_clear"
android:textSize="12dp" />
</LinearLayout>
</ScrollView>
Somehow setting center_horizontal value in the android:gravity is causing issues in Android 2.2 in displaying the hint.
Remove center_horizontal from the value of android:gravity to display the hint.

spinner not align equal to edittext box

How do I set spinner? In my code spinner and edit text box layout margin left and right is look equal in simulator but when I deploy on real device spinner look bigger form left and right side as compare to edit text box how do I set equal both edittext box and spinner in all screen?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="0.2"
android:weightSum="1"
android:orientation="horizontal" >
<EditText
android:id="#+id/txtMobileNo"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="45dp"
android:layout_marginLeft="45dp"
android:layout_height="wrap_content"
style="?android:attr/buttonStyleSmall"
android:gravity="left"
android:hint="#string/MobileNo"
android:background="#ffffff"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:weightSum="1"
android:orientation="horizontal" >
<EditText
android:id="#+id/txtPinNo"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="45dp"
android:layout_marginLeft="45dp"
android:layout_height="wrap_content"
style="?android:attr/buttonStyleSmall"
android:inputType="textPassword"
android:gravity="left"
android:background="#ffffff"
android:singleLine="true"
android:hint="#string/PinNo"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:weightSum="1"
android:orientation="horizontal" >
<Spinner
android:id="#+id/lgnspinner"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:prompt="#string/network_prompt"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_marginTop="-15dp"
android:layout_marginBottom="-17dp"
android:layout_marginRight="41dp"
android:layout_marginLeft="41dp"
android:layout_height="wrap_content" />
</LinearLayout>

how to bring edit text in front when it has focus or is selected in android

I have 3 edit texts on a screen. On small screen devices before touching a edittext my screen looks like
And after touching first edittext(e.g. in front of name), keyboard popup comes, and it looks
i.e. now edittext Amount(which in front of TextView named Amount) is now in front of the user. But as I have touched(or clicked) first(Name) edittext, I want it in front of the user. But no matter which I select every time Amount is in front.
I have tried using requestFocus() and setSelect(true). Even has focus returns true, but nothing works.
So how to do that?
I have given scroll view to the tablelayout which contains these three edittexts and three text boxes in front of them.
my xml code is
<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" >
<LinearLayout
android:id="#+id/rl_upload_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:minHeight="50dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/selected_image_detail"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/rl_upload_top"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_weight="4"
android:baselineAligned="false"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:contentDescription="#string/empty" >
</ImageView>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="2"
android:gravity="center" >
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_table" />
<EditText
android:id="#+id/ed_img_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/text_box_background"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="#string/name"
android:paddingLeft="5dip" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dip" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/amount_table" />
<EditText
android:id="#+id/ed_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/text_box_background"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="#string/amount"
android:paddingLeft="5dip" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dip" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/desc_table" />
<EditText
android:id="#+id/ed_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#drawable/text_box_background"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="#string/desc"
android:paddingLeft="5dip" />
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:id="#+id/rl_upload_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:minHeight="70dp" >
<Button
android:id="#+id/button_update"
android:layout_width="150dp"
android:layout_height="40dp"
android:background="#drawable/btn_update" >
</Button>
</LinearLayout>
Add below line to manifest for related activity,
android:windowSoftInputMode="adjustPan"

Categories

Resources