android weight sum - android

i have defined weightsum for one 1 tablerow and layout_weight. this is what i am doing
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<TextView
android:id="#+id/add"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_weight="0.2"
android:text="#string/restaurant_add"
android:textStyle="bold" />
<TextView
android:id="#+id/Restaurant_add"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_weight="0.8"
android:clickable="true" />
</TableRow>
i am filling the data to Restaurant_add dynamically.. but when i fill the data to Restauant_add, even though i have set the layout_weight to 0.8, it is overriding the data present in android:text="#string/restaurant_add".
what is the mistake i am doing?
Thanks:)

Can you try to set android:layout_width="0dp" to your two TextView ?
Also, your TextView's id Restaurant_add is the same as your string #string/restaurant_add. Make sure that you modify the layout and not your string !

Set both your android:layout_widths to "0dp".

Related

TableLayout refuses to match_parent

I have an xml layout which serves as an item in a ListView. The thing is: when it's added there, the item's width isn't stretched to match_parent, it wraps content instead. It is used in a ListView, so its width needs to be match_parent. I've been reading about it all over stackoverflow, trying every solution availible, but I have had zero result: layout_weight set to 1 does nothing to my layout, stretching columns has given me the best result in terms of width, but in terms of layout itself it failed miserably.
Is there anything that can be done here?
<?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="wrap_content"
android:descendantFocusability="blocksDescendants">
<TableRow
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<CheckBox
android:id="#+id/trackCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:clickable="false"
android:duplicateParentState="true" />
<TextView
android:id="#+id/trackNumberText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="00."
android:textColor="#android:color/black"
android:textSize="18sp" />
<TextView
android:id="#+id/trackTitleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="Title"
android:textColor="#android:color/black"
android:textSize="18sp" />
</TableRow>
<TableRow
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/trackArtistText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:text="Artist" />
</TableRow>
</TableLayout>
Add android:stretchColumns="*" property in your TableLayout
android:stretchColumns
The zero-based index of the columns to stretch. The column indices
must be separated by a comma: 1, 2, 5. Illegal and duplicate indices
are ignored. You can stretch all columns by using the value "*"
instead. Note that a column can be marked stretchable and shrinkable
at the same time.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:descendantFocusability="blocksDescendants">
.....
</TableLayout>
Hope this will help.
I think what you want is tableLayout.setStretchAllColumns(true);. Unfortunately you cannot set that in xml, perhaps place it in your adapter.
None of the mentioned solutions have helped me, so trying to solve this for 3 days I found a solution myself. Unfortunately, the solution is programmatical, the following code is to be added to your custom adapter:
trackCheckBox.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
trackNumberText.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int width = context.getResources().getDisplayMetrics().widthPixels - trackCheckBox.getMeasuredWidth() - trackNumberText.getMeasuredWidth();
trackArtistText.setWidth(width);
trackTitleText.setWidth(width);

How to create an EditText of dynamic fixed width

I'm working on an app where my xml looks a bit like this:
<TableRow
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/receiver_name"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:maxLength="30"
android:maxLines="1" />
</TableRow>
What I want, is that no matter what device you have, the TextView should take up 1/4th of the width, and the EditText should take up 3/4ths of the width.
As it is now, the code works - until you start typing. The EditText gets wider and wider as you type more and more into it. How do I make it so that my EditText stays the same width that is assigned to it by the layout_weight parameters?
you should use LinearLayout like that :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/receiver_name"
android:layout_weight="1" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:maxLength="30"
android:maxLines="1" />
</LinearLayout>
hope that can help you =)
Just like Janoub said you could use the LinearLayout and weights value.
http://developer.android.com/guide/topics/ui/layout/linear.html
But you can also use table layout with 4 columns so your edittext catches 3 columns and the textview 1.
http://www.compiletimeerror.com/2013/07/android-tablelayout-example.html

How to create the layout for android as on image?

How to create the layout for android as on image?
I try next code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kkkkddd"
android:layout_marginLeft="100dp"
android:id="#+id/text_email" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/prompt_email"
android:id="#+id/text_label_email"
android:layout_toLeftOf="#id/text_email" />
...
but label_email is hidden.
You should place your label-TextView first and set for it fixed width. Than place value-TextView to right of label:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="#string/prompt_email"
android:id="#+id/text_label_email"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="kkkkddd"
android:toRightOf="#id/text_label_email"
android:id="#+id/text_email" />
Try Linear layout with orientation as horizontal :)
Imo a LinearLayout would fit better for this purpose. For instance:
<LinearLayout
android:orienation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="kkkkddd"
android:layout_marginLeft="100dp"
android:id="#+id/text_email" />
<TextView
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="#string/prompt_email"
android:id="#+id/text_label_email"
android:layout_toLeftOf="#id/text_email" />
</LinearLayout>
and you could do this for each pair
make parent Linearlayout. And the use inner linearLayouts. In innerlayouts give gravity horizontal. then give .5 weight to its both childs i.e for "Email" textview & "utkin#gmail.com" weight should be .5 for both. set right alignment for "Email" and set left alignment for "utkin#gmail.com".
It will work perfectly.
Voteup or mark true if helpful

How to align spinner and textview in android table layout?

I'm creating one table layout with text view and spinner.
My textview content is too large, so it's wrapped up in my layout. Whenever the textview having wrapped, the same row spinner also increased the height.
My code is,
<TableRow
android:id="#+id/trConfigPeriodStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:weightSum="1" >
<TextView
android:id="#+id/tvConfigPeriodStop"
android:text="Periodic reporting interval while moving"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:textColor="#color/gpsblue"
android:layout_marginLeft="5dp" />
<Spinner
android:id="#+id/spnConfigPeriodStop"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:entries="#array/periodstop_arrays"
android:prompt="#string/periodstop_prompt"
/>
</TableRow>
Suppose I'm changing the layout height in wrap_content in spinner, that time my text view text also hide from view.
How to solve this issue?
Please do the needful.
Thanks in advance.
Try setting layout height as wrap_content for both Spinner and TextView.
Try this :
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trConfigPeriodStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textColor="#color/gpsblue"
android:weightSum="1" >
<TextView
android:id="#+id/tvConfigPeriodStop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="0.5"
android:text="Periodic reporting interval while moving" />
<Spinner
android:id="#+id/spnConfigPeriodStop"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:entries="#array/periodstop_arrays"
android:prompt="#string/periodstop_prompt" />
</TableRow>
i guess it is because tablerow has android:layout_height="wrap_content" and inner textview has android:layout_height="fill_parent", so the height of the tablerow will fit to the height of the spinner, that is the only child with fixed and independent height while rendering.
you will solve it if you change TextView's height also to android:layout_height="wrap_content"

Set a ViewFlipper weight programmatically

I'd like to change weight of a ViewFlipper programmatically.
I have a TableRow in which I have 2 columns. The first column contains a RelativeLayout element when the second column contains a ViewFlipper.
My ViewFlipper contains 2 views. Somehow in my Java code, I change the visible view of my ViewFlipper when I click on a button. What I want to do is to be able, when I change the ViewFlipper view (by calling showNext()), to change the ViewFlipper weight in order for it to be larger in the screen.
In other words, the first column of my row has a weight=1 and the second (the ViewFlipper) has a weight=0.8. When I click on a button I want to change those weights programmatically.
Is that possible ? How ? Below is my XML code.
I've searched on the Web, I found this solution:
myFlipper.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 0.3f));
This doesn't work, it crashes, which is, I think normal as the ViewFlipper isn't a LinearLayout.
Here is my XML code:
<TableRow
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000" >
<RelativeLayout
android:id="#+id/rltest"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_centerInParent="true"
android:paddingLeft="6px"
android:paddingRight="6px"
android:background="#00000000" >
<TextView
android:id="#+id/tvtest"
android:layout_width="match_parent"
android:layout_height="60px"
android:layout_alignParentLeft="true"
android:textColor="#FFFFFF"
android:textSize="22px"
android:singleLine="true"
android:background="#00000000"
android:gravity="center_vertical" />
</RelativeLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="#00000000" >
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="60px" />
<LinearLayout
android:id="#+id/lltest"
android:layout_width="match_parent"
android:layout_height="60px"
android:layout_centerInParent="true"
android:background="#00000000"
android:gravity="center" />
</ViewFlipper>
</TableRow>
Do you have any idea ?
Thanks
My guess: you should be using TableRow.LayoutParams, not LinearLayout.LayoutParams. But that's just a guess without seeing the actual error; any time you ask a question about a crash on SO you should really include the log with the traceback of your error.

Categories

Resources