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?
Related
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"
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.
I am working on a Android Project in which I need to design a User Interface like below image-
I need to show some text on top as Student App, then below that there will be different objects of a Student. Suppose if I have 10 Students then there will be 10 rows for each student. And in each row there will be image on left hand side, then in the middle some text will be there and then on the right hand side there will be another three text.
I made some progress and I have below code. But it's not exact the way I am looking for in the image.
<ImageView
android:id="#+id/icon"
android:src="#drawable/action_eating"
android:layout_width="60dp"
android:layout_height="60dp" />
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" >
<TextView
android:id="#+id/text1"
android:text="test"
android:textColor="#333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="#+id/text2"
android:text="test2"
android:textColor="#6699CC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
And I need to make that list scrollable.
Can anyone help me out with this? Thanks for the help.
Using Table view how you can create column
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 2 columns -->
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="#+id/textView1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:text="Column 2" />
</TableRow>
<!-- edittext span 2 column -->
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<EditText
android:id="#+id/editText1"
android:layout_span="2"
android:text="Column 1 & 2" />
</TableRow>
<!-- just draw a red line -->
<View
android:layout_height="2dip"
android:background="#FF0000" />
<!-- 3 columns -->
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<TextView
android:id="#+id/textView2"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button2"
android:text="Column 2" />
<Button
android:id="#+id/button3"
android:text="Column 3" />
</TableRow>
<!-- display this button in 3rd column via layout_column(zero based) -->
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<Button
android:id="#+id/button4"
android:layout_column="2"
android:text="Column 3" />
</TableRow>
<!-- display this button in 2nd column via layout_column(zero based) -->
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<Button
android:id="#+id/button5"
android:layout_column="1"
android:text="Column 2" />
</TableRow>
</TableLayout>
You need a ListView for your students list, and create a custom class that derives from ArrayAdapter<Student> and override getView(). The list of student objects gets passed to the adapter and the adapter gets passed to the ListView. If you're doing this for a class and your intention is to learn, this is great stuff to get familiar with and is very practical for professional Android development. For extra credit, find a video on YouTube called "World of ListView" from Google I/O 2012 and make your lists faster than the rest of your classmates.
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" />
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.