How to customize xamarin device idiom

Anas Guibene 511 Reputation points

Hello ,

I want to customize device idiom so that the application considers 7-inch devices as a phone, not a tablet .

any solutions ?

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

Accepted answer
  1. Anas Guibene 511 Reputation points

    Hello @JarvanZhang

    I just found the solution
    in MainActivity.cs

    switch (Device.Idiom)  
                    case TargetIdiom.Tablet:  
                        var widhtinch = Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Xdpi;  
                        var heightinch = Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Ydpi;  
                        var inch = Math.Sqrt(widhtinch * widhtinch + heightinch * heightinch);  
                        if(inch <= 7.6)   
                            RequestedOrientation = ScreenOrientation.Portrait;  
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JarvanZhang 23,926 Reputation points


    Welcome to our Microsoft Q&A platform!

    TargetIdiom Enum indicates the Phone and Tablet by the dips of the device's width. To check the inches of the running device, it needs to caculate the value in codehind. The way to get the inch of a device is different on each platform, try to get the value on each platform and then execute the code using DependencyService.

    You could detect the OnSizeAllocated event of the page to perfform the work.

       protected override void OnSizeAllocated(double width, double height)  
           base.OnSizeAllocated(width, height);  
           var screen_inches = DependencyService.Get<ITestInterface>().getScreenSize();  
           //detect the size  

    Best Regards,

    Jarvan Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    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 comments No comments