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" />
Related
I am attempting to create a key for an above pie chart within this Linear layout. However, the weights aren't working properly, and the three columns are split up equally. Does anyone know what may be causing this? Thanks!
Layout XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout1"
android:orientation="vertical"
android:layout_centerHorizontal="true">
<TableRow android:gravity="center"
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:weightSum="4">
<ImageButton
android:layout_width="0dp"
android:layout_height="100dp"
android:id="#+id/aiButton"
android:layout_gravity="center_horizontal"
android:src="#drawable/barchartwhite"
android:background="#CCAF00"
android:layout_weight="1"
android:scaleType="fitCenter"
/>
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
android:text="TP Activity"
android:id="#+id/textView2"
android:layout_gravity="center|center_vertical"
android:textSize="40sp"
android:background="#CCAF00"
android:layout_weight="3"
android:layout_alignParentTop="true"
android:textColor="#FFFFFF"
android:layout_centerHorizontal="true"
android:gravity="center_vertical" />
</TableRow>
<TableRow android:gravity="center"
android:id="#+id/tableRowSub"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage of Activity per TP"
android:id="#+id/textViewSub"
android:layout_gravity="center"
android:textSize="16sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip">
<com.shannonsystemsllc.ediconnect.PieChartView
xmlns:chart="http://schemas.android.com/apk/res-auto"
android:id="#+id/piechart"
android:paddingRight="12dp"
android:paddingLeft="12dp"
android:layout_width="300sp"
android:layout_height="300dp"/>
</TableRow>
</LinearLayout>
Code for creating key:
for (int i = 0; i < customers.size(); i++) {
int c[] = {Color.parseColor("#FF4B66"),Color.parseColor("#00A9AC"),Color.parseColor("#70A200"),Color.parseColor("#FAB448"),Color.parseColor("#BFBFBF"),Color.GREEN,custColor,Color.CYAN,Color.BLUE};
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
TableLayout.LayoutParams tl = new TableLayout.LayoutParams();
final float scale = getActivity().getResources().getDisplayMetrics().density;
int pixels = (int) (30 * scale + 0.5f); //set pixels to dp
TableRow row = new TableRow(activity);
row.setWeightSum(9);
//first row
tl.weight = 1;
lp.weight = 6;
lp.height = pixels;
row.setLayoutParams(lp);
TextView tv1a = new TextView(getActivity());
tv1a.setTextSize(22);
tv1a.setText(customers.get(i));
tv1a.setLayoutParams(lp);
tv1a.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
TextView tv1b = new TextView(getActivity());
tv1b.setTextSize(24);
Float perFloat = ((float)pieChartValues[i]/dataTotal*100);
remainderTotal = remainderTotal - perFloat.intValue();
String percentage;
if (perFloat.intValue() == 0)
{
percentage = ("1%");
}
else
{
percentage = (perFloat.intValue() + "%");
}
lp.weight = 2;
tv1b.setText(percentage);
tv1b.setLayoutParams(lp);
tv1b.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
lp.weight = 1;
ImageView tv1c = new ImageView(getActivity());
tv1c.setLayoutParams(lp);
tv1c.setBackgroundColor(c[i]);
row.setWeightSum(3);
row.addView(tv1b);
row.addView(tv1c);
row.addView(tv1a);
row.setId(i);
ll.addView(row);
}
Screenshot:
Set the weight of the contents inside each row as follows:
- For the percentage TextView, set the weight to 3.
- For the ImageView, set the weight to 8.
- For the name Textview, set the weight to 7.
This will result to the entire row to look slightly offset from the centre. Make sure all three views are set to match parent width.
Once you set weights for the views call:
yourLinearLayout.invalidate()
Try this out and see if it works.
I have an ImageView and a TextView within a RelativeLayout that is nested inside a ScrollView. I can't get the Top of the ImageView to line up with the top of the TextView. It hovers about 150dp down from the top of the text view top edge.
Here is my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transitionForm"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backgroundcolor"
android:orientation="horizontal" >
<ScrollView
android:id="#+id/svTutorial"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:splitMotionEvents="true"
android:layout_above="#+id/ImageBottom"
android:layout_below="#+id/ImageTop"
android:scrollbars="vertical"
>
<RelativeLayout
android:id="#+id/layoutTransitionPicWidgets"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/lblQuestionText"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:layout_marginBottom="30px"
android:layout_marginLeft="50px"
android:layout_marginTop="10dp"
android:layout_marginRight="25px"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:text="get text from db."
android:textColor="#000000"
android:textSize="20sp" />
<ImageView
android:id="#+id/imgPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/slidetest"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I'm trying to programmatically change the width of the Image based on its size, so I am setting all the parameters programmatically.
Here is the code:
imgPic = (ImageView)findViewById(R.id.imgPic);
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
final float density = getResources().getDisplayMetrics().density;
final float dpHeight = outMetrics.heightPixels / density;
final float dpWidth = outMetrics.widthPixels / density;
ViewTreeObserver vto = imgPic.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
imgPic.getViewTreeObserver().removeOnPreDrawListener(this);
final int finalHeight = imgPic.getMeasuredHeight();
final int finalWidth = imgPic.getMeasuredWidth();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = Math.round(.4f * dpWidth * density);
params.leftMargin = 50;
params.topMargin = 0;
params.rightMargin = 25;
//params.gravity= Gravity.TOP;
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.RIGHT_OF, R.id.lblQuestionText);
imgPic.setLayoutParams(params);
return true;
}
});
I've tried doing this with a LinearLayout and get the same results. What am I missing?
You can add this to your ImageView
android:layout_alignTop="#+id/lblQuestionText"
but I think there's more going on than that. You don't specify any alignment attributes on the ImageView, so it may not layout where you think it's going to layout.
It seems like maybe you want the ImageView to be to the right of the TextView, in which case adding this to the ImageView should help:
android:layout_toRightOf="#+id/lblQuestionText"
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.