How to set android:layout_width and android:layout_height programmatically for GridView for Android

soumenhaldersoumenhalder ✭✭Member ✭✭
edited August 2018 in Xamarin.Android

I am using android gridview in Xmarine c# project. Here I want to set android:layout_width and android:layout_height dynamically from cs page(from code) for layout mode/portrait mode.

Here I have used layout_width and layout_height in xaml page, but I want to do it from code.

How to do this?

Here is the code behind module:

int LayoutOrientation = WindowManager.DefaultDisplay.Orientation;
if (LayoutOrientation != 0)
{
try
{
// landscape view
var Lin = FindViewById(Resource.Id.LinLay);
Lin.Orientation = Orientation.Horizontal;
GridView GVMiddle = FindViewById(Resource.Id.GridMiddle);
GVMiddle.NumColumns = 6;
}
catch (Exception ex)
{
string a = ex.Message.ToString();
}
}

Best Answers

Answers

  • NightFuryNightFury ✭✭✭ Member ✭✭✭
    edited August 2018

    Can you try this one

    GridView yourGridview= FindViewById<GridView>(Resource.Id.yourGridview);
    
    LayoutParams paramsss = new LayoutParams(100,100);//height and width are in pixel
    
    yourGridview.LayoutParameters = layoutparams;
    
  • soumenhaldersoumenhalder ✭✭ Member ✭✭
    edited August 2018

    I tried this

    LayoutParams layoutparams = new LayoutParams(100, 100);//height and width are in pixel
    GVMiddle.LayoutParameters = layoutparams;

    but this gives an error in landscape mode, the only error which I get is

    Java.Lang.ClassCastException:

    then app crashed.

  • NightFuryNightFury ✭✭✭ Member ✭✭✭

    @soumenhalder said:
    I tried this

    LayoutParams layoutparams = new LayoutParams(100, 100);//height and width are in pixel
    GVMiddle.LayoutParameters = layoutparams;

    but this gives an error in landscape mode, the only error which I get is

    Java.Lang.ClassCastException:

    then app crashed.

    I think this line is giving error to you

    GridView GVMiddle = FindViewById(Resource.Id.GridMiddle);
    

    So change it to

    GridView GVMiddle = FindViewById<GridView>(Resource.Id.GridMiddle);
    
  • soumenhaldersoumenhalder ✭✭ Member ✭✭
    edited August 2018

    Yes I already used this code

    GridView GVMiddle = FindViewById<GridView>(Resource.Id.GridMiddle);

    still the same error is coming. I am using C# application.

  • NightFuryNightFury ✭✭✭ Member ✭✭✭

    @soumenhalder said:
    Yes I already used this code

    GridView GVMiddle = FindViewById<GridView>(Resource.Id.GridMiddle);

    still the same error is coming. I am using C# application.

    Can you post your exception so i can get clear picture about it.

  • NightFuryNightFury ✭✭✭ Member ✭✭✭
    edited August 2018 Accepted Answer

    I have tried you scenario then i got following exception

    **android app actionbar layoutparams cannot be cast to android widget linearlayout layoutparams**
    

    as per my opinion your trying to modify LayoutParams of child view (which is your GridView) which have parent layout like Linear or Relative or Something else .

    so i just modify it to

            LinearLayout.LayoutParams paramsss = new LinearLayout.LayoutParams(100, 100);//height and width are inpixel
    
    
             gridview.LayoutParameters = paramsss; 
    

    and its working for me.

    Lets give try for it and let me know its working or not.

    These are some links that i followed earlier

    https://stackoverflow.com/questions/19604891/java-lang-classcastexception-android-view-viewgrouplayoutparams-cannot-be-cast

    https://stackoverflow.com/questions/21045198/android-view-viewgrouplayoutparams-cannot-be-cast-to-android-widget-abslistview

  • soumenhaldersoumenhalder ✭✭ Member ✭✭
    Accepted Answer

    @NightFury said:
    I have tried you scenario then i got following exception

    android app actionbar layoutparams cannot be cast to android widget linearlayout layoutparams

    as per my opinion your trying to modify LayoutParams of child view (which is your GridView) which have parent layout like Linear or Relative or Something else .

    so i just modify it to

              LinearLayout.LayoutParams paramsss = new LinearLayout.LayoutParams(100, 100);//height and width are inpixel
          
        
               gridview.LayoutParameters = paramsss; 
    

    and its working for me.

    Lets give try for it and let me know its working or not.

    These are some links that i followed earlier

    https://stackoverflow.com/questions/19604891/java-lang-classcastexception-android-view-viewgrouplayoutparams-cannot-be-cast

    https://stackoverflow.com/questions/21045198/android-view-viewgrouplayoutparams-cannot-be-cast-to-android-widget-abslistview

    Thank you, this is working fine.

  • NightFuryNightFury ✭✭✭ Member ✭✭✭

    @soumenhalder

    Cheers.

    Happy coding..

Sign In or Register to comment.