I have a TextView1, buttons1 and buttons2 Horizontally.
When Button2 click Button1 is invisible.
When Button1 is invisible, I need to increase the width of the textview to cover the space created when button1 is invisible and vice versa.
Can Please anybody help How can I change the Textview width dynamically.
NB: Textview tv.setWidth(pix) is not working. when I tried to find the value of tv.getWidth() gives always zero.
`
<TextView
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello" />
<Button
android:id="#+id/btn1"
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello"
android:visibility="visible" />
<Button
android:id="#+id/btn2"
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello" />
</LinearLayout>`you can use weight for three element and button1.setVisibility(View.GONE)
Please try to use GONE instead of using INVISIBLE. When using INVISIBLE even though you can't see it, it still takes a space.
Wrap your widgets in a RelativeLayout and then check. You will be able to bring your textview on top of your button if it is invisble. You cant do this if you are using LinearLayout because it sets widgets horizontaly or verticaly and does not overlap.
Related
I have a RelativeLayout and want to that my textfield will overlap over the given ImageButton. Currently the textfield is behind the imagebutton. I tried several options but did not find the correct one, any idea?
Thanks
<TextView
android:id="#+id/qrCode_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/qrCode_hint"
android:textSize="20dp"
android:layout_alignBottom="#+id/btn_start_barcode"
android:layout_marginBottom="53dp"
/>
<ImageButton
android:id="#+id/btn_start_barcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/select_form"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:background="#ffffffff"
android:onClick="startBarcodeAction"
android:src="#drawable/barcode_bg" />
The RelativeLayout sets the Z-Index based on ordering of children.
So when you add a view below the others it will set Z-Index of that child higher than the last child.
Use a FrameLayout and put in the following order:
ImageButton
TextView
This will ensure that the TextView will be on top of the ImageButton.
i am suppose to place a TextView right of a multiline TextView.
please check the code below .
<RelativeLayout
android:id="#+id/rltest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rlanswer"
android:layout_marginLeft="#dimen/grdiviewspacing"
android:layout_marginRight="#dimen/grdiviewspacing"
android:background="#android:color/transparent" >
<TextView
android:id="#+id/lblanswer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:ellipsize="end"
android:lines="1"
android:text="#string/strbookmarkmessage"
android:textColor="#color/black"
android:textSize="#dimen/listviewlocationsize"
android:textStyle="bold" />
<TextView
android:id="#+id/btnyesiwillcollectitby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#id/lblanswer2"
android:autoLink="all"
android:linksClickable="true"
android:text="#string/stryesiwillcollectitby"
android:textColor="#color/linkcolor"
android:textSize="#dimen/listviewlocationsize" />
</RelativeLayout>
i have tried the problem with the above code is its not showing the second TextView. where us if i try layout_below - its showing the second text view. can anyone please assists me..
i want something like this - blue textview text to read textview.
Use LinearLayout instead of RelativeLayout and give layout_weight=1 to both the TextView you will get result whatever you want.
If you want to keep using a RelativeLayout, you will have to set the width in dip (instead of wrap_content) for the TextView "lblanswer2", otherwise it will always take the whole place if your text is long.
If using a RelativeLayout is not a necessity, you can follow pratik's answer and implement a LinearLayout, this way your 2 TextViews will share the width equally.
I have an activity with two Buttons and a TextView in a LinearLayout. My TextView is offset downwards and the text doesn't fit inside the box. Can you explain what is happening? I think it is related to padding, and I've read several discussions about the perils of TextView padding, but that doesn't explain why the text is cut off at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#800080">
<Button
android:text="This"
android:background="#drawable/button_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="That"
android:background="#drawable/button_green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#533f93"
/>
</LinearLayout>
This code produces this display:
The purple is the LinearLayout, the blue is the TextView. As you can see, the TextView's top is below those of the buttons and its bottom is below the bottom of the LinearLayout. As I add text to the TextView, the LinearLayout increases its height appropriately, but because the TextView is offset, I always lose the bottom of the last line.
I ran Hierarchy Viewer and it gave me this wireframe:
Which shows the vertical offset at the top, but misses the bottom of the TextView. The same wireframe with the LinearLayout selected looks like this:
According to Hierarchy Viewer, the top of the buttons is at 0, but the top of the TextView is at 7. I've tried various fixes, mostly culled from this site:
android:paddingTop="0dp"
android:background="#null"
android:includeFontPadding="false"
None of these fixed my issue.
Set android:baselineAligned property of your LinearLayout to false.
From documentation:
When set to false, prevents the layout from aligning its children's
baselines. This attribute is particularly useful when the children use
different values for gravity. The default value is true.
give the layout_gravity of the Textview to be center_vertical
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#800080">
<Button
android:text="This"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="That"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#533f93"
android:layout_gravity="center_vertical"/>
</LinearLayout>
Try setting the layout_gravity for the TextView like this:
<TextView
android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#533f93"
android:layout_gravity="top"
/>
If you have this problem with several TextViews you can add android:gravity="center_vertical" to LinearLayout
I currently have this in my layout.xml
<EditText android:id="#+id/body" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:hint="Type to compose" />
<ImageButton android:id="#+id/attach"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/attach_photo" />
<Button android:id="#+id/send" android:text="#string/send"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
Problem I'm facing is the top of the ImageButton is not aligned like the Button and EditText. The other 2 fill up the height nicely but the ImageButton seems to be sticking out upwards. Any ideas?
Try using a RelativeLayout. You can align views to the sides, top and bottom of the parent or any other view.
Using android:layout_alignParentTop="true" or similar.
There are loads of xml commands you can use in RelativeLayout.
You can also set the layout weight property on the children of your linear layout. You can set the weight of edittext, imagebutton and button which will align your components on screen.
I have an EditText and a Button set next to each other on the same horizontal line. It looks great, except when the user enters a lot of text, the EditText is resized, and the Button is squished.
I have both EditText and Button set to layout_width="wrap_content". "fill_parent" messes up the layout, and I don't want to use absolute sizes if I don't have to - the way it is now looks great in both landscape and portrait, I just don't want the EditText to resize.
My layout:
<TableLayout
android:id="#+id/homelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<TextView
android:id="#+id/labelartist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Find artists:" />
</TableRow>
<TableRow>
<EditText
android:id="#+id/entryartist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:background="#android:drawable/editbox_background"
android:editable="true"
android:padding="5px"
android:singleLine="true" />
<Button
android:id="#+id/okartist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:text="Search" />
</TableRow>
</TableLayout>
For EditText use
android:layout_width="0dp" - not required but preferred.
android:layout_weight="1"
And for Button dont specify android:layout_weight
What I do is in the onCreate for the activity, measure the EditText first then apply its maxWidth.
You can do so using code similar to the following:
EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());
Give EditText a maxWidth. That should stop it from resizing beyond the width you provided. Also, if you want it to be contained within 1 line set the maxLines setting to 1 too.
You need to add android:imeOptions to prevent this behavior.
Just use:
android:layout_width="fill_parent"
for both EditText and Button. It will also work set for EditText but it is better to have it on both.
you can define size of your edittext as it shows above
android:minWidth="80dp"
android:maxWidth="80dp"
or
android:layout_width="60dp"
or
you can apply a background image of size you want
but don't forget to define its height and width
with wrap_content.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
I had a similar problem, but I couldn't get the above to work. What I did, is take in the text from EditText box and then format it down to the max size that my screen could handle. When I save this off to the DB, I will save it as display Text and original text.
If you go to this page I put a longer explanation.
The correct way to do this would be to set the android:minWidth and android:maxWidth equal to the width you want. This way you will not have to adjust your layout to use the android:layout_weight.
For example if you want your EditText to always be 80 density pixels no matter how many characters you type in use the following:
android:minWidth="80dp"
android:maxWidth="80dp"
I had the exact query. But i had an EditText and a Button next to each other in a Linear Layout.
I tried android:layout_weight="1" for EditText
android:layout_width="0dp"
And it worked just perfect!!
Thanks a lot!
The chosen solution works, but let me add a little complement:
if you use
android:layout_weight="0.15" (the value is not important)
then
android:layout_width="0dp" if the LinearLayout is Horizontal
or
android:layout_height="0dp" if the LinearLayout is Vertical.