params.setMargins does not work programmatically - android

I set layout_marginStart and layout_marginTop in xml and it is ok, but when I try to set programmatically margins it does not work. I try to find how to resolve, but can not find resolution.
What I do not correctly?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/app_bar_resourses">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyGridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|center"
android:background="#color/borderHigh"
android:columnCount="2"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/firstLinear1"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/firstLinear2"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginStart="#dimen/resourceBorder"
android:layout_marginTop="#dimen/resourceBorder"
android:background="#drawable/resource_panel"
android:orientation="vertical" />
</GridLayout>
</ScrollView>
XML above works
I try to do this by code, and it must work but i can not resolve this problem
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int dp9 = dpToPx(9);
GridLayout MyGridLayout = findViewById(R.id.MyGridLayout);
final int N = 10; // total number of textviews to add
for (int i = 0; i < N; i++) {
LinearLayout testlayout = new LinearLayout(this);
//LinearLayout.LayoutParams tst = new LinearLayout.LayoutParams(0,0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.height = (width/2)-dp9;
params.width = (width/2)-dp9;
params.setMargins(6, 6, 0, 0);
testlayout.setLayoutParams(params);
testlayout.setOrientation(LinearLayout.VERTICAL);
testlayout.setBackgroundResource(R.drawable.resource_panel);
TextView title = new TextView(this);
title.setText("TextView "+i);
title.setTextColor(getResources().getColor(R.color.colorPrimary2));
title.setAllCaps(true);
title.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(title, 10, 24, 1, TypedValue.COMPLEX_UNIT_SP);
testlayout.addView(title);
TextView text = new TextView(this);
text.setText("Text "+i);
testlayout.addView(text);
TextView date = new TextView(this);
date.setText("date "+i);
testlayout.addView(date);
}

Related

Setting layout weight to dynamically added view is not working in Android

I am developing an Android App. In my app, I am adding view dynamically and setting its weight in code as well. But it is not working.
This is my XML for container of dynamic views:
<ScrollView>
.
.
.
<LinearLayout
android:orientation="vertical"
android:id="#+id/id_related_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></LinearLayout>
.
.
.
</ScrollView>
This is how I am adding controls dynamically to it.
private void addRelatedItemViews(final ArrayList<Item> relatedItems)
{
LinearLayout subContainer= new LinearLayout(this);
LinearLayout.LayoutParams initialParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(initialParam);
relatedItemsContainer.addView(subContainer);
for(int i=0;i<relatedItems.size();i++)
{
if(i>0 && i%2==0)
{
//initialize sub container
subContainer = new LinearLayout(this);
LinearLayout.LayoutParams subContainerParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
subContainer.setLayoutParams(subContainerParam);
relatedItemsContainer.addView(subContainer);
}
final int index = i;
View view = layoutInflater.inflate(R.layout.realated_item_view,null);
ImageView imageView = (ImageView)view.findViewById(R.id.iv_related_item_image);
TextView tvName = (TextView)view.findViewById(R.id.tv_related_item_name);
TextView tvPrice = (TextView)view.findViewById(R.id.tv_related_item_price);
TextView tvLikeCount = (TextView)view.findViewById(R.id.tv_related_item_like_count);
Picasso.with(getBaseContext()).load(relatedItems.get(i).getMediumImageUrl()).into(imageView);
tvName.setText(relatedItems.get(i).getName());
tvPrice.setText(CommonHelper.formatCurrency(relatedItems.get(i).getPrice()));
tvLikeCount.setText(CommonHelper.formatLikeCount(relatedItems.get(i).getLikeCount()) + " like");
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openItemActivity(relatedItems.get(index).getId());
}
});
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
}
}
This is my realated_item_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_weight="1"
android:padding="3dp"
android:layout_height="wrap_content">
<LinearLayout
android:background="#drawable/item_bg"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_related_item_image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:padding="5dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:textSize="17dp"
android:id="#+id/tv_related_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="13dp"
android:id="#+id/tv_related_item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="3dp"
android:textSize="10dp"
android:id="#+id/tv_related_item_like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
But it is not settings its weight properly. Actually, each row has two column and each column must be half of screen width. But it is not working.
This is the screenshot:
As you can see, it not taking screen half by half. How can I fix it?
Try this below in your realated_item_view.xml (wrap_content to fill_parent)
<?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_weight="1"
android:padding="3dp"
And change your Java code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
itemContainer.addView(view);
subContainer.addView(itemContainer);
But maybe your image will be stretched by the width. But it will fill half of the width as you want.
UPDATE:
I dont know why you tried failed with above solution. So I write a small demo for referencing. It uses ScrollView as you want. But I strongly recommend you should use GridView instead for better perfomance!!!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
LinearLayout containerLayout = (LinearLayout) findViewById(R.id.container_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
for (int i = 0; i < 4; i ++){
LinearLayout subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams subParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1);
for (int j = 0; j < 3; j ++){
View v = inflater.inflate(R.layout.test_item_layout, null, false);
if (j == 1){
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.icon);
}
subLayout.addView(v, subParams);
}
containerLayout.addView(subLayout, params);
}
}
test_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView"
android:src="#mipmap/ic_launcher"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="100dip" />
<TextView
android:text="Test label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
test_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
Screenshot:
Instead of ViewGroup.LayoutParams try it with LinearLayout.LayoutParams and also set layout width to 0
LinearLayout itemContainer = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1);
itemContainer.setLayoutParams(params);
It might work.
try this
use straggerdgridlayout manager
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(column, 1);
recyclerView.setLayoutManager(gaggeredGridLayoutManager);
column replaces your requirement of column

ScrollView not surrounding entire RelativeLayout

What I did was I added a bunch of TextViews programmatically, but now the ScrollView won't resize to fit all the new TextViews created in the onCreate() method.
onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_LEFT);
for (int i = 1; i < 21; i++) {
TextView number = new TextView(this);
number.setText(Integer.toString(i));
Log.i("i", Integer.toString(i));
number.setLayoutParams(lp);
number.setY(i / 2.54f * getResources().getDisplayMetrics().densityDpi);
relativeLayout.addView(number);
}
}
activity_main.xml:
<ScrollView
android:id="#+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"/>
</ScrollView>
Is there any way to make the ScrollView fit everything inside the RelativeLayout?
Try this,
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="#+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</RelativeLayout>
</ScrollView>
Add this to your code,
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relative_layout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lp);
tv1.setId(1);
rLayout.addView(tv1);
for(int i=1;i<25;i++)
{
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView tv = new Button(this);
tv.setText("Hello "+i+1);
newParams.addRule(RelativeLayout.BELOW, i);
tv.setLayoutParams(newParams);
tv.setId(2);
rLayout.addView(tv);
}

Cannot Change Width of TextView in Code

What I want to do is that ... I want my TextView at the right side of the screen like "Note Edge" screen.
What I have tried is that
This is XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_alignParentRight="true"
android:id="#+id/txtVertical" />
</RelativeLayout>
This is Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView txtVertical = (TextView)findViewById(R.id.txtVertical);
txtVertical.setBackgroundColor(Color.CYAN);
txtVertical.setPivotX(0);
txtVertical.setPivotY(0);
txtVertical.setRotation(90f);
txtVertical.setX(100f);
txtVertical.setY(100f);
txtVertical.setText(txtVertical.getText() + " ");
txtVertical.setSelected(true);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//txtVertical.setWidth(size.y); //doesn't work
txtVertical.getLayoutParams().width = 1000; //doesn't work also
txtVertical.requestLayout();
Log.d("size.y", size.y + "");
}
If I set the attribute android:layout_width="wrap_content", it doesn't work either. How can I change the width of the TextView?
Try this :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtVertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="#string/hello_world" />
</RelativeLayout>
Hope this may helps you
LayoutParams params = textView.getLayoutParams();
params.height = 70;
params.width= 50;
textView.setLayoutParams(params);
Try out;
txtVertical.getLayoutParams().width = 200
(in pixel)
Just add these layoutparams to your TextView programmatically:
// Declaring the TextView LayoutParams
LinearLayout.LayoutParams text_lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
text_lp.gravity = Gravity.CENTER;
// Declaring the TextView itself
TextView txtVertical = (TextView)findViewById(R.id.txtVertical);
txtVertical.setLayoutParams(text_lp);
txtVertical.setGravity(Gravity.RIGHT);
txtVertical.setSingleLine();
txtVertical.setEllipsize(TruncateAt.MARQUEE);
txtVertical.setSelected(true);
//complete text your customization
try this way
TextView txtVertical = (TextView)findViewById(R.id.txtVertical);
txtVertical.requestLayout();
txtVertical.getLayoutParams().height = 230;
txtVertical.getLayoutParams().width = 400;
txtVertical.setPadding(6, 6, 6, 6);

BackgroundColor in LinearLayout of the width of the screen

I created a gallery with horizontalscrollview. In the imageview LinearLayout I've put a color background. I want the LinearLayout spans the width of the screen with or without images. How I can do?
<LinearLayout
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/estandar"
android:padding="2dp"
/>
</HorizontalScrollView>
</LinearLayout>
main.xml
LinearLayout linear2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = linear2 .getLayoutParams();
params.height = 100;
params.width = 300;
but my code does not work.
EDIT:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int anchoPantalla = metrics.widthPixels;
if (listaImagenes.length > 0) {
for (String nombreImagen : listaImagenes) {
InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
final Bitmap bitmap = BitmapFactory.decodeStream(is);
final ImageView imageView = new ImageView(getApplicationContext());
alto = bitmap.getHeight();
ancho = bitmap.getWidth();
final float calculo = ancho / (alto / ALTO_IMAGEN);
imageView.setLayoutParams(new ViewGroup.LayoutParams((int)calculo, ALTO_IMAGEN));
imageView.setImageBitmap(bitmap);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundColor(colorResources);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
linear2.addView(imageView);
}
} else {
// PAINT LINE WIDTHSCREEN x 120dp
}
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = galeria2.getLayoutParams();
params.height = 100;
params.width = 300;
galeria2.setLayoutParams(params);
You have to dynamically add width to each element of HorizontalScrollView. I have modified your code have a look. Its working fine.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FF0040"
android:padding="2dp"
/>
</HorizontalScrollView>
Now in code add element like (see the highlited line)
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);
ImageView iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.ic_launcher);
**iv1.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
ImageView iv2 = new ImageView(this);
**iv2.setLayoutParams(new LinearLayout.LayoutParams(
720, LayoutParams.MATCH_PARENT));**
galeria2.addView(iv1);
galeria2.addView(iv2);

Add layout with parameters programmatically Android

Hello I have a main screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="55dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/toggleButton1"
android:background="#android:drawable/btn_default"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/plus_grey" />
<LinearLayout
android:id="#+id/lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="0dp"
android:background="#A0FFFFFF"
android:orientation="horizontal"
android:visibility="invisible" >
<TextView
android:id="#+id/locinfo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textColor="#color/cherniy"
android:textSize="20sp"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
And there is a imageButton
button = (ImageButton) findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (bIcon == false) {
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
}
else {
button.setImageResource(R.drawable.plus_grey);
m.remove();
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
t.setVisibility(View.INVISIBLE);
linLayout.setVisibility(View.INVISIBLE);
bIcon = false;
}
}
});
I want to programmatically add
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
where w
display = getWindowManager().getDefaultDisplay();
#SuppressWarnings("deprecation")
w = display.getWidth();
But when I do like this
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT);
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;
My application crashes. Can you please tell how to programmatically create with my parameters (tied with the width and height of the screen of the device)? I will take any suggestions. Thank you.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(w*2)/3,
LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
This will throw error because the LinearLayout is the child of Relative layout, so you have to set RelativeLayoutParams for that.
Instead use this
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
(w*2)/3,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(lp);
or
Instead of creating new LayoutParams you can reuse the existing LayoutParams of the view as shown below.
RelativeLayout.LayoutParams lp = linLayout.getLayoutParams();
lp.width = (w*2)/3;
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
linLayout.setLayoutParams(lp);
Instead of making a new LayoutParams, you should modify the existing one.
Change your code to:
button.setImageResource(R.drawable.plus_yellow);
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay);
LinearLayout.LayoutParams lp = linLayout.getLayoutParams();
final int left_margin = whatever;
final int top_margin = 0;
final int right_margin = whatevermore;
final int bottom_margin = 0;
lp.setMargins(left_margin, top_margin, right_margin, bottom_margin);
lp.width = (w*2)/3;
linLayout.setLayoutParams(lp);
t = (TextView)findViewById(R.id.locinfo);
linLayout.setVisibility(View.VISIBLE);
t.setVisibility(View.VISIBLE);
bIcon = true;

Categories

Resources