Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 824
Сергей Светлов Avatar asked Mar 19 '13 14:03

Сергей Светлов


People also ask

What is layout params?

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports. The base LayoutParams class just describes how big the view wants to be for both width and height.

How do I set LayoutParams to Imageview in android programmatically?

LayoutParams layoutParams = new LinearLayout. LayoutParams(30, 30); yourImageView. setLayoutParams(layoutParams); This is because it's the parent of the View that needs to know what size to allocate to the View.

What does Wrap_content mean in Android?

Wrap Content: Wrap content takes the length of the text and wraps its content. Example: “This is a Wrap Text”. The wrap content starts wrapping from “This” and ends at “text”.

What is Match_parent?

match_parent and fill_parent are same property, used to define width or height of a view in full screen horizontally or vertically. These properties are used in android xml files like this. android:layout_width="match_parent" android:layout_height="fill_parent"


1 Answers

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);
like image 173
Triode Avatar answered Oct 21 '22 10:10

Triode