package com.example.radiobuttongroup;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.radiobuttongroup.R.id;
public class MainActivity extends Activity {
RadioButton rd1, rd2, rd3;
Button btn1;
int type;
String title;
EditText input;
AlertDialog.Builder editalert;
AlertDialog alertd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rd1 = (RadioButton) findViewById(id.radioButton1);
rd2 = (RadioButton) findViewById(id.radioButton2);
rd3 = (RadioButton) findViewById(id.radioButton3);
btn1 = (Button) findViewById(id.button1);
rd1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rd2.setChecked(false);
rd3.setChecked(false);
type = 1;
title = "cm to m";
}
});
rd2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rd1.setChecked(false);
rd3.setChecked(false);
type = 2;
title = "m to cm";
}
});
rd3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rd2.setChecked(false);
rd1.setChecked(false);
type = 3;
title = "mm to cm";
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
calculate(title, type);
}
});
}
void calculate(String title, final int type) {
try {
editalert = new AlertDialog.Builder(MainActivity.this);
alertd = editalert.create();
editalert.setTitle("Calculator");
editalert.setMessage(title);
input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
input.setLayoutParams(lp);
input.setText("1000");
editalert.setView(input);
editalert.setPositiveButton("Convert",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
int value = Integer.valueOf(input.getText().toString());
switch (type) {
case 1:
Toast.makeText(
MainActivity.this,
String.valueOf(value / 100),
Toast.LENGTH_LONG).show();
dialog.cancel();
alertd.dismiss();
break;
case 2:
Toast.makeText(MainActivity.this,
String.valueOf(value * 100),
Toast.LENGTH_LONG).show();
dialog.cancel();
alertd.dismiss();
break;
case 3:
Toast.makeText(MainActivity.this,
String.valueOf(value / 100),
Toast.LENGTH_LONG).show();
dialog.cancel();
alertd.dismiss();
break;
default:
break;
}
}
});
editalert.show();
} catch (Exception e) {
Log.v("hata", e.toString());
}
}
}