1. Are you ready for the Galaxy S20? Here is everything we know so far!

Change something with a click on Button

Discussion in 'Android Development' started by B8787, Aug 8, 2012.

  1. B8787

    B8787 Newbie
    Thread Starter

    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 (Text):
    1.  
    2. public class tutorialOne extends Activity implements OnCheckedChangeListener{
    3.  
    4.     TextView textOut;
    5.     EditText textIn;
    6.     RadioGroup gravityG, styleG;
    7.    
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         // TODO Auto-generated method stub
    11.         super.onCreate(savedInstanceState);
    12.         setContentView(R.layout.tutorial1);
    13.         textOut = (TextView) findViewById(R.id.tvChange);
    14.         textIn = (EditText) findViewById(R.id.editText1);
    15.         gravityG = (RadioGroup) findViewById(R.id.rgGravity);
    16.         gravityG.setOnCheckedChangeListener(this);
    17.         styleG = (RadioGroup) findViewById(R.id.rgStyle);
    18.         styleG.setOnCheckedChangeListener(this);
    19.         Button gen = (Button) findViewById(R.id.bGenerate);
    20.        
    21.         gen.setOnClickListener(new View.OnClickListener() {
    22.            
    23.             @Override
    24.             public void onClick(View v) {
    25.                 // TODO Auto-generated method stub
    26.             textOut.setText(textIn.getText()); 
    27.             }
    28.         });
    29.     }
    30.  
    31.     @Override
    32.     public void onCheckedChanged(RadioGroup group, int checkedId) {
    33.         // TODO Auto-generated method stub
    34.         switch(checkedId){
    35.         case R.id.rbLeft:
    36.             textOut.setGravity(Gravity.LEFT);
    37.         break;
    38.         case R.id.rbRight:
    39.             textOut.setGravity(Gravity.RIGHT);
    40.         break;
    41.         case R.id.rbCenter:
    42.             textOut.setGravity(Gravity.CENTER);
    43.         break;
    44.        
    45.         }
    46.        
    47.        
    48.     }
    49.  
    50.    
    51.  
    52. }
    53.  
     



    1. Download the Forums for Android™ app!


      Download

       
  2. jonbonazza

    jonbonazza Android Expert

    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?
     
  3. B8787

    B8787 Newbie
    Thread Starter

    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... :)
     
  4. jonbonazza

    jonbonazza Android Expert

    like this?

    Code (Text):
    1.  
    2. public class tutorialOne extends Activity implements OnCheckedChangeListener{
    3.  
    4.     TextView textOut;
    5.     EditText textIn;
    6.     RadioGroup gravityG, styleG;
    7.    
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         // TODO Auto-generated method stub
    11.         super.onCreate(savedInstanceState);
    12.         setContentView(R.layout.tutorial1);
    13.         textOut = (TextView) findViewById(R.id.tvChange);
    14.         textIn = (EditText) findViewById(R.id.editText1);
    15.         gravityG = (RadioGroup) findViewById(R.id.rgGravity);
    16.         gravityG.setOnCheckedChangeListener(this);
    17.         styleG = (RadioGroup) findViewById(R.id.rgStyle);
    18.         styleG.setOnCheckedChangeListener(this);
    19.         Button gen = (Button) findViewById(R.id.bGenerate);
    20.        
    21.         gen.setOnClickListener(new View.OnClickListener() {
    22.            
    23.             @Override
    24.             public void onClick(View v) {
    25.                 // TODO Auto-generated method stub
    26.                         textOut.setText(textIn.getText()); 
    27.                                 int checkedId = gravityG.getCheckedRadioButtonId();
    28.  
    29.                                 switch(checkedId)
    30.                                 {
    31.                                 case R.id.rbLeft:
    32.                            textOut.setGravity(Gravity.LEFT);
    33.                                break;
    34.                         case R.id.rbRight:
    35.                             textOut.setGravity(Gravity.RIGHT);
    36.                                 break;
    37.                         case R.id.rbCenter:
    38.                                 textOut.setGravity(Gravity.CENTER);
    39.                                     break;
    40.                                 }
    41.             }
    42.         });
    43.     }
    44. }
    45.  
     
  5. B8787

    B8787 Newbie
    Thread Starter

    Thanks!!!
     
  6. CptSupermrkt

    CptSupermrkt Lurker

    Haha I see you're following the same tutorials on YouTube from mybringback :)
     
  7. B8787

    B8787 Newbie
    Thread Starter

    True story ;)
     
  8. B8787

    B8787 Newbie
    Thread Starter

    ps, any other good tuts you could recommend?:)
     
Loading...

Share This Page

Loading...