I programmatically created four Textviews and added it in my Linearlayout.my linearlayout has 216dp width and TextView which I created pro-grammatically has 48 dp width and height.
I want to add padding between TextView. I wrote some code but I received this result
this is a my code
for (int i = 0; i < mDigits; i++) {
DigitView digitView = new DigitView(getContext());
digitView.setWidth(valueInPixels);//48dp
digitView.setHeight(valueInPixels);//48dp
digitView.setTextColor(Color.WHITE);
digitView.setTextSize(mDigitTextSize);
digitView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
digitView.setElevation(mDigitElevation);
}
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.leftMargin = 10;//problem is this line
}
childView.addView(digitView, lp);//childview 216dp
}
how i can to create correct padding between views programmatically?
if anyone has solution please help me
thanks.
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
}
Add padding on view programmatically
Here you have the solution to create a padding in java code.
Try this,
public void setPadding (int left, int top, int right, int bottom)
view.setPadding(0,padding,0,0);
Related
I add two Checkboxes dynamically to a Linearlayout. Then those Linearlayouts are added one after another in a Relativelayout. The weights of the checkboxes are set so that each take 50% of the Linearlayout width. Now, if their heights do not match, the bottom of the checkbox with bigger height disappears. How to solve this? Here's a screenshot:
And the code:
LinearLayout ll;
LinearLayout.LayoutParams lp;
CheckBox ch;
int id = 1200, i, j;
for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) {
ll = new LinearLayout(NotificationSettings.this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ch = new CheckBox(NotificationSettings.this);
lp.weight = 1.0f;
ch.setLayoutParams(lp);
ch.setText(selections.get(i));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j);
ll.addView(ch);
ch = new CheckBox(NotificationSettings.this);
ch.setLayoutParams(lp);
ch.setText(selections.get(i + 1));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j + 1);
ll.addView(ch);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (id == 1200)
p.addRule(RelativeLayout.BELOW, addBelow);
else
p.addRule(RelativeLayout.BELOW, id);
ll.setLayoutParams(p);
ll.setId(++id);
rl.addView(ll);
}
Edit:
When both checkboxes have multiple lines:
Can you make sure that the Linear Layout's height below it is not too large that it covers the above Linear Layout?
Or try changing your Relative Layout Params' height to WRAP_CONTENT
Change
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
To
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Here is my code:
private TabLayout tabLayout;
private int[] tabIcons = {
R.mipmap.ic_compass,
R.mipmap.ic_place,
R.mipmap.ic_passport,
R.mipmap.ic_setting
};
...
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
The size of icon is according to image size. How could I resize it?
set icons padding
for (int i = 0; i < tablayout.getTabWidget().getChildCount(); i++)
{
tablayout.getTabWidget().getChildAt(i).setPadding(10,10,10,10);
}
If you don't want to use xml for the custom view. Simply create imageview on runtime and add that in tablayout in a particular tab.
ImageView imgView= new ImageView(MainActivity.this);
imgView.setImageResource(drawableImage);
imgView.setPadding(10,10,10,10)
tabLayout.getTabAt(1).setCustomView(imgView);
It will look like this.
change of Icon size android.support.design.widget.TabLayout
for (int i = 0; i < view_bottom_tabLayout.getTabCount(); i++)
{
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(60, 60); //set new width & Height
params.gravity = Gravity.CENTER; //set gravity back to center
view_bottom_tabLayout.getChildAt(i).setLayoutParams(params);//set ur new params
}
To get total control of size I recommend using a custom view to render the tab.
First create a new layout - named "my_custom_tab" for this example
<?xml version="1.0" encoding="utf-8"?>
<!--Change the width, height, scaleType to whatever you like-->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:id="#+id/icon"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher"/>
Now in your code set the custom image
private void setUpTabs (ViewPager viewPager) {
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
if (tabs != null) {
tabs.setupWithViewPager(viewPager);
int tabCount = tabAdapter.getCount(); //Assuming you have already somewhere set the adapter for the ViewPager
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = tabs.getTabAt(i);
if (tab != null){
ImageView myCustomIcon = (ImageView) LayoutInflater.from(tabs.getContext()).inflate(R.layout.my_custom_tab, null);
/*Here is where to set image if doing it dynamically
myCustomIcon.setImageBitmap(bitmap);
*/
tab.setCustomView(myCustomIcon);
}
}
}
}
you can do this:
LinearLayout ll = (LinearLayout) tabLayout.getChildAt(0);
for (int i = 0; i < ll.getChildCount(); i++) {
LinearLayout tabView = (LinearLayout) ll.getChildAt(i);
for (int j = 0; j < tabView.getChildCount(); j++) {
if (tabView.getChildAt(j) instanceof TextView) {
TextView textView = (TextView) tabView.getChildAt(j);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
layoutParams.topMargin = 0;
textView.setLayoutParams(layoutParams);
} else if (tabView.getChildAt(j) instanceof ImageView) {
ImageView imageView = (ImageView) tabView.getChildAt(j);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.bottomMargin = 0;
imageView.setLayoutParams(layoutParams);
}
}
}
I decide to close this question. Since we actually cannot set specific size for icon in TabLayout but padding to it.
Ref:
https://developer.android.com/guide/practices/ui_guidelines/icon_design_tab
one code worked for me, the answer above of Deepak Sachdeva
just i edited it a litlle, try it 100% working
from first i add the tab with custom image
ImageView imgView= new ImageView(getApplicationContext());
imgView.setImageResource(R.drawable.icon);
imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imgView.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
tablayout1.addTab(tablayout1.newTab().setCustomView(imgView));
and do that to all your tabs, add them by this way.
I'm trying to add LinearLayouts dynamically but it's just not working. I think I just need another set of eyes to look it over. Can anyone help me?
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
parentLayout.addView(lLayout[i]);
}
You need to set LayoutParams, try adding this:
lLayout[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
It looks like you are trying to make a listview yourself using linearlayouts rather than actually using the ListView already supported.
If you are actually trying to do that, you should first give the layouts inside the view a width and height. I'd also put the list in a scrollview in case it overflows the outer layout.
Maybe try this
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
lLayout = new LinearLayout[8];
for(int i = 0; i < lLayout.length; i++) {
lLayout[i] = new LinearLayout(this);
lLayout[i].setId(i);
lLayout[i].setOrientation(LinearLayout.HORIZONTAL);
if(i%2 == 0) {
lLayout[i].setBackgroundColor(Color.GREEN);
} else {
lLayout[i].setBackgroundColor(Color.MAGENTA);
}
LinearLayout.LayoutParams myLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
myLayoutParams.leftMargin = 0;
myLayoutParams.topMargin = 50 * i;
myLayoutParams.width = myScreenSize; //e.g. 480
myLayoutParams.height = 50;
lLayout[i].setLayoutParams(myLayoutParams);
parentLayout.addView(lLayout[i]);
}
I want to create 4 RelativeLayout dynamically so that each subsequent layout is placed below the previous one. I'm trying to do this whit this piece of code:
RelativeLayout layoutParent = (RelativeLayout)findViewById(R.id.layoutParent);
int layouts = 4;
int dp15 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(dp15, dp15, dp15, dp15);
for (int l = 0; l <= layouts; l++)
{
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
if (l != 0) params.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout);
}
But I can't get the desired position of each layout. Can someone tell me how can I do what I wanna do?
Thank you in advance!
You set the BELLOW rule but never use it when you add the child RelativeLayout to the parent layout(as MisterSquonk said). Also use another set of LayoutParams for the child RelativeLayout:
for (int l = 0; l <= layouts; l++) {
RelativeLayout queueLayout = new RelativeLayout(getApplicationContext());
TextView one = new TextView(getApplicationContext());
one.setText(String.valueOf(l));
queueLayout.setId(2000 + l);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (l != 0) lp.addRule(RelativeLayout.BELOW, queueLayout.getId() - 1);
queueLayout.addView(one, params);
layoutParent.addView(queueLayout, lp);
}
Here's the thing: I want to add some images programmatically. The images should have to have a topMargin of 5dip except for the first image, in a LinearLayout with a vertical orientation manner. below the code segment:
LinearLayout body = (LinearLayout) findViewById(R.id.body);
for (int i = 1; i <= 4; i++) {
ImageView img = new ImageView(this);
MarginLayoutParams lp = new MarginLayoutParams(-2, -2);
img.setImageResource(R.drawable.image);
if (i != 1) {
lp.setMargins(0, 5, 0, 0);
}
img.setLayoutParams(lp);
body.addView(img);
body.requestLayout();
}
By running the program I can see the the 4 images(here) vertically aligned one by one but there is no topMargin(as in the code,5dip). body is the id of the LinearLayout. here's the XML segment to:
<LinearLayout
android:id="#+id/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#901b0e08"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="8dp" >
</LinearLayout>
I cant get what went wrong here.
Thanks.
Try changing your MarginLayoutParams to this:
LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
The reason for doing this, is that body is a LinearLayout and thus you would want to use the LinearLayout-specific LayoutParams.
Try to set padding but margin for ImageView. Sometimes it works fine. Your can directly set padding size to ImageView w/o declare LayoutParams.
You probably need to set LayoutParams' gravity property, for example:
lp.gravity = 0; //AXIS_X_SHIFT
That's what works fine for me.