I have a layout where i have a switch and RelativeLayout which contains 2 Image buttons. By default switch remains in the off state. What i want is when the activity loads i want to set the relative layout to b invisible and if the switch is set in the on state then the RelativeLayout should be visible.
When i try to set it invisible in my OnCreate it is giving me NullPointerException..
How can i set the layout to b invisible when the activity starts and if the switch is checked then only the layout should be visible.. ? Please Help..
Layout.xml :-
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/btnStyleBeige"
android:id="@+id/switch1"
android:layout_marginRight="10dp"
android:layout_alignTop="@+id/textView7"
android:layout_alignStart="@+id/button5" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relative2"
android:layout_marginTop="40dp"
android:layout_below="@+id/switch1"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/taskname">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pickDate"
android:src="@drawable/c2"
style="@style/btnStyleBeige"
android:layout_marginStart="65dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pickTime"
style="@style/btnStyleBeige"
android:src="@drawable/c1"
android:layout_marginEnd="65dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Activity :-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addtask);
getActionBar().setDisplayHomeAsUpEnabled(true);
relativeLayout2.setVisibility(View.INVISIBLE);
Switch aSwitch = (Switch) findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(this);
init();
/** Capture our View elements */
//pDisplayDate = (TextView) findViewById(R.id.displayDate);
pPickDate = (ImageButton) findViewById(R.id.pickDate);
/** Listener for click event of the button */
pPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
/** Capture our View elements */
//pDisplayTime = (TextView) findViewById(R.id.displayTime);
pPickTime = (ImageButton) findViewById(R.id.pickTime);
/** Listener for click event of the button */
pPickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
/** Get the current date and time */
final Calendar cal = Calendar.getInstance();
pYear = cal.get(Calendar.YEAR);
pMonth = cal.get(Calendar.MONTH);
pDay = cal.get(Calendar.DAY_OF_MONTH);
mHour = cal.get(Calendar.HOUR_OF_DAY);
mMinute = cal.get(Calendar.MINUTE);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
relativeLayout2 = (RelativeLayout)findViewById(R.id.relative2);
if(isChecked) {
relativeLayout2.setVisibility(View.GONE);
} else {
relativeLayout2.setVisibility(View.VISIBLE);
}
}
You did not created the reference of relative layout.
Before this line..
relativeLayout2.setVisibility(View.INVISIBLE);
add this one
RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.relative2);