0

Here is what I am getting when I run my application on my device enter image description here

The part that I have problems with is the rows - with text, quote, and web. I dynamically inserted those rows into the scroll view at runtime. Here is my xml code that I used for layout inflating

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" >

    <TextView
        android:id="@+id/stockView"
        android:text="TextView"
        android:layout_weight="2" />

    <Button
        android:id="@+id/stockQuote"
        style="?android:attr/buttonStyleSmall"
        android:text="@string/get_stock_quote" 
          android:layout_weight="1" />

    <Button
        android:id="@+id/webStock"
        style="?android:attr/buttonStyleSmall"
        android:text="@string/go_to_website"
          android:layout_weight="1"  />

</TableRow>

My question is with this code, why is each element still taking the same amount of space. I know that because I specified the text view's layout_weight to 2, it should take up half of the width with each button taking 1/4 of the total width. I know this isn't bc of weight sum as weight sum by default is 4 in this case. Does anyone know how i can get the text view to take up 1/2 of its parent's width?

Squonk
  • 48,735
  • 19
  • 103
  • 135
committedandroider
  • 8,711
  • 14
  • 71
  • 126
  • Check my answer http://stackoverflow.com/questions/26071142/android-ui-default-margin-remove/26071377#26071377 – Piyush Sep 27 '14 at 07:30
  • thanks for trying but that didn't work. That did highlight the fact that all three had the same width – committedandroider Sep 27 '14 at 07:41
  • 1
    Along with the weight attribute try setting a view's width to 0. – Onik Sep 27 '14 at 20:09
  • Thanks but I dont think that will make a difference. At least thats what I got from this thread. http://stackoverflow.com/questions/26072522/what-are-the-default-values-when-you-dont-specify-height-and-width-for-elements – committedandroider Sep 27 '14 at 20:37
  • _"I dont think that will make a difference"_??? Can you explain? This is not what the official docs say about the `weight` attribute. Take a look at [this answer](http://stackoverflow.com/questions/25633660/android-tablelayout-remove-padding/25635441#25635441) with the references to what `TableLayout`, `TableRow` and `weight` represent. – Onik Sep 28 '14 at 15:10

2 Answers2

0

Check this it works..

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >

<TextView
    android:id="@+id/stockView"
    android:layout_width="0dip"
    android:text="TextView"
    android:layout_weight="2" />

<Button
    android:id="@+id/stockQuote"
    android:layout_width="0dip"
    style="?android:attr/buttonStyleSmall"
    android:text="get_stock_quote"
    android:layout_weight="1" />

<Button
    android:id="@+id/webStock"
    android:layout_width="0dip"
    style="?android:attr/buttonStyleSmall"
    android:text="go_to_website"
    android:layout_weight="1"  />

0

You should play with layout_span. Try to add android:layout_span="2" to your textview.

What is the equivalent of "colspan" in an Android TableLayout?

Community
  • 1
  • 1