Change listview layout_below in code

Shay Wilner 1,726 Reputation points
2023-03-14T21:54:54.94+00:00

Hello

It's xamarin android platform

i have a listview

<ListView
          android:layout_width="match_parent" 
          android:id="@+id/dgvresult"
          android:layout_below="@id/title1"
          android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        android:layout_marginTop="4dp
       /> 

in certain condition i need to change android:layout_below instead of android:layout_below="@id/title1" change to android:layout_below="@id/title2"

Thank

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2023-03-15T06:56:37.5233333+00:00

    Hello,

    condition i need to change android:layout_below instead of android:layout_below="@id/title1" change to android:layout_below="@id/title2"

    Do you want to change it at runtime in the background code?

    If so, you can use  relativeLayoutLayoutParams.AddRule(LayoutRules.Below, Resource.Id.title2); to set it.

    RelativeLayout.LayoutParams relativeLayoutLayoutParams = new RelativeLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.WrapContent);
    
    relativeLayoutLayoutParams.AddRule(LayoutRules.Below, Resource.Id.title2);
    relativeLayoutLayoutParams.SetMargins(0, 4, 0, 0);
    listView.LayoutParameters = relativeLayoutLayoutParams;
    

    Please Note:If you have some controls below or above the listView, you need to AddRule for all of them.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful