I am very newbie on android studio and java. I have made a simple page which contain check boxes, and a button to print out in a toast message the checked box selected. The problem is if i clicked on submit button without selecting anything,the app stop working and crash. What i want to achieve is if i didn't select anything and i click on submit button it should send me a toast message "Please select a box" without making the app crash.
XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Favorite Courses: "
android:textSize="20sp"
android:layout_marginTop="30dp"
android:layout_below="@+id/txDate"
android:id="@+id/fvCourses"/>
<CheckBox
android:id="@+id/cbCs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/fvCourses"
android:layout_alignBottom="@+id/fvCourses"
android:text="Computer Science" />
<CheckBox
android:id="@+id/cbAcc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/fvCourses"
android:layout_below="@+id/cbCs"
android:text="Accounting" />
<CheckBox
android:id="@+id/cbGraphic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/fvCourses"
android:layout_below="@+id/cbAcc"
android:text="Graphic Designer" />
<Button
android:id="@+id/btnapply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="330dp"
android:text="Confirm" />
</RelativeLayout>
Java code:
package test.cb.app;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends AppCompatActivity {
TextView textView;
CheckBox cbCs,cbAcc,cbGraphic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbCs = findViewById(R.id.cbCs);
cbAcc = findViewById(R.id.cbAcc);
cbGraphic = findViewById(R.id.cbGraphic);
Button btnapply = findViewById(R.id.btnapply);
btnapply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StringBuffer result = new StringBuffer();
result.append("Computer Science check : ").append(cbCs.isChecked());
result.append("\nAccounting check : ").append(cbAcc.isChecked());
result.append("\nGraphic check :").append(cbGraphic.isChecked());
Toast.makeText(MainActivity.this, result.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
I expect that i have to check for null values on the submit button.
What would be the code to achieve that ?
Any help would be greatly appriciated.
Thank you!
Aucun commentaire:
Enregistrer un commentaire