Can't Change Android Button Padding - android

I have a table of buttons and am attempting to dynamically create and set the padding around the text of each with the following function:
public static void AddChartColBtn(TableRow row, String txt, String style) {
Button note_chart_btn;
note_chart_btn = new Button(ctx);
note_chart_btn.setText(txt);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.setMargins(0, 0, 0, 0);
note_chart_btn.setLayoutParams(params);
row.addView(note_chart_btn);
}
I've tried working with LayoutParams classes to set margins and paddings, etc...but no matter what I try the button always fills the entire space of the current table cell, ignoring any padding. The Button class doesn't appear to have layout params, padding that can be manipulated directly. How can dynamically create and declare a button with 0 padding around it's text?

set Both
params.setMargins(0, 0, 0, 0);
params.setPadding(0, 0, 0, 0);

Use like this.
Button note_chart_btn;
note_chart_btn = new Button(ctx);
note_chart_btn.setText(txt);
note_chart_btn.setGravity(Gravity.CENTER);
note_chart_btn.setPadding(left, top, right, bottom);
I hope this will help you.

Try setting something like this
android:padding="10dp" for every button you have in your layout in the .xml file of your layout

setPadding should after the setBackgroundResource
Like this:
button.setBackgroundResource(R.mipmap.btn_bg)
button.setPadding(100, 0, 100, paddingBo0tom)

Related

How to add divider between two Horizontal Scroll Bar in android by programmatically

I want to add divider between two Horizontal Scroll Bar by programmatically.
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
divider.setLayoutParams(lp);
divider.setBackgroundColor(Color.WHITE);
You can just create a simple View and set its height to 1 or 2 (or whatever your desired thickness of the line is) and the width to whatever you may want that to be (MATCH_PARENT?). You can change the color of it by simple setting this View background (setBackgroundColor). This is the easiest method.
`View divider = new View
`divider.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, toolKit.size(1), 0, toolKit.size(1), 0, 0));
divider.setBackgroundColor(toolKit.getColor(R.color.white_50_per));

How to align image in android

I am dynamically adding controls in my activity. simultaneously i'm adding a editbox and button, but facing some issue in image alignment.
Here is my code which ads the editText and Button and returns to the linear layout which is vertical in alignment.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(0);
final EditText textView = new EditText(this);
textView.setLayoutParams(lparams);
textView.setSingleLine(true);
final LayoutParams lparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final Button button = new Button(this);
textView.setLayoutParams(lparams1);
if(id == R.id.new_alternate_number_button)
{
if(contactNumber == "")
{
textView.setHint("Enter contact Number");
}
else
{
textView.setText(contactNumber);
}
textView.setInputType(InputType.TYPE_CLASS_PHONE); //to popup numpad
}
else
{
if(contactEmailID == "")
{
textView.setHint("Enter Email ID ");
}
else
{
textView.setText(contactEmailID);
}
}
button.setBackgroundResource(R.drawable.ic_delete);
button.setBackgroundResource(R.drawable.ic_delete);
button.setOnClickListener(deleteView);
layout.addView(textView);
layout.addView(button);
textView.post(new Runnable() {
public void run() {
textView.requestFocus();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
}
});
return layout;
In my XML file i have declared linear layout which is vertical in alignment i.e icon should be at the end of screen and EditText should be left aligned.. also i need a space between EditText and image..
Thanks in advance
Your LayoutParams is set to WRAP_CONTENT for Width instead of FILL_PARENT (or MATCH_PARENT).
A "pattern" that I use in similar cases is that even though I'm dynamically adding a new row/section, I still keep they layout in an xml file for that row, and then I dynamically inflate and find the elements by id, and bind to them. Sort of like how we deal with List Items except in this case, you are not using a list.
By keeping the layout in the XML file, it'll be easier to prototype what you want to see, add padding etc. And you can even use a RelativeLayout if you can't get the LinearLayout to work. Again, you can do all of this in code, but doing layout in the XML offers more flexibility (and is simpler to deal with in my mind)
This how you can go about adding space between the Button and EditText:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(50, 0, 0, 0); // Adding margin to the left of your button
Button yourButton = new Button(this);
yourButton.setText("some text");
linearLayout.addView(yourButton, layoutParams);
The sequence of parameters in setMargins method:
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
Know more about setMargins method here.
Now that I realize your requirements properly (at least I guess so) I have another answer for you. Keep me updated about how it helps.
You will have to specify weight for the elements. Let's say total weightSum of your layout is 8, then if you specify weight of the EditText as 7, it will take 7/8th of the total space. And set wight of the Button as 1, so it will take 1/8th of the space. I am using 8 just as an example, you can change it to any number you want, do some trial and error see what weights are suiting your needs the best.
Also, the weights are in float, so don't forget the "f" after the number, like 1f and 7f:
(Again, I am not near my dev machine so there may be some errors. But this should work.)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 7f);
// Last parameter 6f defines weight. So yourEditText will take 7/8th of the space
linearLayout.addView(yourEditText, layoutParams);
layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
// Last parameter 6f defines weight. So yourEditText will take 1/8th of the space
linearLayout.addView(yourButton, layoutParams);
Let's see how this helps. :)

Android: Add new edittext onto new lines

I have added a button and an edittext onto a framelayout. The problem is I appear to lose the functionality of being able to click the button. When I click the button, the keyboard appears for the edittext. How can I add button/editext objects into a layout so that they appear on a new row and do not affect each other. I was using the padding option to move them about, but this still causes the button to be non-clickable.
Here is my code: -
FrameLayout reportLayout = (FrameLayout) findViewById(R.id.reportDetailLayout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button executeButton = new Button(this);
executeButton.setClickable(true);
executeButton.setOnClickListener(handleOnClick(executeButton));
EditText text1 = new EditText(this);
executeButton.setText("Execute");
executeButton.setMinimumHeight(10);
executeButton.setMinimumWidth(150);
text1.setId(1);
text1.setHint("Enter Value");
executeButton.setPadding(0, 0, 0, 0);
text1.setPadding(12, 70, 0, 0);
executeButton.setLayoutParams(params);
text1.setLayoutParams(params);
reportLayout.addView(executeButton);
reportLayout.addView(text1);
FrameLayout will cause Views to overlap. Try LinearLayout with setOrientation().
And you don't use padding for placement. Padding is not same thing as a margin.

Android adding dynamic button click disabled when adding edittext

I am dynamically adding a button to a frame layout. Which works great. When I add an edittext object to the same frame layout, it appears the functionality of being able to click the button appears to stop.
Can anybody help me with this. Here is my code: -
FrameLayout reportLayout = (FrameLayout) findViewById(R.id.reportDetailLayout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button executeButton = new Button(this);
executeButton.setClickable(true);
executeButton.setOnClickListener(handleOnClick(executeButton));
EditText text1 = new EditText(this);
executeButton.setText("Execute");
executeButton.setMinimumHeight(10);
executeButton.setMinimumWidth(150);
text1.setId(1);
text1.setHint("Enter Value");
executeButton.setPadding(0, 0, 0, 0);
text1.setPadding(12, 70, 0, 0);
executeButton.setLayoutParams(params);
text1.setLayoutParams(params);
reportLayout.addView(executeButton);
reportLayout.addView(text1);
Thanks
Martin
your edit text is top of your button. your touch event can clicks only edit texts area.
you change button or edit text's location, i think this problem will solve.
It needed to be a Linear Layout.

Setting margins for an imageview dynamically

may I know how to set margin in imageview dynamically?
You're probably looking for something like this: http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)
Note this part of the method description though:
These supply parameters to the parent of this view specifying how it
should be arranged
Which means that if you have an ImageView inside of a LinearLayout, you need to supply the method with LinearLayout.LayoutParams, like this:
ImageView image = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
params.setMargins(1, 1, 1, 1);
image.setLayoutParams(params);
And then you just call setMargins or set the specific leftMargin, bottomMargin etc. properties of the LayoutParams.

Categories

Resources