Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change linear layout top margin programmatically android

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?

like image 639
user2064024 Avatar asked Jun 18 '13 12:06

user2064024


People also ask

How do you set the top margin in Java?

setMargins(left, top, right, bottom); yourbutton. setLayoutParams(params); Depending on what layout you're using you should use RelativeLayout. LayoutParams or LinearLayout.

How do I set margins to recyclerView programmatically?

margin); int marginTopPx = (int) (marginTopDp * getResources(). getDisplayMetrics(). density + 0.5f); layoutParams. setMargins(0, marginTopPx, 0, 0); recyclerView.


2 Answers

   layout = (LinearLayout) findViewById(R.id.layoutbtnlinear_aboutme);    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();    params.setMargins(0, 50, 0, 0);     layout.setLayoutParams(params); 
like image 174
Sagar Maiyad Avatar answered Sep 19 '22 17:09

Sagar Maiyad


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 
like image 38
Atul Bhardwaj Avatar answered Sep 20 '22 17:09

Atul Bhardwaj