I am trying to inflate a view multiple times.
my view consists of one imageview and a textview below it and placing it at particular position.
Below is my item.xml which I am inflating multiple times:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="100dp"
android:background="#android:color/holo_blue_dark"
android:layout_height="100dp">
<ImageView
android:layout_gravity="center"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#mipmap/ic_launcher"
android:id="#+id/imageView"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="20dp"
android:textSize="12sp"
android:textStyle="bold"
android:text="Large Text"
android:id="#+id/textView" />
</LinearLayout>
Here is my code with inflate:
for (int i = 0; i < 1; i++) {
final View v = linf.inflate(R.layout.item, null);
TextView tvs = (TextView) v.findViewById(R.id.textView);
ImageView ivs = (ImageView) v.findViewById(R.id.imageView);
tvs.setText(i+"");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
int w = (mWidth/2)-50;
int h =(mHeight/2)-50;
params.leftMargin = w;
params.topMargin = h;
Log.e("W-H", mWidth + "-" + mHeight);
rr.addView(v,params);
}
I am able to place the item.xml successfully but its not fitting screen properly I have set height and width fixed 100dp.
Here is what I am getting
here
The constructor for LayoutParams is in pixels, so you have to convert to dps for it to display correctly on your screen. This can be accomplished as in this stackoverflow answer:
int width_dps = 100;
int height_dps = 100;
final float scale = MainActivity.this.getResources().getDisplayMetrics().density;
int width_pixels = (int) (width_dps * scale + 0.5f);
int height_pixels = (int) (height_dps * scale + 0.5f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width_pixels, height_pixels);
Alternatively, if you are using a fixed size, you can update your xml to allow wrap_content to work correctly. You will be overriding the LinearLayout params, so you can update your subviews:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
and update a subview (android:layout_width="100dp"):
<TextView
android:gravity="center"
android:layout_width="100dp"
android:layout_height="20dp"
android:textSize="12sp"
android:textStyle="bold"
android:text="Large Text"
android:id="#+id/textView" />
I have code to add checkboxes from an Array to the LinearLayout.
LinearLayout my_layout = (LinearLayout) findViewById(R.id.test);
for (int n = 0; n < listitems.size(); n++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(Integer.parseInt(listitems.get(n).get("cbid")));
cb.setText(listitems.get(n).get("product"));
cb.setTextColor(Color.BLACK);
my_layout.addView(cb);
}
how can i make sure that between each checkbox there is a margin of 2-3dp?
And that the background of the checkboxes has rounded edges?
This is my XML to set the boxes in
<LinearLayout
android:id="#+id/Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/test"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp" />
<LinearLayout
android:id="#+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#13ca8c"
/>
</LinearLayout>
LinearLayout my_layout = (LinearLayout) findViewById(R.id.test);
for (int n = 0; n < listitems.size(); n++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(Integer.parseInt(listitems.get(n).get("cbid")));
cb.setText(listitems.get(n).get("product"));
cb.setTextColor(Color.BLACK);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
parms.leftMargin = 2;
params.rightMargin = 2;
params.topMargin = 2;
params.bottomMargin = 2;
my_layout.addView(cb,params);
}
And for rounded corners use a background image
i have two linear layouts in one frame layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/image12">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layoutbtnlinear_aboutme"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:gravity="bottom"
android:layout_marginTop="10dp"
android:background="#b2b2b2"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgShare_layout_aboutme"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="right|center|end"
android:layout_weight="1.63"
android:src="#drawable/ic_share" />
<TextView
android:id="#+id/txtTitle_layout_aboutme"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="left"
android:layout_weight="0.3"
android:fontFamily="Times New Roman"
android:text="About Me"
android:textColor="#android:color/black"
android:textSize="35sp"
android:textStyle="italic" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:id="#+id/btnSlidingDrawerHandler_layout_aboutme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/ic_1_navigation_collapse" />
<ListView
android:id="#+id/listView_layout_aboutme"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:footerDividersEnabled="true"
android:dividerHeight="4px"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical">
</ListView>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Here I'm seting top margin of linear layout with id layoutbtnlinear_aboutme to 10dp but in code I want to change this 10dp to 50dp on some condition how can I change this top margin programatically?
layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
params.setMargins(0, 50, 0, 0);
layout.setLayoutParams(params);
LayaoutParams usually create confusion while setting margin because of their parent layout... So this MarginLayoutParams is very useful which works with all layouts.
Java Code
MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
params.width = 200; //Ths value 200 is in px... Please convert in DP
params.leftMargin = 100;
params.topMargin = 200;
Kotlin code
val params: MarginLayoutParams = view!!.layoutParams as MarginLayoutParams
params.width = 200
params.leftMargin = 100
params.topMargin = 200
This updates the top margin without the need to update the other margin values.
LinearLayout layout = (LinearLayout) findViewById(R.id.your_linear_layout);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.topMargin = 200;
layout.setLayoutParams(layoutParams);
use this
layout = (LinearLayout) findViewById(R.id.layuout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
layout.setLayoutParams(layoutParams );
Use this method to set margin in dp
private void setMargins (View view, int left, int top, int right, int bottom) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
final float scale = getBaseContext().getResources().getDisplayMetrics().density;
// convert the DP into pixel
int l = (int)(left * scale + 0.5f);
int r = (int)(right * scale + 0.5f);
int t = (int)(top * scale + 0.5f);
int b = (int)(bottom * scale + 0.5f);
p.setMargins(l, t, r, b);
view.requestLayout();
}
}
Call the method :
setMargins(linearLayout,0,0,5,0);
https://stackoverflow.com/a/50951911/8523391
Kotlin solution:
fun LinearLayout.setMargins(left: Int? = null, top: Int? = null, right: Int? = null, bottom: Int? = null) {
val params: LinearLayout.LayoutParams = this.layoutParams as LinearLayout.LayoutParams
val left = left?.let { it } ?: params.leftMargin
val top = top?.let { it } ?: params.topMargin
val right = right?.let { it } ?: params.rightMargin
val bottom = bottom?.let { it } ?: params.bottomMargin
params.setMargins(left, top, right, bottom)
this.requestLayout()
}
then you just need to:
layoutbtnlinear_aboutme.setMargins(top = 10)
I have set up margins directly using below code (I tried using LinearLayout.LayoutParams but is was not working for me)
LinearLayout layout = (LinearLayout)findViewById(R.id.yourrelative_layout);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
params.setMargins(3, 300, 3, 3);
layout.setLayoutParams(params);
Only this here is to notice that LayoutParams should be imported for following package android.widget.RelativeLayout.LayoutParams unless you will hit error.
How to set programmatically width of TextView to be 0dp ? I am trying to put in LinearLayout couple TextViews but all to be same width, and when I put in xml like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="left"
android:gravity="left"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="test"
android:layout_gravity="right"
android:gravity="right"
/>
</LinearLayout>
but when in code try like
TextView txtTemp = new TextView(this);
txtTemp.setText(captures.get(i));
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
.getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;
if (i == 0) {
tempLL.gravity = Gravity.LEFT;
}
if (i == (captures.size() - 1)) {
tempLL.gravity = Gravity.RIGHT;
} else {
tempLL.gravity = Gravity.CENTER;
}
txtTemp.setLayoutParams(tempLL);
I got exception on tempLL.width null exception. How to solve this ?
Instead of using
LinearLayout.LayoutParams tempLL = (LinearLayout.LayoutParams) txtTemp
.getLayoutParams();
tempLL.width =0;
tempLL.height = LinearLayout.LayoutParams.WRAP_CONTENT;
tempLL.weight = 1;
try this
LinearLayout.LayoutParams tempLL = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT,1.0);
txtTemp.setLayoutParams(tempLL);
You are creating a new textview and there is no layoutparams specified yet. So getLayoutParams should return null. So create a new layoutparam object and set that to the new textview.
Hi a have RelativeLayout and want display 6 buttons like a "TableLayout", i calc size buttons and tried display on Activity. Only last button appear.
I trying do Activity with 6 buttons (buttonsperscreen = 6) when call method.
Can you help me?
The code is:
private void doLayoutWithButtons(int buttonsperscreen) {
// if total butons is % 2 == 0
if (buttonsperscreen % 2 == 0) {
// get display dimensions
DisplayMetrics d = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(d);
int w = d.widthPixels;
int h = d.heightPixels;
// calc size of buttons
int widthButton = w / 2; // only two columns of buttons.
int heightButton = h / ((buttonsperscreen) / 2); // sample 100px /
// (6/2) = 3
int posx = 0;
int posy = 0;
for (int i = 1; i <= buttonsperscreen; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("Botão: " + i);
// atribuindo o tamanho do botão.
newButton.setHeight(heightButton);
newButton.setWidth(widthButton);
newButton.setId(1000 + i);
// positioning buttons...
RelativeLayout layout1 = new RelativeLayout(this);
// set layout with size of button
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthButton, heightButton);
params.leftMargin = posx;
params.topMargin = posy;
newButton.setLayoutParams(params);
layout1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(newButton);
setContentView(layout1);
// to calc positions x,y for each button for next iteration
if (posx + widthButton < w) {
posx = posx + widthButton;
} else {
posx = 0;
posy = posy + heightButton;
}
}
}
}
Thanks
Mateus
If you're trying to display buttons evenly spaced across a screen, you don't need to do this in code. You can specify things like this in a layout.
Something like this will give 6 buttons equal widths displayed horizontally.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="#+id/button1"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="#+id/button2"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="#+id/button3"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="#+id/button4"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="#+id/button5"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="#+id/button6"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
As long as you keep the layout_width to be 0dp, and set the weight to be 1, Android will automatically set the widths to be equal, no matter what number of buttons you have.