How to change/resize EditText in table layout? - java

I just want to resize the edittext so that it matches my texts. When I insert an edittext in a table layout in column 3 it fills the whole space of column 3 and looks big. I want to change the size. How is it possible?
Below is my .xml coding:
<TableRow>
<TextView
android:layout_column="1"
android:text="S"
android:layout_width="150dp"
/>
<TextView
android:layout_column="2"
android:text="C"
android:layout_width="75dp" />
<TextView
android:layout_column="3"
android:text="G"/>
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:layout_column="1"
android:id="#+id/s1"
android:text="Medium Text"/>
<TextView
android:layout_column="2"
android:id="#+id/c1"
android:text="Text"/>
<EditText
android:id="#+id/g1"
android:layout_column="3" />
</TableRow>

I understand your problem... i tried using wrap_content etc .. but didnt work you have to manually set the height of the EditText....
here's the code:
<?xml version="1.0" encoding="utf-8"?>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="3" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:layout_weight="1" />
</TableRow>

Try these,
1. Set the "stretchColumn" property of table layout.
2. Set the layout_width of the EditText to "wrap_content".
3. If you want you can fix the size of the EditText by manually setting
the layout_width value in dp or dip.

you don't need to specify the column number it picks it itself just place it there if you want edit text to fit the space just strech the column 1&2 in table layout
or you can add linear layout as the table row and assign weight

Try
<EditText
android:layout_width="fill_parent"
android:id="#+id/txthwdxml"
android:layout_height="wrap_content" />

Related

Change the text size of a TextView and a Column in TableLayout in Android

This is the layout
As you can see, I'm trying to stretch the text box so it will be next to the "NOPE" button. I'm being restricted by the column's size, and the text box's size, which I don't know how to change.
XML:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/editBtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_column="2"
android:onClick="editEvent"
android:text="EDIT" />
<TextView
android:id="#+id/attendingEventInListviewRow"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_column="38"
android:height="30sp"
android:tag="Events2goName"
android:text="Events2go"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="normal" />
<Button
android:id="#+id/cancel_arrive"
style="?android:attr/buttonStyleSmall"
android:layout_width="130dp"
android:layout_height="36dp"
android:layout_column="37"
android:layout_gravity="right"
android:layout_weight="0.08"
android:onClick="selectCancelArrive"
android:text="Nope" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableRow>
If you set width with hard code using dp it will not work properly with multiple screen. I don't know which screen size you testing you app .. you can set TextView android:layout_width="100dp" it will work for you I hope..
But best is
You can set android:weightSum in TableRow and set android:layout_weight Views under TableRow ... below code is done for you. And it will fit with all screen size..
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="10">
<Button
android:id="#+id/editBtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_column="2"
android:layout_weight="2"
android:onClick="editEvent"
android:text="EDIT" />
<TextView
android:id="#+id/attendingEventInListviewRow"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="wrap_content"
android:layout_column="38"
android:height="30sp"
android:tag="Events2goName"
android:text="Events2go"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="normal" />
<Button
android:id="#+id/cancel_arrive"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="36dp"
android:layout_column="37"
android:layout_gravity="right"
android:layout_weight="0.08"
android:onClick="selectCancelArrive"
android:text="Nope" />
</TableRow>
try this...
<TextView
android:id="#+id/attendingEventInListviewRow"
android:layout_width="0dp"
android:layout_weight=""
android:layout_height="wrap_content"
android:layout_column="38"
android:height="30sp"
android:tag="Events2goName"
android:text="Events2go"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="normal" />
For example, if you have 3 columns (100% weight) and you want the first column use a 40% of the total weight, put android:layout_weight="0.4",if the second column use a 50% put android:layout_weight="0.5" and the 3rd and last column uses 10% default android:layout_weight="0.1"

button onClick action

I am developing a contacts list android app and I am trying to insert 5 new edit text fields every time I press the add address button. I tried to make the text fields in the java file inside the onClick method but it didn't work, the thing is that I heard that you can add a layout inside another layout and now I am trying to add a layout with the 5 text fields inside the layout containing the button. This is the code of the .xml file containing some other fields and the add address button
<?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="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/add_contact_layout"
android:orientation="vertical" >
<EditText
android:id="#+id/first_name_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/first_name_hint" >
</EditText>
<EditText
android:id="#+id/second_name_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/second_name_hint" />
<EditText
android:id="#+id/mobile_phone_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/cell_phone_hint"
android:inputType="phone" />
<EditText
android:id="#+id/work_phone_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/work_phone_hint"
android:inputType="phone" />
<EditText
android:id="#+id/email_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/email_text_hint"
android:inputType="textEmailAddress" />
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/add_address_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_address_text" />
<Spinner
android:id="#+id/address_spinner"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</LinearLayout>
</ScrollView>
This is the code of a new .xml file containing the five edit text fields:
<?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" >
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/typeOfAdresstextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/spinner_value"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/address_street_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/street_hint"
android:inputType="textPostalAddress" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/address_number_editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/address_number_hint"
android:inputType="number" />
<EditText
android:id="#+id/address_city_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/city_hint"
android:inputType="textPostalAddress" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/address_state_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/state_hint"
android:inputType="textPostalAddress" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/address_zipcode_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/zipcode_hint"
android:inputType="number" />
</TableRow>
</LinearLayout>
My goal is to insert these 5 text fields below the button and the spinner, and every time I press the button these text fields appear again for the user to be able to add all the addresses that he/she wants (below the previous filled address text fields). I don't know if this is the right way but if there is another way to accomplish this I am open to suggestions. Here I leave a sketch of what I want to accomplish
You could either create a FrameLayout after your TableLayout in the first XML layout file then inflate the 2nd layout into the FrameLayout. Or, just embed the 2nd layout into the first within a containing layout (like the FrameLayout I suggested above) and mark the container as android:visibility="gone". When your button is pressed, change the visibility and your other 5 fields will become visible.

Why are my TextViews being moved around?

I am trying to display a single list item after the user clicks on an item. However, I have noticed that the layout is messed up when there is too much text. Sometimes it's pushed half way, sometimes it's completely to the right side of the screen. I don't understand why this is happening. How can I resolve this issue? Please note I have the exact same layout in a popup dialog with no problems.
This is my XML code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="#+id/add_spot_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="1" >
<TableRow
android:layout_marginBottom="1sp"
android:layout_marginTop="2sp" >
<TextView
android:id="#+id/nameDetails"
android:layout_width="fill_parent"
android:textSize="25sp"
android:textStyle="bold" />
</TableRow>
<!-- separator -->
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView
android:layout_marginLeft="5sp"
android:text="#string/distance"
/>
<TextView android:id="#+id/distanceDetails" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView
android:layout_marginLeft="5sp"
android:text="#string/address"
/>
<TextView android:id="#+id/addrDetails" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView
android:layout_marginLeft="5sp"
android:text="#string/type"
/>
<TextView android:id="#+id/typeDetails" />
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView
android:layout_marginLeft="5sp"
android:text="#string/terrain" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RatingBar
android:id="#+id/terrainDetails"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:stepSize="1" />
</RelativeLayout>
</TableRow>
<TableRow>
<TextView
android:layout_marginLeft="5sp"
android:text="#string/difficulty" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RatingBar
android:id="#+id/difficultyDetails"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:stepSize="0.1" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_marginBottom="2sp"
android:layout_marginTop="2sp" >
<TextView
android:layout_marginLeft="5sp"
android:text="#string/description" />
<TextView
android:id="#+id/descDetails"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:scrollHorizontally="false" />
</TableRow>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
</TableLayout>
</ScrollView>
Here are some screenshots to show you what I mean:
What it should be:
What it shouldn't be:
Pushed to the right side of the screen
Too much text in description. Goes off the screen
Pushed only half way.
The size of a column in a TableLayout is given by its largest content.
The way you organized your layout, the title nameDetails is in the first column.
You'll notice that, in the cases you don't like, the second column is neatly aligned with the end of your title.
To solve this, either move the title out of the table layout, or use the android:layout_span attribute to span it on 2 columns
I believe this is happening because you have your title #+id/nameDetails as the first row of the table. It is including the length of this <textview> cell when it determines the size of the left hand side of the table.
Can you move that textview out of the tableview?

RelativeLayout with MATCH_PARENT doesn't work

Here's what I want to achieve:
I have at table with a lot of table rows. Each row should have two text view with a product title and subtitle on top of each other (subtitle not implementet yet). The should be left aligned. To the right I want a spinner to select the quantity.
The texts come from the database and are of course of different lengths.
Here is what I've come up with so far:
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/barmenu"
android:background="#99ffff"
android:scrollbars="vertical">
<TableRow
android:layout_height="wrap_content"
android:background="#ffff99"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#0000ff">
<TextView
android:id="#+id/productTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#88ffff"
android:ellipsize="end"
android:padding="2dp"
android:singleLine="true"
android:text="Product name"
android:textColor="#000000"
android:gravity="left"
android:textSize="13sp"
android:textStyle="bold" />
<Spinner
android:gravity="right"
android:layout_height="40dp"
android:layout_toRightOf="#id/productTitle"
android:width="100dp"
android:layout_width="wrap_content" />
</RelativeLayout>
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:background="#ffff99"
android:layout_width="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:background="#0000ff">
<TextView
android:id="#+id/productTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#88ffff"
android:ellipsize="end"
android:padding="2dp"
android:singleLine="true"
android:text="Some long long long long name"
android:textColor="#000000"
android:gravity="left"
android:textSize="13sp"
android:textStyle="bold" />
<Spinner
android:gravity="right"
android:layout_height="40dp"
android:layout_toRightOf="#id/productTitle2"
android:width="100dp"
android:layout_width="wrap_content" />
</RelativeLayout>
</TableRow>
</TableLayout>
</ScrollView>
Screendump:
First problem is that the RelativeLayout doesn't fill out the width of the table row. And the second problem: the spinners are not right aligned.
What am I missing?
best regards
Allan
android:fillViewport="true"
add this line to your ScollView attributes and you're done :)
I'd make it simpler and lighter. Just use RelativeLayout for all these 4 views. Align to right both spinners and make textview's with - match_parent and add toLeftOf the relative spinner. I think you will see what you want.
Add android:layout_weight="1" to your relativelayout

Can someone explain why the row is not wrapping the text in this TableLayout?

I am having trouble with this for some reason... The column with the "name" shouldn't take up as much space as it does. The column does not wrap the text. The setup below is what I've been using and it has worked previously. What am I missing? Could there be something up with Eclipse?
<?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="fill_parent"
android:fadingEdgeLength="100dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#333333"
android:stretchColumns="*">
<TableRow android:layout_margin="1dp" android:background="#333333" android:gravity="center">
<TextView
android:gravity="center"
android:padding="3dip"
android:text="Friends"
android:textColor="#000000"
android:textSize="22dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#000000">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Name"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Address"
android:textColor="#66cccc"
android:textSize="15dp" />
<TextView
android:gravity="left"
android:padding="3dip"
android:text="Interests"
android:textColor="#006666"
android:textSize="15dp" />
</TableRow>
<TableRow android:layout_margin="1dp" android:background="#000000">
<TextView
android:gravity="left"
android:padding="3dip"
android:text="This is short"
android:textColor="#ffffff"
android:textSize="13dp"
android:width="0dip"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:gravity="left"
android:padding="3dip"
android:text="This is some text that might be too long and might need to wrap to the next line."
android:textColor="#cc0033"
android:textSize="13dp"
android:width="0dip"
android:layout_height="match_parent"
android:background="#222222" />
<TextView
android:gravity="left"
android:padding="3dip"
android:text="This is some text that will be long so it needs to wrap and go to the next line blah blah blah."
android:textColor="#99cc00"
android:textSize="13dp"
android:width="0dip"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
From the docs:
The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.
That might be causing some problems although the wording of that suggests to me that it should ignore your settings anyway and just overwrite them. Still, it might be worth getting rid of your layout_width and layout_height attributes in those TextViews in your TableRows and seeing if it helps.
Update:
Just noticed you have stretchColumns set to *. Change it to 1,2.

Categories