Adaptive User Interface Windows 10

With the launch of Windows 10, Microsoft changed the whole paradigm of programming. Microsoft took us to era of Universal Applications, which means that our same code will run on all the devices. This is being highly appreciated among developers but at the same time, it’s raising question that how will the same interface work on all devices and different sizes. This article answers the same issue. We’ll be looking on how we can design interface for universal applications, which runs on all devices. The prerequisite for this tutorial are  Windows 10, either Technical preview or updated, and Visual Studio 2015.

 

Discussing some theories of Adaptive Interface, there are different approaches to this. We can either tailor completely different interfaces for different devices and screen sizes, or we can change our existing designs according to device and size. We won’t be going in tailoring different interfaces in the blog, as it’s a topic in its own, but I’ll be covering all the methods through which we can change our existing interface and make it adapt to our Screen sizes.

 

Let’s start by making our Universal Application:

Now, let’s just create a Basic interface, with three rectangles box of different colors. XAML of the interface can be found below:

XAML:
<Grid Background="Black"       
        <Rectangle x:Name="rectangle" Fill="#FF6E6ED8"
HorizontalAlignment="Left" Height="295" Margin="7,165,0,0"
Stroke="Black" VerticalAlignment="Top" Width="306"
d:LayoutOverrides="VerticalAlignment"/> 
        <Rectangle x:Name="rectangle1" Fill="#FF20F09B"
HorizontalAlignment="Left" Height="295" Margin="397,165,0,0"
Stroke="Black" VerticalAlignment="Top" Width="306"
d:LayoutOverrides="HorizontalAlignment"/> 
        <Rectangle x:Name="rectangle2" Fill="#FFD89E6E"
HorizontalAlignment="Left" Height="295" Margin="819,165,0,0"
Stroke="Black" VerticalAlignment="Top" Width="306"
d:LayoutOverrides="HorizontalAlignment"/> 
 
    </Grid>

We will change the position of these boxes on the basis of device screen and size of the application.

This can be done by two techniques, either using visual states or using state triggers. We’ll be going through both. The initial steps is same for both of techniques, only the final and the last step is different.

So let’s open our project in blend:

Now, let’s create different Visual states for different screen sizes. If you are wondering  what exactly is visual state, well, Visual states are a technique whereby changes to the state of a control will change the UI, by loading XAML control templates for the current state. (MSDN)

To create a new visual state Group:

Now, let’s create different states by changing the position and size of our rectangle. Every state will represent an interface for different device and screen size. For this tutorial sake’s, I’ll be just making three states. New States can be added in group by:

What we have to do In every Visual state is, just re-manage or you can say scale our interface according to the given size and our needs. In this way, we’ll be re-creating or scaling our single interface for our every Visual State.

I’m scaling my interface as:

 

Large:

Medium:

Small:

Now save our project (ctrl+s), and let’s move back to visual studio. Visual studio might prompt you and notify that changes outside project has been made and whether you wish to load new changes or not. Click on “yes to all” and that’s it! Your new XAML should look like this:

Code:

XAML
<Grid Background="Black"        <VisualStateManager.VisualStateGroups            <VisualStateGroup x:Name="VisualStateGroup"                <VisualState x:Name="small"                    <VisualState.Setters                        <Setter Target="rectangle.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,10,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle.(FrameworkElement.Width)"
 Value="194"/> 
                        <Setter Target="rectangle.(FrameworkElement.Height)"
Value="174"/> 
                        <Setter Target="rectangle1.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,236,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle1.(FrameworkElement.Width)"
Value="194"/> 
                        <Setter Target="rectangle1.(FrameworkElement.Height)"
Value="176"/> 
                        <Setter Target="rectangle2.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,475,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle2.(FrameworkElement.Width)"
Value="204"/> 
                        <Setter Target="rectangle2.(FrameworkElement.Height)"
Value="195"/> 
                    </VisualState.Setters> 
                </VisualState> 
                <VisualState x:Name="Large"/> 
                <VisualState x:Name="Medium"                    <VisualState.Setters                        <Setter Target="rectangle.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,10,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle1.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>391,10,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle2.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,365,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                    </VisualState.Setters> 
                </VisualState> 
            </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="rectangle" Fill="#FF6E6ED8" HorizontalAlignment="Left"
 Height="295" Margin="7,165,0,0" Stroke="Black" VerticalAlignment="Top"
Width="306" d:LayoutOverrides="VerticalAlignment"/> 
        <Rectangle x:Name="rectangle1" Fill="#FF20F09B"
HorizontalAlignment="Left" Height="295" Margin="397,165,0,0" Stroke="Black"
VerticalAlignment="Top" Width="306" d:LayoutOverrides="HorizontalAlignment"/> 
        <Rectangle x:Name="rectangle2" Fill="#FFD89E6E" H
orizontalAlignment="Left" Height="295" Margin="819,165,0,0" Stroke="Black"
VerticalAlignment="Top" Width="306" d:LayoutOverrides="HorizontalAlignment"/> 
 
    </Grid>

 

Now, comes the decision of whether you want to move with Visual State Manager for adaptive code or State triggers. It’s to be noted here that Visual State manager is c# based and State triggers are XAML based. More-over, we also have to decide the property and the values for which new state is triggered.

 

Let’s see visual state manager first. The property we’ll be using to change state will be the width of screen size. The widths at which we’ll be changing our states will be 600,900 and 1200. Add the following code in your file:

Code:

C#
public MainPage() 
        { 
            this.InitializeComponent(); 
             
            this.SizeChanged += MainPage_SizeChanged; 
        } 
 
        private void MainPage_SizeChanged 
( object sender, SizeChangedEventArgs e ) 
        { 
            string state = "Large"; 
            if ( e.NewSize.Width < 600 ) 
                state = "small"; 
            else if ( e.NewSize.Width < 900 ) 
                state = "Medium"; 
            else if ( e.NewSize.Width < 1200 ) 
                state = "Large"; 
            VisualStateManager.GoToState ( this, state, true ); 
 
 
 
        } 

 

 

 

 

What’s happening over here is that whenever user changes the screen size of his application, the new size is checked. And the relevant visual state is activated.  And that’s it. That’s how we make adaptive screen for different application size. To use visual state manager for different devices, use the following code:

Code:

C#
public MainPage() 
        { 
            this.InitializeComponent(); 
            this.Loaded += MainPage_Loaded; 
            this.SizeChanged += MainPage_SizeChanged; 
        } 
 
        private void MainPage_Loaded ( object sender, RoutedEventArgs e ) 
        { 
            var width = Window.Current.Bounds.Width; 
            string state = "Large"; 
            if ( width < 600 ) 
                state = "small"; 
            else if ( width < 900 ) 
                state = "Medium"; 
            else if ( width < 1200 ) 
                state = "Large"; 
            VisualStateManager.GoToState ( this, state, true ); 
        } 
 
        private void MainPage_SizeChanged ( object sender, SizeChangedEventArgs e ) 
        { 
            string state = "Large"; 
            if ( e.NewSize.Width < 600 ) 
                state = "small"; 
            else if ( e.NewSize.Width < 900 ) 
                state = "Medium"; 
            else if ( e.NewSize.Width < 1200 ) 
                state = "Large"; 
            VisualStateManager.GoToState ( this, state, true ); 
 
 
 
        } 

 

 

In addition to screen size being changed, we are also catering different devices by checking the width of our screen at the time page is loaded.

 

Now coming to state triggers, as I mentioned earlier, it’s all XAML based. All we have to do is notify our visual states, that when should they be triggered. This can be done by adding VisualState.StateTriggers in every VisualState. Our new XAML code for states will look like:

XAML
<Grid Background="Black"        <VisualStateManager.VisualStateGroups            <VisualStateGroup x:Name="VisualStateGroup"                <VisualState x:Name="small"                    <VisualState.StateTriggers                        <AdaptiveTrigger MinWindowWidth="599"/> 
 
                    </VisualState.StateTriggers> 
                    <VisualState.Setters                        <Setter Target="rectangle.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,10,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle.(FrameworkElement.Width)"
Value="194"/> 
                        <Setter Target="rectangle.(FrameworkElement.Height)"
Value="174"/> 
                        <Setter Target="rectangle1.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,236,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle1.(FrameworkElement.Width)"
Value="194"/> 
                        <Setter Target="rectangle1.(FrameworkElement.Height)"
Value="176"/> 
                        <Setter Target="rectangle2.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,475,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle2.(FrameworkElement.Width)"
Value="204"/> 
                        <Setter Target="rectangle2.(FrameworkElement.Height)"
Value="195"/> 
                    </VisualState.Setters> 
                </VisualState> 
                <VisualState x:Name="Large"                    <VisualState.StateTriggers                        <AdaptiveTrigger MinWindowWidth="1199"/> 
 
                    </VisualState.StateTriggers> 
                </VisualState> 
                <VisualState x:Name="Medium"                    <VisualState.StateTriggers                        <AdaptiveTrigger MinWindowWidth="899"/> 
 
                    </VisualState.StateTriggers> 
                    <VisualState.Setters                        <Setter Target="rectangle.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,10,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle1.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>391,10,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                        <Setter Target="rectangle2.(FrameworkElement.Margin)"                            <Setter.Value                                <Thickness>10,365,0,0</Thickness> 
                            </Setter.Value> 
                        </Setter> 
                    </VisualState.Setters> 
                </VisualState> 
            </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="rectangle" Fill="#FF6E6ED8" HorizontalAlignment="Left"
 Height="295" Margin="7,165,0,0" Stroke="Black" VerticalAlignment="Top"
Width="306" d:LayoutOverrides="VerticalAlignment"/> 
        <Rectangle x:Name="rectangle1" Fill="#FF20F09B" HorizontalAlignment="Left"
Height="295" Margin="397,165,0,0" Stroke="Black" VerticalAlignment="Top"
Width="306" d:LayoutOverrides="HorizontalAlignment"/> 
        <Rectangle x:Name="rectangle2" Fill="#FFD89E6E"
HorizontalAlignment="Left" Height="295" Margin="819,165,0,0" Stroke="Black"
VerticalAlignment="Top" Width="306" d:LayoutOverrides="HorizontalAlignment"/> 
 
    </Grid>

 

 

 

Over here, every state has his own trigger, which gets activated on its trigger. The property we are using to activate is MinWindowWidth. The superiority which State Triggers have above visual state managers is that they can be previewed by changing device settings in visual studio at the time of designing. And more over, as it’s XAML based, so we don’t have to code differently for device change and application size change.

 

Demo:

 

You can download the complete code from here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s