Test
Test

Reputation: 45

Responsive layout in C# Xamarin Android, how?

I'm making an Android application in Xamarin. I want to make it responsive, so I need to get the screen size value (this worked). Now I need to send this to another activity, but I can't get the value because I first need to open a resource layout.

string width1 = Intent.GetStringExtra("width") ?? "Data not available";
width = width1.ToString();
string height2 = Intent.GetStringExtra("height") ?? "Data not available";
height = height2.ToString();

// Set our view from the "recepten" layout resource
if (Convert.ToInt32(width) <= 350 && Convert.ToInt32(height) <= 420)
{
SetContentView(Resource.Layout.login);
}
else
{
SetContentView(Resource.Layout.loginSmall);
}

I tried this (isn't working), but I can't get another layout when another layout already opened.

Does anyone know any how?

Upvotes: 1

Views: 3648

Answers (1)

Stefan Wanitzek
Stefan Wanitzek

Reputation: 2124

I think you are on the wrong track. You should never need to query the actual screen size for a responsive layout.

You can define different layouts for different screen sizes and android will pick the best one for the device.

To do this create a couple of layout-folders prefexed with "large", "xlarge" etc. as described in the official android documentation:

Resources/layout/my_layout.xml // layout for normal screen size ("default") 
Resources/layout-large/my_layout.xml // layout for large screen size    
Resources/layout-xlarge/my_layout.xml // layout for extra-large screen size
Resources/layout-xlarge-land/my_layout.xml // layout for extra-large in landscape orientation

A call to SetContentView contains the neccesary logic to pick the right layout for the screen size of the device.

Read here more information about supporting multiple screen sizes.

Upvotes: 6

Related Questions