I'm using Android Studio to create an app. The user will select a button from a list of three buttons which will take them to a new Activity named SurveyActivity. In SurveyActivity, there are a few relative layouts with content. But depending on which button to press, not all of the relative layouts will appear. The relative layouts are linked to each other because they're one under another.
The problem I have encountered is that when certain views are View.GONE, then you can't access the ID for the android:layout_below
I have already tried this post but I don't seem to understand why you use layout params.
I know that if you use a LinearLayout you can do it, but I wanted to know if there was a way to do it this way with Relative Layouts.
Here is the SurveyActivity:
public class SurveyActivity extends AppCompatActivity {
String timeOfDay;
private RelativeLayout relativeLayoutMentally, relativeLayoutPhysically, relativeLayoutEnergised,
relativeLayoutMotivated, relativeLayoutAlert, relativeLayoutSatisfied, relativeLayoutCalm;
@Override
@TargetApi(17)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
// Initalisation
relativeLayoutMentally = (RelativeLayout)findViewById(R.id.mentally);
relativeLayoutPhysically = (RelativeLayout)findViewById(R.id.physically);
relativeLayoutEnergised = (RelativeLayout)findViewById(R.id.energised);
relativeLayoutMotivated = (RelativeLayout)findViewById(R.id.motivated);
relativeLayoutAlert = (RelativeLayout)findViewById(R.id.alert);
relativeLayoutSatisfied = (RelativeLayout)findViewById(R.id.satisfied);
relativeLayoutCalm = (RelativeLayout)findViewById(R.id.calm);
relativeLayoutMentally.setVisibility(View.VISIBLE);
relativeLayoutPhysically.setVisibility(View.VISIBLE);
relativeLayoutSatisfied.setVisibility(View.VISIBLE);
// Intent
Intent intent = getIntent();
timeOfDay = intent.getStringExtra("Time of Day");
switch(timeOfDay){
case "Morning":
relativeLayoutEnergised.setVisibility(View.VISIBLE);
relativeLayoutMotivated.setVisibility(View.VISIBLE);
relativeLayoutAlert.setVisibility(View.VISIBLE);
relativeLayoutCalm.setVisibility(View.GONE);
break;
case "Afternoon":
relativeLayoutEnergised.setVisibility(View.VISIBLE);
relativeLayoutMotivated.setVisibility(View.VISIBLE);
relativeLayoutAlert.setVisibility(View.VISIBLE);
relativeLayoutCalm.setVisibility(View.GONE);
break;
case "Evening":
relativeLayoutEnergised.setVisibility(View.GONE);
relativeLayoutMotivated.setVisibility(View.GONE);
relativeLayoutAlert.setVisibility(View.GONE);
relativeLayoutCalm.setVisibility(View.VISIBLE);
break;
}
}
}
Here is the xml file of the survey:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.happinesssurvey.SurveyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/mentally"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/seekBarMentally"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionMentally"
android:layout_marginTop="23dp"
android:max="10"
android:padding="10dp"
android:progress="5" />
<TextView
android:id="@+id/questionMentally"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_mentally" />
<TextView
android:id="@+id/label_mentally"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/physically"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--android:layout_below="@+id/mentally"-->
<SeekBar
android:id="@+id/seekBarPhysically"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionPhysically"
android:layout_marginTop="23dp"
android:max="10"
android:padding="10dp"
android:progress="5" />
<TextView
android:id="@+id/questionPhysically"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_physically" />
<TextView
android:id="@+id/label_physically"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/energised"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--android:layout_below="@id/physically">-->
<SeekBar
android:id="@+id/seekBarEnergised"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionEnergised"
android:layout_marginTop="23dp"
android:max="10"
android:padding="20dp"
android:progress="5" />
<TextView
android:id="@+id/questionEnergised"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_energised" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/questionEnergised"
android:gravity="fill_horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="220dp"
android:layout_marginRight="220dp"
android:text="@string/label_unenergised" />
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/label_energised" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/motivated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--android:layout_below="@id/energised">-->
<SeekBar
android:id="@+id/seekBarMotivated"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionMotivated"
android:layout_marginTop="23dp"
android:max="10"
android:padding="20dp"
android:progress="5" />
<TextView
android:id="@+id/questionMotivated"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_motivated" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/questionMotivated"
android:gravity="fill_horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="220dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="220dp"
android:text="@string/label_unmotivated" />
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/label_motivated" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/satisfied"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--android:layout_below="@id/motivated">-->
<SeekBar
android:id="@+id/seekBarSatisfied"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionSatisifed"
android:layout_marginTop="23dp"
android:max="10"
android:padding="20dp"
android:progress="5" />
<TextView
android:id="@+id/questionSatisifed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_satisifed" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/questionSatisifed"
android:gravity="fill_horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="220dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="220dp"
android:text="@string/label_unsatisfied" />
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/label_satisfied" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--android:layout_below="@id/satisfied">-->
<SeekBar
android:id="@+id/seekBarAlert"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionAlert"
android:layout_marginTop="23dp"
android:max="10"
android:padding="20dp"
android:progress="5" />
<TextView
android:id="@+id/questionAlert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_alert" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/questionAlert"
android:gravity="fill_horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="220dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="220dp"
android:text="@string/label_not_alert" />
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/label_alert" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/calm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--android:layout_below="@id/alert">-->
<SeekBar
android:id="@+id/seekBarCalm"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/questionCalm"
android:layout_marginTop="23dp"
android:max="10"
android:padding="20dp"
android:progress="5" />
<TextView
android:id="@+id/questionCalm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/question_calm" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/questionCalm"
android:gravity="fill_horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="220dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="220dp"
android:text="@string/label_not_calm" />
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="@string/label_calm" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
I expect that after I do it, some of the relative layouts will show up
Thank you