How to customize xamarin device idiom

Anas Guibene 491 Reputation points
2021-07-05T10:52:18.86+00:00

Hello ,

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

any solutions ?

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

Accepted answer
  1. Anas Guibene 491 Reputation points
    2021-07-06T09:19:13.877+00:00

    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)   
                        {     
                             
                            Device.SetTargetIdiom(TargetIdiom.Phone);  
                            RequestedOrientation = ScreenOrientation.Portrait;  
                              
                        }  
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. JarvanZhang 23,961 Reputation points
    2021-07-06T03:02:34.297+00:00

    Hello,​

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.