Android layout params

There are two different ways to set the width and height of your layout. For one way, you specify the width and height of the layout in xml file using android:width and android: height attribute. Alternatively, you can set the width and height programmatically. In this Android tip, I am going to show you how to achieve the goal using the second way.
In code, to set the width and height of the layout, you need to use the setLayoutParams() method of the object referencing the layout. The method has parameter that is a LayoutParams object. With the LayoutParams object you can specify the width and height of the layout.



To have an example on using LayoutParams to set the width and height of the layout, you create a new Android project. Then copy and paste the following code to the activity_main.xml file. activity_main.xml file displays vertical bar that has six buttons. The layout that we are going to change its width and height programmatically is the top layout.

<LinearLayout
   xmlns:android= "http://schemas.android.com/apk/res/android"
   xmlns:tools= "http://schemas.android.com/tools"
   android:id="@+id/layoutparent"
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:paddingLeft= "@dimen/activity_horizontal_margin"
   android:paddingRight= "@dimen/activity_horizontal_margin"
   android:paddingTop= "@dimen/activity_vertical_margin"
   android:paddingBottom= "@dimen/activity_vertical_margin"
   android:orientation="vertical"
   android:background="#08088A"
>
   <ScrollView
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     >
  
<LinearLayout

     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:orientation= "vertical"
     >
   <Button
     android:id="@+id/button1"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text="Button"
     android:textSize="18sp"


     />
   <Button
     android:id="@+id/button2"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text="Button"
     android:textSize="18sp"


     />
   <Button
     android:id="@+id/button3"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text="Button"
     android:textSize="18sp"


     />
   <Button
   android:id="@+id/button4"
   android:layout_width= "wrap_content"
   android:layout_height= "wrap_content"
   android:text="Button"
   android:textSize="18sp"


   />
   <Button
     android:id="@+id/button5"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text="Button"
     android:textSize="18sp"


     />
   <Button
     android:id="@+id/button6"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:text="Button"
     android:textSize="18sp"


     />
   </LinearLayout>

   </ScrollView>
</LinearLayout>


In the onCreate() method of the MainActivity class, you write the following code to set the width and height of the layout.

LinearLayout layoout= (LinearLayout)findViewById( R.id.layoutparent);
  layout.setLayoutParams (new FrameLayout.LayoutParams (FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT ));

A common error occurs when using LayoutParams is class cast exception. To avoid this problem, you note that the LayoutParams object that you pass to the setLayoutParams() method must be created from the LayoutParams class that is part of the parent of the layout. Otherwise, you get class cast exception. In this example, the parent of the top layout (the layout that its width and height are set programmatically) is FrameLayout. If your layout is placed in a RelativeLayout, the LayoutParams object must be created from the LayoutParams class that is in the RelativeLayout (RelativeLayout.LayoutParams).

Android-layout-params

Posted by: Dara | post date: 02-17-2015 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.