1

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);
    }
}
Flame of udun
  • 2,136
  • 7
  • 35
  • 79

2 Answers2

1

You are trying to get a view inside a fragment from the Activity. You can't use findViewById() for such cases. Instead, you should expose a public method from your fragment to use get that specific view. But make sure that your fragment is inflated and added to the activity before calling it.

Here's an example: https://stackoverflow.com/a/17023533/1395437

Community
  • 1
  • 1
Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
1

You forgot to inflate the XML layout on onCreateView() like this

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.your_layout, container, false);

            RelativeLayout alarmSetBlock = (RelativeLayout) view.findViewById(R.id.sleep_text_block);
alarmSetBlock.setVisibility(Visibility.VISIBLE);

            return view;
        }
Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52