You are on page 1of 8

MY CHECK BOX

LAYOUT

Attribute

Component Attribute Values


LinearLayout orientation vertical
gravity center_vertical
LinearLayout orientation hozontal
layout_width 300dp
layout_height wrap_content

TextView layout_width 5dp


layout_height wrap_content
layout_marginLeft 30dp
layout_marginTop 30dp
text Sex
textSize 34sp
TextView id textViewSex
layout_width Wrap_content
layout_height Wrap_content
layout_marginTop 30dp
lext
textSize 34sp
ImageView id image
layout_width wrap_content
layout_height 200dp
layout_marginLeft 30dp
layout_marginTop 50dp
visibility invisible
CheckBox id checkBoxMale
layout_width 300dp
layout_height 100dp
layout_marginLeft 30dp
layout_marginTop 30dp
lext Male
textSize 34sp
CheckBox id checkBoxFemale
iayout_width 300dp
iayout_height 100dp
iayout_marginLeft 30dp
text Female
textSize 34sp

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:gravity="center_vertical"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_weight="1"
android:text="Sex"
android:textSize="34sp" />

<TextView
android:id="@+id/textViewSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_weight="1"
android:textSize="34sp" />

</LinearLayout>

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:scaleType="centerInside"
android:visibility="invisible"
app:srcCompat="@drawable/male"
tools:visibility="invisible" />

<CheckBox
android:id="@+id/checkBoxMale"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="Male"
android:textSize="34sp" />

<CheckBox
android:id="@+id/checkBoxFemale"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:text="Female"
android:textSize="34sp" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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:gravity="center_vertical"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="5dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_weight="1"
android:text="Sex"
android:textSize="34sp" />

<TextView
android:id="@+id/textViewSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_weight="1"
android:textSize="34sp" />

</LinearLayout>

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:scaleType="centerInside"
android:visibility="invisible"
app:srcCompat="@drawable/male"
tools:visibility="invisible" />

<CheckBox
android:id="@+id/checkBoxMale"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="Male"
android:textSize="34sp" />

<CheckBox
android:id="@+id/checkBoxFemale"
android:layout_width="300dp"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:text="Female"
android:textSize="34sp" />

</LinearLayout>

MainActivity.java

package com.example.mycheckbox;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

CheckBox checkBoxMale;
CheckBox checkBoxFemale;
TextView textTViewSex;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textTViewSex = findViewById(R.id.textViewSex);
image = findViewById(R.id.image);
checkBoxMale = findViewById(R.id.checkBoxMale);
checkBoxFemale = findViewById(R.id.checkBoxFemale);

checkBoxMale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkBoxMale.isChecked()==true && checkBoxFemale.isChecked()==false){
textTViewSex.setText("Male");
image.setVisibility(View.VISIBLE);
image.setImageResource(R.drawable.male);
}
if (checkBoxFemale.isChecked()==true && checkBoxMale.isChecked()==false){
textTViewSex.setText("Female");
image.setVisibility(View.VISIBLE);
image.setImageResource(R.drawable.female);
}
if (checkBoxFemale.isChecked()==true && checkBoxMale.isChecked()==true){
textTViewSex.setText("");
image.setVisibility(View.INVISIBLE);
}
if (checkBoxFemale.isChecked()==false && checkBoxMale.isChecked()==false){
textTViewSex.setText("");
image.setVisibility(View.INVISIBLE);
}
}
});

checkBoxFemale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkBoxMale.isChecked()==true && checkBoxFemale.isChecked()==false){
textTViewSex.setText("Male");
image.setVisibility(View.VISIBLE);
image.setImageResource(R.drawable.male);
}
if (checkBoxFemale.isChecked()==true && checkBoxMale.isChecked()==false){
textTViewSex.setText("Female");
image.setVisibility(View.VISIBLE);
image.setImageResource(R.drawable.female);
}
if (checkBoxFemale.isChecked()==true && checkBoxMale.isChecked()==true){
textTViewSex.setText("");
image.setVisibility(View.INVISIBLE);
}
if (checkBoxFemale.isChecked()==false && checkBoxMale.isChecked()==false){
textTViewSex.setText("");
image.setVisibility(View.INVISIBLE);
}
}
});
}
}

You might also like