• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Change something with a click on Button

B8787

Newbie
Nov 15, 2011
31
0
Hello,

I am quite new to Android coding, but I have a question. Right now I am having the code below. The thing is that right now the word changes to the left, center or right from the moment I click a RadioButton. I actually wants it to change only if I click the Generate button. Any tips how I can achieve this? Thanks!

Code:
public class tutorialOne extends Activity implements OnCheckedChangeListener{

	TextView textOut;
	EditText textIn;
	RadioGroup gravityG, styleG;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tutorial1);
		textOut = (TextView) findViewById(R.id.tvChange);
		textIn = (EditText) findViewById(R.id.editText1);
		gravityG = (RadioGroup) findViewById(R.id.rgGravity);
		gravityG.setOnCheckedChangeListener(this);
		styleG = (RadioGroup) findViewById(R.id.rgStyle);
		styleG.setOnCheckedChangeListener(this);
		Button gen = (Button) findViewById(R.id.bGenerate);
		
		gen.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			textOut.setText(textIn.getText());	
			}
		});
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch(checkedId){
		case R.id.rbLeft:
			textOut.setGravity(Gravity.LEFT);
		break;
		case R.id.rbRight:
			textOut.setGravity(Gravity.RIGHT);
		break;
		case R.id.rbCenter:
			textOut.setGravity(Gravity.CENTER);
		break;
		
		}
		
		
	}

	

}
 
Please explain what, exactly, should happen when the user presses the generate button. which gravity should the view be set to when the generate button is pressed?

Well let's say we have a Left and a Right RadioButton.

I think I can use textOut.setGravity(Gravity.RIGHT); and textOut.setGravity(Gravity.LEFT); , but the problem is I don't really know how to retrieve the information whether the left or right radiobutton is checked... :)
 
Upvote 0
like this?

Code:
public class tutorialOne extends Activity implements OnCheckedChangeListener{

	TextView textOut;
	EditText textIn;
	RadioGroup gravityG, styleG;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tutorial1);
		textOut = (TextView) findViewById(R.id.tvChange);
		textIn = (EditText) findViewById(R.id.editText1);
		gravityG = (RadioGroup) findViewById(R.id.rgGravity);
		gravityG.setOnCheckedChangeListener(this);
		styleG = (RadioGroup) findViewById(R.id.rgStyle);
		styleG.setOnCheckedChangeListener(this);
		Button gen = (Button) findViewById(R.id.bGenerate);
		
		gen.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
               			textOut.setText(textIn.getText());	
                                int checkedId = gravityG.getCheckedRadioButtonId();

                                switch(checkedId)
                                {
                                case R.id.rbLeft:
			               textOut.setGravity(Gravity.LEFT);
		                       break;
                		case R.id.rbRight:
		                	textOut.setGravity(Gravity.RIGHT);
                           		break;
		                case R.id.rbCenter:
                      			textOut.setGravity(Gravity.CENTER);
                               		break;
                                }
			}
		});
	}
}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones