Activity_main.java
===================
package com.example.mca.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_ok = (Button) findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "OK Clicked", Toast.LENGTH_SHORT).show();
}
});
ImageButton btn_img = (ImageButton) findViewById(R.id.btn_img);
btn_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Image Clicked", Toast.LENGTH_SHORT).show();
}
});
final CheckBox btn_check = (CheckBox) findViewById(R.id.btn_check);
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(btn_check.isChecked())
btn_check.setText("Unchecked Me");
else
btn_check.setText("Checked Me");
}
});
final TextView tv = (TextView) findViewById(R.id.textmsg);
final RadioGroup group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedid) {
RadioButton rb = (RadioButton) findViewById(checkedid);
if(rb!=null)
tv.setText(rb.getText()+" is selected");
}
});
Button btn_clear = (Button) findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
group.clearCheck();
tv.setText("Choose an option");
}
});
Button btn_click = (Button) findViewById(R.id.btn_click);
btn_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "You Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
===================
app\src\main\res\layout.xml
===================
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mca.myapplication.MainActivity">
<Button
android:text="OK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/btn_ok" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_ok"
android:layout_alignParentEnd="true"
android:id="@+id/btn_toggle" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/btn_img"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<CheckBox
android:text="Checked Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_img"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp"
android:id="@+id/btn_check" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="@+id/group">
<RadioButton
android:text="Option 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignStart="@+id/btn_check"
android:id="@+id/rdb1"
android:layout_weight="1" />
<RadioButton
android:text="Option 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignEnd="@+id/chk_btn"
android:id="@+id/rdb2"
android:layout_weight="1" />
<RadioButton
android:text="Option 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignParentStart="true"
android:id="@+id/rdb3"
android:layout_weight="1" />
<RadioButton
android:text="Option 4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chk_btn"
android:layout_alignParentStart="true"
android:id="@+id/rdb4"
android:layout_weight="1" />
</RadioGroup>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignParentEnd="true"
android:id="@+id/textmsg" />
<Button
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textmsg"
android:layout_alignParentEnd="true"
android:id="@+id/btn_clear" />
<Button
android:text="Click me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/btn_img"
android:id="@+id/btn_click" />
</RelativeLayout>
===================
package com.example.mca.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_ok = (Button) findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "OK Clicked", Toast.LENGTH_SHORT).show();
}
});
ImageButton btn_img = (ImageButton) findViewById(R.id.btn_img);
btn_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Image Clicked", Toast.LENGTH_SHORT).show();
}
});
final CheckBox btn_check = (CheckBox) findViewById(R.id.btn_check);
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(btn_check.isChecked())
btn_check.setText("Unchecked Me");
else
btn_check.setText("Checked Me");
}
});
final TextView tv = (TextView) findViewById(R.id.textmsg);
final RadioGroup group = (RadioGroup) findViewById(R.id.group);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedid) {
RadioButton rb = (RadioButton) findViewById(checkedid);
if(rb!=null)
tv.setText(rb.getText()+" is selected");
}
});
Button btn_clear = (Button) findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
group.clearCheck();
tv.setText("Choose an option");
}
});
Button btn_click = (Button) findViewById(R.id.btn_click);
btn_click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "You Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
===================
app\src\main\res\layout.xml
===================
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mca.myapplication.MainActivity">
<Button
android:text="OK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/btn_ok" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_ok"
android:layout_alignParentEnd="true"
android:id="@+id/btn_toggle" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:id="@+id/btn_img"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<CheckBox
android:text="Checked Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_img"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp"
android:id="@+id/btn_check" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="@+id/group">
<RadioButton
android:text="Option 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignStart="@+id/btn_check"
android:id="@+id/rdb1"
android:layout_weight="1" />
<RadioButton
android:text="Option 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignEnd="@+id/chk_btn"
android:id="@+id/rdb2"
android:layout_weight="1" />
<RadioButton
android:text="Option 3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignParentStart="true"
android:id="@+id/rdb3"
android:layout_weight="1" />
<RadioButton
android:text="Option 4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/chk_btn"
android:layout_alignParentStart="true"
android:id="@+id/rdb4"
android:layout_weight="1" />
</RadioGroup>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_check"
android:layout_alignParentEnd="true"
android:id="@+id/textmsg" />
<Button
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textmsg"
android:layout_alignParentEnd="true"
android:id="@+id/btn_clear" />
<Button
android:text="Click me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="@+id/btn_img"
android:id="@+id/btn_click" />
</RelativeLayout>
No comments:
Post a Comment