Make a Layout inactive in android - android

I have a RelativeLayout and in this RelativeLayout there are 4 Buttons. Outside this RelativeLayout there is CheckBox.The whole View is in a RelativeLayout.
I want to make the 4 Buttons to inactive (which are present in the RelativeLayout) by selecting on CheckBox and I want to make all the buttons active when I again select the CheckBox. so what to do ?
I have also tried relativeLayout.setClickable(false); but it is not working.

final int[] BUTTON_IDS = { R.id.button1, R.id.button2, };
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
for (int btnId = 0; btnId < BUTTON_IDS.length; btnId++) {
Button btn = (Button) findViewById(btnId);
btn.setEnabled(isChecked);
}
}
});

UTDATE
Try this:
<yourRelativeLayout>.setEnabled(false);
or you can change state of all your buttons to disabled.

Related

How to group radio buttons programmatically in Java/Kotlin?

I have a custom view for radio buttons in the XML layout file with ConstraintLayout. I need to group those radio buttons. But when I try to envelop that ConstraintLayout with the Radio group, it does not group the buttons and all options are getting selected.
RadioButtons need to be the direct children of a RadioGroup in order for the grouping functionality to work. A RadioGroup is just a special LinearLayout that implements the grouping functionality for all the children added that are RadioButtons. You can simply implement your own grouping functionality by adding the RadioButtons to a List and then implementing OnCheckedChangeListener and setting it on each of the RadioButtons as follows:
private final List<RadioButton> radioButtons = new ArrayList<>();
private int checkedId;
private void setRadioButton(RadioButton radioButton) {
radioButtons.add(radioButton);
radioButton.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
private final CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener =
new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkedId = buttonView.getId();
}
for (RadioButton radioButton : radioButtons) {
if (radioButton.getId() != checkedId) {
radioButton.setChecked(false);
}
}
}
};
you can set false the checked of icon in the defult running like this code in layout
xml
android:checked="false"
and in coding you can call binding.rdiobutton.setonclicklistenert{
binding.btnclick.setonclicklistener)
Use the following code in kotlin
radioGroup.check(id)
First, you write the name of the radio group, then instead of the ID, put the ID of the radio button,
For example, it is like this in my project
rg_licenseType.check(R.id.rb_three)

How to show an EditText on checking Check Box in Android Activity?

I want to add check box in my android activity such that when the check box is checked one more editText should appear in the activity..
Please tell me how to do that.
Take 2 EditText and make one's visibility Gone by default. Then check if the checkbox is checked or unchecked! If checked make 2nd EditText's visibility Visible.
Setup a listener which will perform this function..
public void addView(LinearLayout lay, EditText etxt){
lay.addView(etxt);
}
public EditText editText(Context context, int id, LinearLayout.LayoutParams params,int paddings){
EditText ib = new EditText(context);
ib.setId(id);
//ib.setBackgroundResource(Background);
// you can use this for a layout //
ib.setLayoutParams(params);
// you can use padding option by getting an array as argument
ib.setPadding(pad,pad,pad,pad);
return ib;
}
You can use this like:
LinearLayout main = (LinearLayout)findViewById(R.id.layout_main);
// Now Show the EditText inside the View
chkBox= (CheckBox)findViewById(R.id.Check_box);
satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
EditText eTxt = editText(context,502/*id here*/,lp,0);
addView(main,eTxt);
}
}
});

How can I get the selected value of the checked checkboxes written programmatically in android?

I have a CheckBox written programmatically in android, it is programmatically written because the value of the CheckBox will be based on online content, so basically, the program has no idea on how many CheckBox will going to print. I use for loops to print checkboxes, and it was successful, but among those checkboxes, I want to get the value of the CheckBox that will going to be selected.
How can I do that?
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
CheckBox cb = new CheckBox(survey);
cb.setText(ansar[z]);
choiceslayouts.addView(cb);
}
You need to create a boolean array for saving the state of all checkbox
Then add setOnCheckedChangeListener for each CheckBox
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
boolean[] array = new boolean[ansar.length];
for (int z = 0; z<ansar.length; z++){
...
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
array[z] = isCheck;
}
});
}
OR
You can set the tag for each CheckBox to check the state of CheckBox
1.Implement onClickListener for your Activity
public YourActivity extends Activity implements View.OnClickListener
2.Then set the tag for each CheckBox
for (int z = 0; z<ansar.length; z++){
...
cb.setTag("tag"+z);
cb.setOnClickListener(this);
}
3.Finnaly, implement onClick
public void onClick(View v) {
switch (v.getTag()) {
case "tag0":
if(((CheckBox) v).isChecked()) {
// your checkbox 0 is checked
}else{
// your checkbox 0 is not checked
}
break;
case "tag1":
// do the same
break;
...
}
}
Hope this help
I think you should use a ListView or a RecyclerView with custom layout and custom adapter, in order to optimize performance. Anyway it should be possible to achieve what you're look for without a major change to your code.
Your problem is that in the for loop you recreate a new checkbox with the exact same properties as the previous one, so even if you add it to your parent view, you are basically adding the same checkbox over and over again, and you can't listen to the event in the "single" checkboxes.
You should try something like this:
String ans = _jsonObject.optString("choices").toString();
String[] ansar = ans.split("`");
final boolean[] cbValue = new boolean[ansar.length];
cb = new CheckBox[ansar.length];
for (int z = 0; z<ansar.length; z++){
cb[z] = new CheckBox(survey);
cb[z].setText(ansar[z]);
cb[z].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cbValue[z] = !cbValue[z];
}
});
choiceslayouts.addView(cb[z]);
}
cbValue[] should hold the values of your checkboxes.
This is without error-checking, but it should give you the idea.

How to force RadioGroup to select only one RadioButton at a time programatically?

I am designing customize form programmatically where user can put a question and can add multiple options using radio buttons. I have taken RadioGroup and i am adding radio buttons in it. But while selecting i want only one radio button get selected at a time. How to implement it programmatically. Please help me..
Here is my code
final RadioGroup radioGroup = new RadioGroup(getApplicationContext());
radioGroup.setId(1);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
RadioButton radioButtonView = new RadioButton(getApplicationContext());
radioButtonView.setId(i++);
radioButtonView.setText(addnew);
radioGroup.addView(radioButtonView, p);
loption.addView(radioGroup, p);
Thanks in advance,
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged " + buttonView.getId());
mRadioGroup.clearCheck();
if (isChecked) {
mRadioGroup.check(buttonView.getId());
}
}
It seems that you are making a new RadioGroup for every RadioButton.
You should add every new RadioButton to the same RadioGroup. The RadioGroup will then make sure only one RadioButton can be selected at the time.
I have tried the same problem when I insert LinearLayout to put the radiobuttons in a grid.
I solved this way.
Considering that we have more RadioGroups, we know the RadioButton that was pressed. For this reason, just remove the listener from all of your RadioGroups and then deselect all the RadioButtons, resetting onCheckedChangeListener to your RadioGroup.
This is my code:
onCheckedChangeListener= new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, #IdRes int i) {
clearAllRadioButton();
((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
resetListenerRadioGroup();
}
};
onCheckedChangeListener2 = new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, #IdRes int i) {
clearAllRadioButton();
((RadioButton) binding.getRoot().findViewById(i)).setChecked(true);
resetListenerRadioGroup();
}
};
binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
private void clearAllRadioButton() {
binding.rg2.setOnCheckedChangeListener(null);
binding.rg.setOnCheckedChangeListener(null);
binding.failed.setChecked(false);
binding.draft.setChecked(false);
binding.sent.setChecked(false);
binding.inbox.setChecked(false);
}
private void resetListenerRadioGroup(){
binding.rg2.setOnCheckedChangeListener(onCheckedChangeListener2);
binding.rg.setOnCheckedChangeListener(onCheckedChangeListener);
}
*do not use the radioButton's clearCheck () because it does not respond well when resetting listeners.
Check this out it will work for you:
First Get RadioButton:
RadioButton radioBalls = (RadioButton) findViewById(R.id.radioGameInPlayBalls);
RadioButton radioTime = (RadioButton) findViewById(R.id.radioGameInPlayTime);
radioBalls.setOnCheckedChangeListener(this);
radioTime.setOnCheckedChangeListener(this);
Now in #overide method:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean c) {
switch (buttonView.getId()) {
case R.id.radioGameInPlayBalls:
if (radioTime.isChecked() && c) {
radioBalls.setChecked(false);
return;
}
radioBalls.setEnabled(c);
break;
case R.id.radioGameInPlayTime:
if (radioBalls.isChecked() && c) {
radioTime.setChecked(false);
return;
}
radioTime.setEnabled(c);
break;
default:
break;
}
}
You should check a radio button using RadioGroup, not radio button directly. Below, I populate radio group from list of options and get the radio button id of option to be checked and check the corresponding button after populating radio group. Same goes if you determine the checked option by index.
int checkedId = -1;
for(JsonElement option : options){
RadioButton radioButton = new RadioButton(getContext());
radioGroup.addView(radioButton);
if(checkedOptionId.equals(id)){
checkedId = radioButton.getId();
}
}
radioGroup.check(checkedId);

Maintaining the CheckBox Behaviour

I am having a select all checkbox button in my layout and a list view each with a check box in the right. I have added following code for the select all checkbox as :
selectall.setOnCheckChangedListener code :-
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(true);
}
Here I am maintaining each list view item
holder.mcbGoupMember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
commonFriends.get(position).setChecked(isChecked);
}
});
Now here I am stuck in a situation, I want that when I manually select all the checkbox items, then at that instant the select all button/checkbox should automatically be checked on and vice versa.
How can I achieve this??
Refer this sample program for listview checkbox scroll issue
You could just keep a simple count of the number of items you have checked at a time. If you check an item increase it by one, if you uncheck an item decrease it by one. After each increase/decrease check if this checked items number is equal to number of items on your list and check the select all if it is. Just make sure that your selectall.setOnCheckChangedListener sets the list checkbox values to whatever value it receives in isChecked. For example:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(isChecked);
}
}

Categories

Resources