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);
Related
I have a small button and when clicked it will expand to width:match_parent. When clicked again I want the button parameters from the xml to be applied. Its a custom button so its not as easy as just resizing the width.
My current code looks like:
btnBottomSheetTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
mSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
btnBottomSheetTitle.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 150));
btnBottomSheetTitle.setCornerRadius(0);
} else {
mSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});
If You have style for button You can set it programmatically. Before setting setOnClickListener save current button's LayoutParams
final android.view.ViewGroup.LayoutParams savedParams = btnBottomSheetTitle.getLayoutParams();
In Your else add:
btnBottomSheetTitle.setBackgroundResource(R.drawable.your_style);
btnBottomSheetTitle.setLayoutParams(savedParams);
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.
I need to use text view as CheckBox background and set different color for each state. Some how I managed to get it with Java. But I don't think it is the
proper way. Is there any other method to achieve this?
Implement onClickListener to your textview, something like:
int ispressed = 0;
TextView txtview = (TextView) findViewbyId(R.id.textview1);
txtview.setOnClickListener(new new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ispressed=0){
//CHANGE BACKGROUND COLOR
ispressed=1;
}else{
//RESTORE BACKGROUND COLOR
}
}
});
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 am trying to put validations in my ImageButton's onClick(). It is like the tile in Boggle game. If I clicked image 1 then the nearest ImageButtons must be the only ImageButton that is clickable and the remaining buttons will be set as unclickable. How can I achieve it? Here's my code declared at onCreate().
public void tileClick() {
if (image1.isPressed()) {
image1.setClickable(false);
image1.setImageResource(R.drawable.changes);
//clickable when image1 is pressed/clicked
image2.setClickable(true);
image5.setClickable(true);
image6.setClickable(true);
//unclickable
image3.setClickable(false);
image4.setClickable(false);
image7.setClickable(false);
image8.setClickable(false);
image9.setClickable(false);
image10.setClickable(false);
image11.setClickable(false);
image12.setClickable(false);
image13.setClickable(false);
image14.setClickable(false);
image15.setClickable(false);
image16.setClickable(false);
}
}
CustomClickListener
//get ImageButton letter
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Log.i(TAG, "arg0.getId()=" + arg0.getId());
if (arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+("a");
text.setText(generatedString);
//change ImageButton's background when clicked
((ImageButton) arg0).setImageResource(R.drawable.changea);
//Set ImageButton clickable = false when already clicked
arg0.setClickable(false);
}
}
};
//all 16 ImageButtons declared like this :
image1.setOnClickListener(myCommoClickListner);
Change your method to accept an ImageButton
public void tileClick(ImageButton clickedBtn) {
// validation logic
}
pass the clicked ImageButton to the function from onClick()
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
...
tileClick((ImageButton) arg0) // should change arg0 to something meaningful (v, view, etc...)
...
}
}
};
Then set the buttons clickable true/false according to the button passed.
You could put the ImageButtons in an Array and iterate over them and set the clickable according to which button was pressed.