Background: I'm making an application that can creates timed based alarms, location based or both and in the activity that creates the alarm I have checkbox for each- timepicker and map and you uncheck it if you don't want one of these features in your alarm. Here's a picture of this activity-http://prntscr.com/dr1s74 .
If you uncheck the map checkbox it removes the fragment and sets layout params of the radioGroup to below the title (because the map is gone now), center it and to add margin.
While addRule(RelativeLayout.BELOW, id) works, both setMargins(left, top, right, bottom) and addRule(RelativeLayout.CENTER_HORIZONTAL) doesn't work and because of that the radioGroup is off center and without the correct space below the title.
XML Activity:
Description of the possible problem: The only reason I can think it happens is that I have CoordinatorLayout as the parent layout (because its interaction between FAB and SnackBar) and ScrollView inside it containing RelativeLayout (Open to suggestions if you have a better way). And that for some reason makes problems when trying to add rules to layout params. XML Code (Removed the radioButtons and CheckBox for easier viewing):
<android.support.design.widget.FloatingActionButton
android:id="@+id/saveAlarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:onClick="fabClicked"
app:srcCompat="@android:drawable/ic_menu_save" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout_alarm_creator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rome.locationalarm.AlarmCreator">
<EditText
android:id="@+id/alarmTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="Alarm title" />
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@+id/alarmTitle"
android:layout_marginTop="10dp"
tools:context="com.example.rome.locationalarm.MapsFragment" />
<com.example.rome.locationalarm.CustomTimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/days"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/chooseLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/map"
android:layout_centerHorizontal="true"
android:text="Choose Location" />
<RadioGroup
android:id="@+id/days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/map"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</RadioGroup>
</RelativeLayout>
</ScrollView>
JAVA Code (Only the important part):
if(locationCheckBox.isChecked()){
//If user wants location
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW, mapFragment.getId());
//All of these don't work
layoutParams.setMargins(0, 10, 0, 0); // (left, top, right, bottom)
//All of these are tries to center the radioGroup
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.removeView(radioGroup);
layout.addView(radioGroup, layoutParams);
//layout.updateViewLayout(radioGroup, layoutParams); doesn't work as well.
Thank you for taking your time trying to help, have a nice year!
you need to do this to get the LayoutParams