I need to align button,and textview in on same line,button aligned right side and textview also aligned right side,
I tried lot of methods but aligned first textview then aligned button,
how to solve this problem please any one help and solve my problem in programatically,
aligned success in layout xml design but I need it programatically.
place both views inside your layout and set orientation to "horizontal".
<LinearLayout>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
linear layout can also be nested into one another!
if you want to implement more lines of views then you could just define a vertical layout (the main.xml will by default define one for you when first created) and inside that vertical linear layout just insert how many horizontal linear layouts (like the one I written above) as you wish.
Something like this should work. You should modify this to fit your needs (i.e. set the proper text size, width, height, etc).
TextView tv = new TextView(this);
tv.setText("Text");
Button bt = new Button(this);
bt.setText("Button");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.addView(tv);
ll.addView(bt);
setContentView(ll);
Related
I currently have a program that uses a linear layout that contains a Textview that is populated with x amount of values (dependent on arrayList size determined in separate activity) but Scrollview is not working as I anticipated.
Since Scrollview can only have one direct child, I have the Linear Layout with the TextView nested within; however when I wrap both of these within Scrollview, the Linear Layout is still not scrollable
//This is what I am using to populate the Linear Layout that I'm trying to make scrollable
TextView DisplayString = (TextView)findViewById(R.id.stringNumsCon1);
LinearLayout LinContainer = (LinearLayout)findViewById(R.id.LinLay);
Intent intent = getIntent();
ArrayList<String> timerVals = (ArrayList<String>)getIntent().getSerializableExtra("timerVals");
DisplayString.setTextSize(15);
DisplayString.setTextColor(Color.BLACK);
for(int i=0; i<timerVals.size(); i++){
DisplayString.append(timerVals.get(i));
DisplayString.append("\n");
}
//This is how I am trying to make it scrollable within the xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/LinLay"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="391dp">
<TextView
android:id="#+id/stringNumsCon1"
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical">
</TextView>
</LinearLayout>
</ScrollView>
First Activity (used to create arraylist that will populate Linear Layout on next activity)
Portion of second activity I wish to make scrollable (Blue lines delineating what part of page I want scrollable, the rest I want to remain static)
I expected this to make the Linear Layout portion scrollable, but I can see data is cut off and inaccessible when actually implementing this code
You won't need the ScrollView, neither the LinearLayout. Remove them from your layout.
All you will need is set in your TextView in xml:
android:scrollbars = "vertical"
so your final xml layout would be:
<TextView
android:id="#+id/stringNumsCon1"
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical"
android:scrollbars = "vertical" />
then set the movement method in onCreate of your activity/fragment as shown below:
Java
DisplayString.setMovementMethod(new ScrollingMovementMethod());
Kotlin
DisplayString.movementMethod = ScrollingMovementMethod()
P.S.1: You can find the original answer here
P.S.2: Consider to use camel case naming convention for your variable names. E.g. use displayString instead of DisplayString.
I have a TextView and a Button in an android XML layout(TextView is at left most side and Button is at right most side in the same line), as the text in TextView is dynamic so it should take a minimum width for some initial text to be visible. As width of devices changes the minimum width of TextView does not look good all the time.
For example if width of device becomes greater, then the minimum width alloted to the TextView would look so bad because it would not use more space available on the screen.
So what I want to do is to set the minimum width of TextView according to the position of Button present at the Right most side of the screen
Like minWidth = "leave this dp" form Button.
I hope you have understood it well if not then ask me more about it.
Seeking for attributes to use.
Use a LinearLayout with horizontal orientation put that TextView and Button inside that LinearLayout. Give weight of that TextView to 1 and set width to 0dp and use wrap_content for Buttn's width
Code snippet
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
Put button and textview inside linearlayout and set weight for textview and button according your requirement. it will set layout view according device screen size.
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 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.
how do I get a TextView into the visible area of a screen by scrolling a ScrollView, if the TextView is not a direct child of the ScrollView?
I've got a LinearLayout view that has a TextView at the top, then a ScrollView and below a Button:
<LinearLayout
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/s"
android:layout_height="0dip"
android:layout_weight="1" >
<TableLayout
android:id="#+id/t"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/r" >
<TextView
android:id="#+id/a"/>
<TextView
android:id="#+id/b"/>
<TextView
android:id="#+id/c"/>
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_height="wrap_content"/>
</LinearLayout>
That generated a wholly filled screen with the label at the top border, the button at the bottom border, and a table between it; depending on how many rows this table has, you can scroll it or not.
The contents of the table are generated by Java code, I just added one row as example of what is inserted there.
I now need to make sure a certain row (not necessarily the last one) is visible by scrolling vertically. I can access every element from s to c, but I can't figure out the code to make the ScrollView s scroll at all.
I tried requestChildRectangleOnScreen, scrollBy and, scrollTo, but possibly always with the wrong arguments.
For now I don't care whether TextView a is vertically centered or at the bottom or top border, it would indeed be great if it was visible at all. X-scroll should stay 0, ideally (i. e. leftmost position).
In case this is important: this function is only called when the user enters the screen with a special Intent that's telling the screen to scroll to TableRow x (it's just to show the latest change).
If you need even more information, please ask.
Thank you!
The problem was indeed that I called the function directly from a sub call of onCreate. This meant the layout wasn't done yet, thus all scrolling was done before the elements had a height and so it scrolled by 0 pixels.
The solution is to use View.post(Runnable r):
ScrollView s = (ScrollView) findViewById(R.id.s);
TextView a = (TextView) findViewById(R.id.a);
s.post(new Runnable() {
public void run() {
int[] location = new int[2];
a.getLocationInWindow(location);
int y = location[1];
s.getLocationInWindow(location);
s.scrollTo(0, y-location[1]); // or some other calculation
}
});