I want to change the constraints of the togglebutton programmatically, but I am already set constraints to the togglebutton in xml file .What I need is when I click a togglebutton i want to override the constraints of the togglebutton i.e., when i click on togglebutton1 then add constraints from left side of togglebutton2 with the left side of togglebutton3 with marging 0.
private ToggleButton toggleButton1;
private ToggleButton toggleButton2;
private ToggleButton toggleButton3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
toggleButton3 = (ToggleButton) findViewById(R.id.toggleButton3);
CompoundButton.OnCheckedChangeListener compoundButton = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
set.connect(toggleButton3.getId(),ConstraintSet.LEFT,toggleButton2.getId(),ConstraintSet.LEFT,0);
set.applyTo(constraintLayout);
} else
// somecode
}
};
toggleButton1.setOnCheckedChangeListener(compoundButton);
}
hope someone help me
I don't have a solution for you, but I can show you something that convinces me that connect is not working right/left as it does top/bottom. Change your OnCheckedChangeListener code to the following and run it.
CompoundButton.OnCheckedChangeListener compoundButton = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
if (isChecked) {
set.connect(toggleButton3.getId(), ConstraintSet.LEFT, toggleButton2.getId(), ConstraintSet.LEFT, 0);
} else {
set.connect(toggleButton3.getId(), ConstraintSet.BOTTOM, toggleButton2.getId(), ConstraintSet.BOTTOM, 0);
}
set.applyTo(constraintLayout);
}
};
When you first click the toggleButton1, you will not see a change. When you click it a second time, you should see toggleButton3 jump so its bottom aligns with the bottom of toggleButton2. (Make sure it doesn't line up to begin with.)
Your issue may be related to this bug report.
If you do find a solution, it would be great for you to post it as an answer here since I have run across this issue a number of times.
I hope that helps.
EDIT
Here is what you can do to fix this. Use ConstraintSet.START and ConstraintSet.END instead of ConstraintSet.LEFT and ConstraintSet.RIGHT. I just tried it and it works OK. I can't say why left and right don't work.
Related
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);
}
}
});
I want to show a Seekbar, when I check an Checkbox and the Seekbar shall disappear, when I uncheck the Checkbox.
But it only works for me, that the Seekbar appears by clicking the Checkbox but doesn't disappear, when I uncheck the Checkbox.
I tried it with a ClickListener:
begrenzungsCheckBox = (CheckBox) findViewById(R.id.filebegrenzungCheckBox);
begrenzungsCheckBox.setOnClickListener(filebegrenzungsListenerCheckbox);
OnClickListener filebegrenzungsListenerCheckbox = new OnClickListener() {
#Override
public void onClick(View v) {
TextView filebegrenzungAnzeige = (TextView) findViewById(R.id.filebegrenzungAnzeige);
SeekBar filebegrenzungsSeekbar = (SeekBar) findViewById(R.id.filebegrenzungSeekbar);
filebegrenzungAnzeige.setVisibility(View.VISIBLE);
filebegrenzungsSeekbar.setVisibility(View.VISIBLE);
}
};
and here is the corresponding xml layout file:
<SeekBar
android:id="#+id/filebegrenzungSeekbar"
android:layout_width="wrap_content"
android:visibility="invisible"
android:layout_height="wrap_content" />
How to use the ClickListener in order to have the possibility to check and uncheck the Checkbox or which alternatives are there to realise that?
OnClickListener fires for every click event. It doesn't care whether the checkbox gets checked or unchecked. Use an OnCheckedChangedListener instead:
*your checkbox*.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
TextView filebegrenzungAnzeige = (TextView) findViewById(R.id.filebegrenzungAnzeige);
SeekBar filebegrenzungsSeekbar = (SeekBar) findViewById(R.id.filebegrenzungSeekbar);
filebegrenzungAnzeige.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
filebegrenzungsSeekbar.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
}
});
Use this:
if (chk1.isChecked()) {
ShowSeekBar();
} else {
makeInvisibleSeekBar();
}
If I have a radiogroup with 7 radiobuttons and I want them to be aligned like that:
button2 button5
button1 button3 button6
button4 button7
Is there a way I can do the above with one RadioGroup (I am ok with even button1 being on first line but this is the general layout I am looking for)
Please help
Thanks
You could implement your own button handling logic, based on this http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html:
public class RadioButtonHandler implements RadioButton.OnCheckedChangeListener {
private CompoundButton currentlyCheckedButton = null;
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked) {
currentlyCheckedButton .setChecked(false);
currentlyCheckedButton = button;
}
}
public void addButton(RadioButton button) {
// if this is the first button, set it as the current button
if (currentlyCheckedButton == null) {
currentlyCheckedButton = button;
}
button.setOnCheckedChangeListener(this);
}
public CompoundButton getCheckedRadioButton() {
return currentlyCheckedButton ;
}
}
Instantiate this class and call addButton for each button that the class should control. Then you can layout your buttons in whatever view group you need. Add extra logic to the onCheckChanged callback or call getCheckedRadioButton which will return a reference to the currently checked button.
[EDIT] Fixed typo = getCheckedButton -> getCheckedRadioButton
Is there anyway to control the text position for a ToggleButton's on and off state? For instance, I want the text label to be left aligned when on and right aligned when off.
EDIT:
Also, I'd to include a little padding for the text on the left and right. About 5dp. and have finer control over the label placement if possible.
ANSWER:
This is what I needed!
button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);
public class StackActivity extends Activity implements OnCheckedChangeListener {
private ToggleButton tb;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tb = (ToggleButton)findViewById(R.id.toggleButton1);
tb.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
tb.setGravity(Gravity.LEFT);
tb.setPadding(5,0,0,0); // Set left padding
} else
{
tb.setGravity(Gravity.RIGHT);
tb.setPadding(0,0,5,0); // Set right padding
}
}
}
ToggleButton b1 = (ToggleButton) findViewById(R.id.button1);
if(b1.isChecked()){
b1.setGravity(Gravity.RIGHT);
}
else{
b1.setGravity(Gravity.LEFT);
}
Note, that you will not see any changes if the button is not of a minimum size (has to be bigger than the lable text).
Since ToggleButton is a subclass of TextView, try to use android:gravity="left".
Please prefer to http://developer.android.com/reference/android/widget/TextView.html#attr_android:gravity.
Change the alignment by changing the gravity whenever the button is clicked by adding some code in the OnClickListener like this:
toggleButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (((ToggleButton)v).isChecked())
toggleButton.setGravity(Gravity.LEFT);
else
toggleButton.setGravity(Gravity.RIGHT);
}
});
This is what I needed!
button.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
button.setPadding(0, 0, 5, 0);
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.