Add padding on view programmatically
view.setPadding(0,padding,0,0);
This will set the top padding to padding
-pixels.
If you want to set it in dp
instead, you can do a conversion:
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
programmatically set margin/ padding of textview in a listview
maintitle.setPadding(50, 0, 0, 0);
(method 2) should work.
I think the problem is that, although you do a lot of changes to the TextView
, at the end you kind of inflate a new layout again in:
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
R.layout.main_alllatestnewslist, from, to);
so, make a custom adapter, then inflate and customize your TextView
in the getView()
.
setPadding() on Button programmatically not working
OK, with help from war_Hero in comments I've found the solution.
setBackgroundColor() don't work. It sets the entire background, including the padding, to the same color.
This:
replyButton.setBackgroundColor(Color.parseColor("#C70000"));
Should be replaced with something like this:
replyButton.getBackground().setColorFilter(ContextCompat.getColor(mContext, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
Android: set just one padding of textview programmatically
use
yourTextView.setPadding(0, 10, 0, 0);
Adjust only the parameters you need and set the other ones to zero.
If you need to preserve other existing paddings, use yourView.getPaddingLeft()
, yourView.getPaddingTop()
and so on.
android correct padding between views programmatically
First add appropriate android:paddingLeft to your childview I added as below:
<LinearLayout
android:id="@+id/childView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:background="#000"
android:orientation="horizontal">
Then edit you code as below
for (int i = 0; i < 4; i++) {
DigitView digitView = new DigitView(this);
digitView.setWidth(valueInPixels);//48dp
digitView.setHeight(valueInPixels);//48dp
digitView.setTextColor(Color.WHITE);
digitView.setBackgroundColor(Color.YELLOW);
digitView.setTextSize(mDigitTextSize);
digitView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
digitView.setElevation(mDigitElevation);
}
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.rightMargin = 10;//problem is this line
childView.addView(digitView, lp);//childview 216dp
}
How to add Margin/padding in LinearLayout programmatically?
Try this one code
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
horizontalLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
hope so it will be useful for you. :)
How to add padding completely around a bitmap programmatically?
This may or may not help you but don't pad the Bitmap, add it to a container in the view like a linearlayout and then pad the linearlayout . It should be a lot easier to deal with.
int left = (canvas.getWidth()-mainBitmap.getWidth())/2;
int top = (canvas.getHeight()-mainBitmap.getHeight())/2;
canvas.drawBitmap(mainBitmap, left, top, null);
This should get the the bitmap to the center in relation to the canvas.
I got it from here if you need more info How to align the canvas object to center of the screen?
Add padding to FrameLayout programmatically without moving inner Fragment with ScrollView content
If you have still problem you can use :
android:descendantFocusability="blocksDescendants"
In your child ScrollView.
Related Topics
Get Cursor Position in Android in Edit Text
How to Use View.Ontouchlistener Instead of Onclick
Remove Image from Cache in Glide Library
How to Programmatically Set the Background Color Gradient on a Custom Title Bar
How to Style the Standard React-Native Android Picker
Android Broadcastreceiver, Auto Run Service After Reboot of Device
The Import Android.Support.V7 Cannot Be Resolved
Android: How to Change Checkbox Size
Livedata Update on Object Field Change
Android: Automatically Choose Debug/Release Maps API Key
Getapplicationcontext(), Getbasecontext(), Getapplication(), Getparent()
Why Do I Get Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Android - R Cannot Be Resolved to a Variable
Android - Hold Button to Repeat Action
Android: Generate Random Color on Click
How to Perform an SQLite Query Within an Android Application