I need to set a RelativeLayout
block in an activity to VISIBLE
. But I can't seem to assign the RelativeLayout
to a variable like so :
Relative Layout alarmSetBlock = (RelativeLayout) findViewById(R.id.sleep_text_block);
alarmSetBlock
is assigned null
instead.
I think I am supposed to use something like inflater.inflate but don't know how to use it outside of fragments.
Please tell me the easiest/best way to resolve this.
layout xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<Button
android:id="@+id/alarm_set_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="@color/mediumGray"
android:text="SET ALARM"
android:onClick="showTimePickerDialog"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/sleep_text_block"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="gone">
<TextView
android:id="@+id/sleep_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:text="Alarm set to :"/>
<TextView
android:id="@+id/alarm_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/sleep_text"
android:textColor="@color/colorAccent"
android:textSize="36sp" />
</RelativeLayout>
</LinearLayout>
The RelativeLayout
I am trying to initialize is the second one in the layout.
Here is the fragment file for the above layout. I am trying to initialize the RelativeLayout
inside the activity java file that holds this fragment.
public class SleepSetFragment extends Fragment {
public static SleepSetFragment newInstance()
{
SleepSetFragment fragment = new SleepSetFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_sleep_set, container, false);
}
}