programmatically set margin/ padding of textview in a listview programmatically set margin/ padding of textview in a listview android android

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().


Be careful, (int) pixels != dp

public int dp2px(int dp) {    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());}


Try the following code, it works for me.

textView.setX(40);

My solution is for SDK 14 and above. Not in ListView but RecyclerView.