Android: Set Button Height Same As Width
Solution 1:
You could extend Button and force it to be square, like this:
publicclassSquareButtonextendsButton {
publicSquareButton(Context context) {
super(context);
}
publicSquareButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicSquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)publicSquareButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
intwidth= MeasureSpec.getSize(widthMeasureSpec);
intheight= MeasureSpec.getSize(heightMeasureSpec);
intsize= width > height ? height : width;
setMeasuredDimension(size, size);
}
}
Solution 2:
I had a very similar problem where I needed to add buttons dynamically on the screen dependent on the size of an arrayList. As far as I gathered you cannot do this using xml and would have to program the View in the activity (or fragment)
Button Strings example:
ArrayList<String> trending = new ArrayList<String>() {
{
add("one"); add("two"); add("three"); add("four"); add("five"); add("six"); add("eleven");
add("seven"); add("eight"); add("nine");add("ten");
}
};
Here are the buttons within a programmed Layout (mine were toggle buttons)
//LAYOUT SETTINGS 5TableLayoutrLayout5=newTableLayout(getActivity());
rLayout5.setOrientation(TableLayout.VERTICAL);
LayoutParamsparam7=newLayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
param7.addRule(RelativeLayout.BELOW, rLayout4.getId());
rLayout5.setBackgroundColor(Color.parseColor("#EEEBAA"));
rLayout5.setLayoutParams(param7);
// List<ToggleButton> togButtStore = new ArrayList<ToggleButton>();inti=0 ;
while (i < trending.size()){
if (i % 3 == 0){
tr = newTableRow(getActivity());
rLayout5.addView(tr);
}
ToggleButtontoggleBtn=newToggleButton(getActivity());
toggleBtn.setText(trending.get(i));
toggleBtn.setId(i);
toggleBtn.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
@OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Contextcontext= getActivity().getApplicationContext();
CharSequencetext="Hello toast!";
intduration= Toast.LENGTH_SHORT;
Toasttoast= Toast.makeText(context, text, duration);
toast.show();
} else {
// The toggle is disabled
}
}
});
tr.addView(toggleBtn);
i++;
}
You need to add each of the layouts you programmed to the view
root.addView(rLayout5);
getActivity().setContentView(root);
and then of course do not forget to return the view (as you would normally).
I thought to give you one lay out setting with the Button settings, this then might be helpful to start programming the Layout using Java rather then the xml. And just to add, any layout and text setting you do in xml can also be set in Java, and its more flexible.
Solution 3:
Set your Button height and width programatically works awesome,Hope it helps ..
Enter below code to your class file .
DisplayMetricsdisplaymetrics=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
intscreenHeight= displaymetrics.heightPixels;
intbtnHeight= (int) (screenHeight * 0.25);
intbtnWidth= (int) (screenHeight * 0.25);
button1.getLayoutParams().height = btnHeight;
button1.getLayoutParams().width = btnWidth;
button2.getLayoutParams().height = btnHeight;
button2.getLayoutParams().width = btnWidth;
button3.getLayoutParams().height = btnHeight;
button3.getLayoutParams().width = btnWidth;
button4.getLayoutParams().height = btnHeight;
button4.getLayoutParams().width = btnWidth;
We used Displaymetrics here which will change your button size according to device's screen size here button width and height is 25% of screen height which you can change according to your convenience .
Solution 4:
You should do this way:
Buttonbutton= (Button) findViewById.(R.id.button);
intbuttonWidth= button.getLayoutParams().width;
button.setLayoutParams(newLayoutParams(buttonWidth, buttonWidth));
Hope this will help you.
Post a Comment for "Android: Set Button Height Same As Width"