- Forum posts: 1
Dec 5, 2014, 7:37:03 PM via Website
Dec 5, 2014 7:37:03 PM via Website
Hi,
I am beginner in android development. To understand radioButton click event I have written below code to capture clickevent of radio button.Code is as below. Code is working only for first radio button press. Please suggest
package com.example.radiobuttonpress;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.util.Log;
public class MainActivity extends ActionBarActivity {
private static final String logTag = "MainActivity";
RadioGroup checkGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(logTag,"Calling checkButtonPressed");
checkButtonPressed();
public void checkButtonPressed(){
RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_container);
Log.i(logTag,"Calling getCheckedRadioButtonId");
final RadioButton rd = (RadioButton)findViewById(checkGroup.getCheckedRadioButtonId());
rd.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.i(logTag,"Check switch value");
switch(rd.getId()){
case R.id.radio0:
Log.i(logTag,"Button 0 Pressed");
rd.setText("First Button is Pressed");
break;
case R.id.radio1:
Log.i(logTag,"Button 1 Pressed");
rd.setText("Second Button is Pressed");
break;
case R.id.radio2:
Log.i(logTag,"Button 2 Pressed");
rd.setText("Third Button is Pressed");
break;
}
}
});
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
— modified on Dec 5, 2014, 8:11:56 PM
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.