Reputation:
I have my android layout which has two radiobutton in one Radiogroup. I have these following conditions in my code:
If I checked first radio button then one editText should appear.
If I checked second radio button then two editText should appear.
I have pasted my code here...In my code editText is not appear when i m clicking on the either of the radio button.
package com.example.audio111;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.Toast;
@SuppressLint("NewApi")
public class Audioactivity extends Activity {
String value;
String selection;
RadioGroup rg;
int pos;
int pos1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
// do operations specific to this selection (adding your EditText)
RelativeLayout mRlayout = new RelativeLayout(Audioactivity.this);
RelativeLayout.LayoutParams mRparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
EditText myEditText = new EditText(Audioactivity.this);
mRparams.leftMargin=270;
mRparams.topMargin=150;
myEditText.setWidth(32);
myEditText.setEms(50);
myEditText.setLayoutParams(mRparams);
mRlayout.addView(myEditText);
break;
case R.id.radio1:
// do operations specific to this selection (adding two EditTexts)
break;
}
}
});
}
Upvotes: 0
Views: 871
Reputation: 1314
I think both ways for getting selected RadioButtons you are using are not correct. Why don't you try with the 'standard' way to do this?
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
// do operations specific to this selection (adding your EditText)
break;
case R.id.radio1:
// do operations specific to this selection (adding two EditTexts)
break;
}
}
});
Upvotes: 0
Reputation: 778
@Override
protected void onResume() {
super.onResume();
mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio0:
mEditText1.setVisibility(View.VISIBLE);
mEditText2.setVisibility(View.GONE);
break;
case R.id.radio1:
mEditText1.setVisibility(View.VISIBLE);
mEditText2.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
});
}
Upvotes: 0
Reputation: 28823
You added the EditTexts
to RelativeLayout
, fine.
But those RelativeLayouts
are also generated dynamically. You need to add them in parent layout, too.
Something like:
LinearLayout linLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linLayout = (LinearLayout) findViewById(R.id.container) //your xml's container layout
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.rdbutton0:
Toast.makeText(getBaseContext(), "You have Clicked moveable",
Toast.LENGTH_SHORT).show();
RelativeLayout mRlayout = new RelativeLayout(Audioactivity.this);
.
.
mRlayout.addView(myEditText);
linLayout.addView(mRlayout); //add the newly created elativeLayout to your container Layout
break;
case R.id.rdbutton1:
Toast.makeText(getBaseContext(), "You have Clicked fixed",
Toast.LENGTH_SHORT).show();
RelativeLayout mRlayout1 = new RelativeLayout(Audioactivity.this);
RelativeLayout.LayoutParams mRparams1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
.
.
mRlayout1.addView(myEditText2);
linLayout.addView(mRlayout1); //add the newly created elativeLayout to your container Layout
break;
}
}
});
}
Hope this helps.
Upvotes: 1