package gr.mybook.mybasicviews;

import com.example.mybasicviews.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {
	Button btn;
	CheckBox box1, box2;
	RadioGroup radio;
	ToggleButton tog;
	ProgressBar mbar;
	
	Handler handler = new Handler(){
	      @Override
	      public void handleMessage(Message msg) {
	         mbar.incrementProgressBy(20);
	      }
	   };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mbar = (ProgressBar) findViewById(R.id.progressbar);
	    mbar.setProgress(0);
		btn=(Button)findViewById(R.id.btn1);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			      new Thread(new Runnable() {
			         public void run() {
			            try {
			               for (int i=0;i<10;i++) {
			                  Thread.sleep(1000);
			                  handler.sendMessage(handler.obtainMessage());
			               }
			            }
			            catch (Throwable t) {
			               // end thread execution
			            }
			          }
			       }).start();
			}
		});
		
		box1=(CheckBox)findViewById(R.id.chk1);
		box1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (box1.isChecked()) {
					Toast.makeText(MainActivity.this, "Check Box 1 is selected", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(MainActivity.this, "Check Box 1 is deselected", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		box2=(CheckBox)findViewById(R.id.chk2);
		box2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (box2.isChecked()) {
					Toast.makeText(MainActivity.this, "Check Box 2 is selected", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(MainActivity.this, "Check Box 2 is deselected", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		radio=(RadioGroup)findViewById(R.id.rdg1);
		radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
			    RadioButton rb1=(RadioButton)findViewById(R.id.rdb1);
				if (rb1.isChecked()) {
					Toast.makeText(MainActivity.this, "Radio Button 1 is selected", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(MainActivity.this, "Radio Button 2 is selected", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		tog=(ToggleButton)findViewById(R.id.togb1);
		tog.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (tog.isChecked()){
					Toast.makeText(MainActivity.this, "Toggle Button is ON", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(MainActivity.this, "Toggle Button is OFF", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
