Make EditText height dynamically adjust - android

The first picture shows an EditText of a fixed size. What I want to have is the height should adjust to fill the the space available above the spinner.
I may increase the the size of the height but I want to be the same for different screen sizes. One more thing I need to note is that I want the feature in the second picture to be preserved. When the keyboard appears, I want the spinner to move up and not be hidden underneath.
Thank you
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/textify" />
<ScrollView
android:id="#+id/myScrollView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/widget37"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/field"
android:layout_width="match_parent"
android:layout_height="321dp"
android:layout_marginTop="10dip"
android:hint="#string/text_hint"
android:inputType="textAutoCorrect|textMultiLine"
android:gravity="left|top"
android:singleLine="false"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:overScrollMode="always"
android:textSize="18sp" >
</EditText>
</LinearLayout>
</ScrollView>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="51dp" />
</LinearLayout>

Noob here, but I would also recommend using the layout_weight in your linear layout in hopes that the EditText would adjust according to other view elements. Or it wouldn't be hard to switch to a RelativeLayout and link the EditText bottom with the Spinner top.

Related

Static footer is overlapped by input panel

I am trying to create a screen where you have a ScrollView on top and under that you have a TextView and a Button.
The ScrollView contains a TableLayout which it self contains TextView's, Button's and EditText's.
What I would like to achieve is that when a EditText is focues and the input panel opens on the phone, the buttom footer would be moved on top of the panel and the scroll area resized smaller, so that you can click the calculate button at any moment.
Here is my current xml:
The activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/activity_germinative_scroll"/>
<TextView
android:id="#+id/textViewCalculationResult"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".70"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:gravity="center"
android:text="0.00 KG/HA"
android:textSize="35sp"/>
<Button
android:id="#+id/calculateGerminative"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".30"
android:text="#string/calculate_germinative"
android:textSize="25sp"
android:textAllCaps="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:background="#drawable/menu_button_states"
android:nextFocusDown="#id/editSeedMass"/>
</LinearLayout>
Scroll activity:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical"
android:showDividers="beginning|end"
>
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:padding="10dip"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="#+id/teraMass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".40"
android:textSize="20sp"
android:editable="false"
android:text="#string/seed_mass_text"
android:textAllCaps="true"/>
<Space
android:layout_width="13dp"/>
<Button
android:id="#+id/seedMassInfo"
android:tag="seedMassInfo"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/info"/>
</TableRow>
<Space
android:layout_height="3dp"/>
... Many other TableRow's with the exact same layout...
</TableLayout>
</ScrollView>
So what happens right now is that when I focus on one of the input fields in my scroll area, the button at the bottom gets resized into a very thin little button and depending of the screen size, the text-field in the footer will get resized also.
I use this setting for the activity in the manifest:
android:windowSoftInputMode="stateHidden|adjustResize">
Is the problem because of the overall layout I use or what?
You should set fixed height for the Button calculateGerminative and TextView textViewCalculationResult, and set the scrollview height to dynamic, i.e.
android:layout_height="0dp"
android:layout_weight="1"

Distribution of objects in an activity

I am performing correction on some screens, and on the screen I'm working now, there was a difficulty.
I have a few fields on screen, and even trying to relate the button with the bottom of the screen, the button is in the center.
And it is a large white part after button. I do not know what I can do to fix it. Note: I created the LinearLayout to try solucinar the problem and positions them.
My Code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000">
<LinearLayout
android:id="#+id/LinLayoutSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner_TamPet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/tamPet_array"
android:prompt="#string/tamPet_prompt"
android:layout_marginTop="50dp" />
<EditText
android:id="#+id/char_SobreVoce"
android:inputType="textMultiLine"
android:lines="8"
android:minLines="6"
android:maxLines="10"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#id/spinner_TamPet"
android:background="#drawable/edit_text"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:layout_below="#id/LinLayoutSpin">
<Button
android:id="#+id/btnCadastraCliente"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sou um DoggieWalker"
android:layout_below="#id/char_SobreVoce"
android:background="#drawable/btn_background" />
</LinearLayout>
</RelativeLayout>
The new print after first comment. The white part is up and the form down.
Set android:fillViewPort = "true" in your ScrollView.
In the second LinerLayout set android:layout_height="wrap_content"
you can optimize your screen layout by removing these LinerLayout, only need one RelativeLayout for this screen
Set layout_gravity to bottom for the relative layout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout_gravity="bottom"
android:background="#ff000000">

How to make widget in the middle of layout stretch to fill the parent in Android

I have an Android layout question. I have a display where I am putting some text into the middle of a view that has a header at the top and three buttons at the bottom. What I am having trouble with is getting the TextView (which is inside a ScrollView) to stretch to fill up the available screen space. If I set the layout_height of the ScrollView to "fill_parent" the three buttons are pushed off the bottom of the screen. If I set it to "wrap_content" it is only as large as is needed to support the text that is put into the TextView.
Here is the XML for the layout I am using:
<?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="fill_parent"
android:orientation="vertical" >
<LinearLayout android:orientation="horizontal"
android:gravity="top"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
style="#android:style/TextAppearance.Large"
android:text="#string/meetingdetail" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/meeting"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium" />
</ScrollView>
<Button android:id="#+id/navigation"
android:text="#string/naviation"
android:onClick="meetingInfoOnClick"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="#+id/mapsingle"
android:text="#string/mapsingle"
android:onClick="meetingInfoOnClick"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="#+id/goback"
android:text="#string/goback"
android:onClick="meetingInfoOnClick"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Could someone suggest what would work best to make the ScrollView with the TextView fill up the available space but still allow the three buttons to appear?
Set the height of the ScrollView to 0dp and its weight to 1.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView android:id="#+id/meeting"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium" />
</ScrollView>
However if you don't plan to have more than one views scrollable you should remove the ScrollView and use:
android:scrollbars="vertical"
attribute on your TextView since the TextView is scrollable itself.
Just replace your ScrollView tag code to..
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView android:id="#+id/meeting"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="#android:style/TextAppearance.Medium" />
</ScrollView>
It will work fine, if still you have any problem then tell me. if works then accept as answer.

Make a component fill all available space on screen

I want something like this:
But I don't know how to resize the TextView so it gets all the available space on screen which is not ocuppied by the EditText or the Buttons. May I do it in the code, or in the xml?
At XML I tried putting the TextView into a FrameLayout, but it makes no difference. Currently looks like:
<?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">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/consola"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars = "vertical"
android:text="#string/hello"/>
</FrameLayout>
<EditText
android:id="#+id/comando"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="Conectar"
android:id="#+id/boton_conectar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Enviar"
android:id="#+id/boton_enviar"
android:layout_width="wrap_content"
android:layout_weight="0.5"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
At the code I'm just checking if the Buttons are pushed with Listeners. One of them, when pushed, gets the text at the EditText, and appends it to the TextView. It works, and TextView gets higher, while EditText and Buttons downs one line. If I go on appending lines, finaly EditText and Buttons get out of the screen. I want to avoid this behaviour, and accomplish to get this 3 widgets sticked to the bottom of the screen.
Use the android:layout_weight=1 attribute, like the buttons on the bottom of the form. That will assign most of the space to it and anything that's left to the rest of the elements.
It's all about the weight. This should give you what you want:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="TextView"
android:id="#+id/textView1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</TextView>
<EditText android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:text="Button"
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp">
</Button>
<Button android:text="Button"
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1">
</Button>
</LinearLayout>
</LinearLayout>
(Side note: When using weight, setting the corresponding height/width to 0dp sometimes gets around some weird behavior.)
Try android:fillViewport="true".

Android: even spacing between view aligned vertically

My layout have 4 EditText views aligned vertically. but there is empty spacing after the last EditText. I want to align these views evenly so that the spacing b/w, before and after the views is same.
How to achieve this?
--nehatha
You can wrap every of your EditText to a FrameLayout with equal layout weight and set the Gravity of those frame layouts to center (or layout_gravity of EditTexts).
Edited:
Well, my initial solution leaves less space at top and bottom than between the edit texts. But this one works fine: simply add FrameLayout between each view and at top and bottom with layout_weight="1":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
></FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
<FrameLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
>
</FrameLayout>
</LinearLayout>
if you are using xml, then in all EditText add this tag,
android:layoutMargin = "5dip"
and in your parent layout (i ll assume its LinearLayout )
android:layoutHeight = "wrap_content"
Updated
adding hard coded values does't harm your code, since "dip" and "dp" are all units which get adapted to different screen resolutions. it has zero affect evn if the screen resolution changes..
chek this.. http://developer.android.com/resources/samples/MultiResolution/index.html

Categories

Resources