I am using segmented control from https://github.com/Kaopiz/android-segmented-control and it works quite fine except last button is always squeezed like this:
I am adding RadioButtons dynamically using simple layout:
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="#style/RadioButton" />
I tried variations like Layout_width="wrap content" and removing "layout_weight" - same result.
Parent layout of the segmented control itself also gives no hints:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#color/white"
android:gravity="center"
android:minHeight="60dp">
<info.hoang8f.android.segmented.SegmentedGroup
android:id="#+id/segmentedGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
segmentedgroup:sc_border_width="1dp"
segmentedgroup:sc_corner_radius="10dp"
segmentedgroup:sc_tint_color="#FFEB3B">
</info.hoang8f.android.segmented.SegmentedGroup>
</RelativeLayout>
Answer found by chance at this topic: Programmatically-added RadioButtons refuse to obey LayoutParams weighting
Just setting layout params programmatically before adding Button to RadioGroup reached the desired effect:
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
radioButton.setLayoutParams(layoutParams);
Related
I'd like to use a RadioGroup with RadioButton in a Dialog. I can have few RadioButton & I can have a lot of them, that's why I wrapped RadioGroup in a ScrollView. My layout looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_title_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn_cancel"
android:layout_below="#+id/tv_title_settings">
<RadioGroup
android:id="#+id/radio_options_settings"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<Button
android:id="#+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
For the case with a lot of views I get:
The problem is with few elements:
I want it to ignore this empty space. I was trying with LinearLayout but the items overlap my button. Is there a workaround for this?
P.S. I can put RadioButton elements in a RecycleView but I have to restart it every time user selects a new RadioButton to achieve RadioGroup property - so I refused this idea.
I am writing an Android app that requires me to programmatically add a TextView to a TableRow which is, in turn, added to a TableLayout. For the design to look proper, the TextView width must be the same as the TableRow width. The problem I am having is that even though I have attempted to set the width of the TextView to match using MATCH_PARENT, it is not taking effect.
Here is a sample of the Java that I am trying to use to do this:
final TableRow myTableRow = new TableRow(getApplicationContext());
final TextView myTextView = new TextView(getApplicationContext());
myTextView.setWidth(TableRow.LayoutParams.MATCH_PARENT);
myTextView.setPadding(5, 0, 5, 0);
myTextView.setTextColor(Color.BLACK);
myTextView.setTextSize(30);
myTextView.setBackgroundColor(Color.RED);
myTextView.setText("1");
myTableRow.addView(myTextView);
myTableLayout.addView(myTableRow); // Assume 'myTableLayout' has already been declared.
Here is the XML for myTableLayout:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myTableLayout">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowOne">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textViewOne"
android:layout_weight="1"
android:textSize="40sp"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowTwo">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textViewTwo"
android:textColor="#000000"
android:textSize="30sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tableRowThree">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewThree"
android:textColor="#000000"
android:textSize="20sp"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:layout_weight="1" />
</TableRow>
</TableLayout>
Does anyone have any idea what I could be doing wrong?
Here is a picture of the way the layout appears. The code posted here is not identical to the actual app code, but it is enough to show the problem. Specifically, the code that was removed concerns the replies. In the example code posted as part of the question, only one TextView is shown in the Java code, but in the actually project, there are two.
Layout Example Image
There are several ways you can handle this.
TableLayout and TableRow both extends linearlayout, so maybe you need to set Orientation.
you could set the layout params(margin params) like this
layout_width = 0dp ;
layout_weight = 1 ;
The best way to give height and width property to a view is to use Layout Parameters. most of times (almost all the times for me) setting width and height directly wont work.
TableRow.LayoutParams mParam=new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// set layout parameter to view
textView.setLayoutParams(mParam);
I am trying to align to the right some ListView items, but they won't budge from the left hand side. The gravity and layout_gravity attributes don't appear to be working.
This code also aligns the layout to the right in the XML code, but doesn't at run time when populating the ListView.
Here is the XML I am using, which also uses a 9 patch image as the background of the LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:background="#drawable/my_9Patch_image"
android:id="#+id/contentLayout"
android:orientation="horizontal"
android:layout_gravity="right"
android:gravity="right"
>
<TextView
android:layout_width="wrap_content"
android:maxWidth="330dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:singleLine="false"
android:textSize="16dp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:textSize="10dp"
android:layout_gravity="left|bottom"
android:gravity="left|bottom"
/>
</LinearLayout>
I also tried to set the params in my adapter's getView method using:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
mLinearLayout.setLayoutParams(params);
Thanks in advance for any help.
I would look at rowbuttonlayout.xml from http://www.vogella.com/tutorials/AndroidListView/article.html
Basically, you are trying to change the gravity of the items, but you are trying form the outside of the list view. Likely it is trying to adhere by moving the whole list view to the right, but the list view takes up all of the space so you don't even notice it
If you want individual elements to be left justified, you will want to make custom cells that are left justified that are made when you want left justified, or make your own cell, and left justify the stuff in that cell when necessary
Preliminary solution (I'll be back). This seems to be inefficient but you can add another LinearLayout to wrap the other LinearLayout. Take the containter LinearLayout and set both the height and width to match_parent, and put the gravity attribute to right. Then add the 9 patch image to the nested LinearLayout instead of having it at the parent container LinearLayout view level, and change the its height and width to wrap_content.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contentLayout"
android:orientation="horizontal"
android:gravity="right"
>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/purple_right_2"
>
<TextView
android:id="#+id/message_outgoing"
android:layout_width="wrap_content"
android:maxWidth="330dp"
android:layout_height="wrap_content"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:singleLine="false"
android:textSize="16dp">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:id="#+id/clockView"
android:textColor="#e5e5e5"
android:minHeight="0dp"
android:textSize="10dp"
android:layout_gravity="left|bottom"
/>
</LinearLayout>
</LinearLayout>
I have two RadioButtons within a View that is set within an AlertDialog. Although the text for the first one appears horizontal, the text for the second button comes out vertical. Initially, I thought this was due to the View not filling the width of the dialog itself, but that doesn't appear to be the problem (I've tried making the text smaller and the I get the same result). I'm sure I've come across this in the past, but I can't remember how I resolved it. I'm using RadioButtons elsewhere in my app within AlertDialog's with no issue, so I'm pretty stumped. This is what it looks like. At no point do I hard-code the width of any layout or item in dp; I only ever use match_parent or wrap_content:
So, any help in trying to resolve this would be greatly appreciated!
My layout (LayoutInflator is called on it and then values/listeners are applied):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:orientation="horizontal">
<CheckBox
android:id="#+id/check_box"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.45"
android:textSize="16sp"
android:textColor="#color/Black" />
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.55"
android:orientation="horizontal"
android:enabled="False">
<RadioButton
android:id="#+id/radio_button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textDirection="ltr"
android:enabled="False"
android:textColor="#color/Black"/>
<RadioButton
android:id="#+id/radio_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:enabled="False"
android:textColor="#color/Black"
android:checked="true"/>
</RadioGroup>
</LinearLayout>
Inflating the layout like this works fine and gives the desired output:
LinearLayout inflatedLayout = (LinearLayout) this.getLayoutInflater().inflate(R.layout.template, null);
currDialog = CreateDialog.getDialog(this, "Title", inflatedLayout );
currDialog.show();
However, when I add this to a TableRow object, which is then added to a TableLayout object, it gives the unexpected result. I'm going to try with a RelativeLayout first.
You're using layout weights, and you are forcing your RadioGroup take up only 55% of the space. Since the space is not enough, the text goes to vertical. To be 100% sure that your radiogroup will fit, you will need to set it's width to wrap_content.
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:enabled="False">
You might also want to consider changing the orientation to vertical, in which case it will probably fit with only 55% of the space, but I still highly recommend wrap_content
android:id="#+id/radio_button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
There isn't enough available space for the second RadioButton to fit it's text, so it stretches the height to be able to fit the text.
I am trying to put the zoom controls of the map on the bottom right corner of screen. I could do it with RelativeLayout using both alignParentBottom="true" and alignParentRight="true", but with Framelayout I did not find any such attributes. How do I align it to the bottom-right of screen?
Actually it's possible, despite what's being said in other answers. If you have a FrameLayout, and want to position a child item to the bottom, you can use android:layout_gravity="bottom" and that is going to align that child to the bottom of the FrameLayout.
I know it works because I'm using it. I know is late, but it might come handy to others since this ranks in the top positions on google
I also ran into this situation and figured out how to do it using FrameLayout.
The following output is produced by the code given below.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/contactbook_icon"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="140"
android:textSize="12dp"
android:textColor="#FFFFFF"
android:layout_gravity="bottom|right"
android:layout_margin="15dp" />
</FrameLayout>
Change the margin value to adjust the text position over the image. Removing margin might make the text to go out of the view sometimes.
It can be achieved using RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="#drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Setting android:layout_gravity="bottom|right" worked for me
Two ways to do this:
1) Using a Frame Layout
android:layout_gravity="bottom|right"
2) Using a Relative Layout
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
If you want to try with java code. Here you go -
final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
yourView.setLayoutParams(params);
params.gravity = Gravity.BOTTOM; // set gravity
Use bottom|right as the layout_gravity value of the element which you wish to align in the bottom right position of the parent layout.
android:layout_gravity="bottom|right"
you can add an invisible TextView to the FrameLayout.
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="#+id/daily_delete_btn"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="DELETE"
android:layout_gravity="center"/>
<Button
android:id="#+id/daily_save_btn"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="SAVE"
android:layout_gravity="center"/>
</LinearLayout>
There are many methods to do this using constraint widget of the activity.xml page of android studio.
Two most common methods are:
Select your button view and adjust it's margins.
Go to "All attributes",then to "layout_constraints",then select
layout_constraintBottom_toBottomOf_parent
layout_constraintRight_toRightOf_parent
You can't do it with FrameLayout.
From spec:
http://developer.android.com/reference/android/widget/FrameLayout.html
"FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen."
Why not to use RelativeLayout?