I am trying to make two TextViews that will be on the same row. The first one should be left aligned and the second one right aligned, when one of them becomes to big the first one (the left aligned should go on new row). I was able to make the first one to go on new line when the second TextView (the right aligned one) became too big but the problem is that it doesn't display the whole information from it. So anyone has any ideas. I'd like it to be with RelativeLayout but any help would be appreciated.
You should play with android:layout_weight to acheive this , this is how it should be :
<LinearLayout .....
android:orientation="horizontal" >
<TextView android:id="#+id/txtView1"
android:layout_height="wrap_content"
android:gravity="left"
android:text="TextView Left"
android:layout_width="0dp"
android:layout_weight="1" />
<TextView android:id="#+id/txtView2"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="0dp"
android:text="TextView Right"
android:layout_weight="1" />
</LinearLayout>
You should try putting the two textviews into a linearlayout, set the layout_weight="1" on both textviews and their layout_width="0".
give weight = 1 for both text views....
if it does't work....change relative layout to linear layout or table row and give weight to 1 for both text view
Put your TextViews in LinearLayout and set layout_weight=1. it will solve your problem.
Related
I am working on a text view which shows the number of selected items (from a multiple choice list) with the number having a circle around it. I thought to add the circle by using the setCompoundDrawablesWithIntrinsicBounds (as I don't want to have 2 separate text views to achieve the desired look), but my question is if it's possible to somehow apply some text (in this case the number) over the circle image or if not, what would be the best solution for the label I have in mind?
I need to implement something looking like this:
What is your advice? Thanks for the help in advance!
You can use LinearLayout for that instead of single TextView implement click of LinearLayout.
For XMl use this:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/line">
<TextView
android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_bg"/>
<TextView
android:id="#+id/txtDropDown"
android:layout_width="0dp"
android:weight="1"
android:layout_height="wrap_content"
android:drawable_right="#drawable/arrow_down"/>
</LinearLayout>
I'm trying to do something relatively simple but really struggling.
I want to do something like this:
Explanation:
I want to create a block of fixed height (say 100dp) that is separated into three cells (that are separated by the green dashed lines shown in the picture above). The cells should also be of fixed size
The text in each block would be updated at runtime, and I want an arbitrary number of these blocks
Thanks a lot in advance.
I would suggest that you use a cardview embedded in a recyclerview. then you can set the number of such blocks in run time.
If you wanted to fix the number of blocks, then you can use Relative Layout.
Sample Relative Layout can be :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container"
android:orientation="vertical">
<TextView
android:id="#+id/largetxt1"
android:layout_width="300dp"
android:layout_height="50dp"
android:text="TEXT"/>
<TextView
android:id="#+id/smalltxt1"
android:layout_width="300dp"
android:layout_height="20dp"
android:text="MORE SMALLER TEXT"
android:layout_below="largetxt1"/>
<Button
android:id="#+id/button1"
android:text="CLICK"
android:layout_toEndOf="#id/largetxt1"
adnroid:layout_width="60dp"
android:layout_height="70dp"
/>
.
.
.
.
..
</RelativeLayout>
This is the sample for the first set of boxes. You can set use the same and repeat it for the number of sets you wanted. Make sure to place layout of views correctly
you can use weightSum and layout_weight attribute xml file
As a beginner in Android, I'm trying to display a tree. I have already found how to add programmatically new TextViews but I don't know how to place them, I mean how to set their padding/margin depending on the father node.
Thanks a lot for your answers,
Baptiste
Dear first Use relative layout as parent and then add text view child.i did not add relative layout add relative layout as parent.
<TextView
android:id="#+id/txttxt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="node"
android:textColor="#color/white"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/txttxt12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_below="#+id/txttxt1"
android:text="Child"
android:textColor="#color/white"
android:layout_alignLeft="#id/txttxt1"/>
this is sample code i made for you. you can use according to your requirement.
I have been working on an app that shows statuses about a server. I have a CardView for each server, with a RelativeView inside. On the left is an image, aligned to the cards left. In the middle, I have a TextView, aligned to the image right. On the right, I have a TextView, aligned to the right of the card.
Basically, my issue is, without using a LinearLayout, how can I make it so the middle TextView does not overlap the right TextView, preferably in the layout's XML? The text in both views is dynamically long, making a LinearLayout not very preferable.
Here is a diagram of the Layout to help you picture what I'm talking about. Sorry for the external link, it was getting reformatted in the post.
1.Aline middle TextView to centerHorizontal of parent, give fixed width , margin left and right to it. Mention that it is right of another TextView by using layout_toLeftOf.
2.Also aligh right hand side TextView to right by using alignRightToParent = true. Then give left margin to it.
I tried by using below xml code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="#+id/sun"
android:background="#004700" >
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/sun12"
android:singleLine="false"
android:text="abcdgsss ssssssssssssssss ssssssss sssssssssssssssssssssss"
android:textColor="#fff"
android:textSize="16sp" />
<TextView
android :id="#+id/sun12"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:text="abcdgsss ssssssssssssssss ssssssss sssssssssssssssssssssss"
android:textColor="#fff"
android:textSize="16sp" >
</TextView>
</RelativeLayout>
I figured out how to do it programatically. Simply, you want to subtract the widths and padding of the surrounding views from the size of the container view, and set the leftover value to the text view's width. Here is an example:
int view_length = personViewHolder.container.getWidth() - personViewHolder.container.getPaddingStart() - personViewHolder.container.getPaddingEnd();
view_length = view_length - personViewHolder.object_to_left.getWidth() - personViewHolder.object_to_left.getPaddingStart() -personViewHolder.object_to_left.getPaddingEnd();
view_length = view_length - personViewHolder.object_to_right.getWidth() - personViewHolder.object_to_right.getPaddingStart() - personViewHolder.object_to_right.getPaddingEnd();
personViewHolder.view_to_set_width.getLayoutParams().width = view_length;
personViewHolder.view_to_set_width.invalidate();
You can place the views in a RelativeLayout (if they are not already in one), then and use the ToStartOf or ToEndOf attributes to align one of them to the start or end of the other. You could also use ToLeftOf or ToRightOf, but this is not recommended because in some locales you want the user to read from right to left instead of left to right. This will ensure that the two views never overlap (assuming you haven't placed negative margins on either of the views). This can be extended to as many views as you want, as long as you correctly configure their alignment attributes.
I have a table row in a linear layout
The table row is set to fill parent
I have three objects inside the tablerow a textview then a editbox and then a buttong
they all line up horizontally
i want the textview on the left to be fixed at 100p width
I want the button on the right to be fixed at 50dp width
how do i set the editbox in the middle so that all three fill the width of the screen which might vary from device to device
I have tried various combinations of match and wrap but cant seem to get it
Any ideas
Mark
You have to use layout_weight for particular child of your view group or use relativelayout may solve your problem and please post you layout XML.
Try the following, using layout_weight:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="#+id/textView" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_weight="1" />
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/button" />
</LinearLayout>
That's when LAYOUT WEIGHTS come in handy.
For the fixed elements, just assign width & height as usual.
For the elastic ones, set "0dp" as the size you want to be elastic, and enter a weight in the weight field (layout parameters of a linearlayout).
Which weight?
If you only have one elastic element, it doesnt matter. If you have 2, then the available space will be distributed proportionally between weights. So if you wanted 2 elastic elements, one twice the size of the other, you'd assign weights 1 & 2.
By the way, heavy use of LinearLayouts with weight are somewhat discouraged because it takes Android twice the time to calculate their size. If your table has a lot of rows, you will certainly notice it's not as fast as fixed elements.