Implementing Adaptive User Interface Flows

Introduction

 
When we talk about layouts then something with boundaries might come mind. We have already talked about layouts in our previous articles. Depending on the layout that an application is currently showing, the user interface flow might be different.
 

Determine the current layouts

 
When we are viewing something on the screen of the Android device then whether we are viewing on the various screen devices we must understand how it is possible that the same application is on a 4-inch device running the same as it is running on a 7-inch device. Then there are certain classes and objects to understand the layout. Let us create an activity as shown in the code below however this code might just be a reflection of the entire code.
  1. public class NewsReaderActivity extends FragmentActivity {    
  2. boolean mIsDualPane;    
  3.     
  4. @Override    
  5.    public void onCreate(Bundle savedInstanceState) {    
  6.       super.onCreate(savedInstanceState);    
  7.       setContentView(R.layout.main_layout);    
  8.     
  9.       View articleView = findViewById(R.id.article);    
  10.       mIsDualPane = articleView != null &&     
  11.       articleView.getVisibility() == View.VISIBLE;    
  12.    }    
  13. }   
    Note: Notice that this code queries whether or not the "article" pane is available that is much more flexible than hard-coding a query for a specific layout.
     

    Reaction Depending on Current Layout

     
    Some actions might have a different result depending on the current layout. Let us use an example that a browser having a page contains some of the links or anything that is navigational or clickable. Such as when we click it, it goes to the new tab so something like that is related to the layout. Whenever there are two window panes then there is a dual-mode pane and then when one is clicked it shows something on the new tab.
     
    Code
    1. @Override    
    2. public void onHeadlineSelected(int index) {    
    3.    mArtIndex = index;    
    4.    if (mIsDualPane) {    
    5.    /* display article on the right pane */    
    6.       mArticleFragment.displayArticle(mCurrentCat.getArticle(index));    
    7.    } else {    
    8. /* start a separate activity */    
    9.       Intent intent = new Intent(this, ArticleActivity.class);    
    10.       intent.putExtra("catIndex", mCatIndex);    
    11.       intent.putExtra("artIndex", index);    
    12.       startActivity(intent);    
    13.    }    
    14. }   
      Likewise, if the application is in dual-pane mode, it should set up the action bar with tabs for navigation, whereas if the app is in single-pane mode, it should set up navigation with a spinner widget. So our code should also check which case is appropriate as in the following:  
      1. final String CATEGORIES[] = { "Top Stories""Politics""Economy""Technology" };    
      2.     
      3. public void onCreate(Bundle savedInstanceState) {    
      4. ....    
      5.    if (mIsDualPane) {    
      6.    /* use tabs for navigation */    
      7.       actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS);    
      8.       int i;    
      9.       for (i = 0; i < CATEGORIES.length; i++) {    
      10.          actionBar.addTab(actionBar.newTab().setText(    
      11.          CATEGORIES[i]).setTabListener(handler));    
      12.       }    
      13.          actionBar.setSelectedNavigationItem(selTab);    
      14.    }    
      15.       else {    
      16. /* use list navigation (spinner) */    
      17.          actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_LIST);    
      18.          SpinnerAdapter adap = new ArrayAdapter(this,     
      19.          R.layout.headline_item, CATEGORIES);    
      20.          actionBar.setListNavigationCallbacks(adap, handler);    
      21. }    
      22. }   

        Reuse Fragments in Other Activities

         
        Reuse the Fragments on other activities. Likewise, it might be a good consideration in the optimized programming of the recurring pattern in the designing for multiple screens is to have a portion of your interface that's implemented as a pane on some screen configurations and as a separate activity on other configurations. Reusing the fragments in the code reduces redundancies and duplicity as shown below.
         
        Code 
        1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        2.    android:layout_width="fill_parent"    
        3.    android:layout_height="fill_parent"    
        4.    android:orientation="horizontal">    
        5. <fragment android:id="@+id/headlines"    
        6.    android:layout_height="fill_parent"    
        7.    android:name="com.example.android.newsreader.HeadlinesFragment"    
        8.    android:layout_width="400dp"    
        9.    android:layout_marginRight="10dp"/>    
        10. <fragment android:id="@+id/article"    
        11.    android:layout_height="fill_parent"    
        12.    android:name="com.example.android.newsreader.ArticleFragment"    
        13.    android:layout_width="fill_parent" />    
        14. </LinearLayout>   
          Note: As the preceding layout indicates it is necessary to do something with fill_parent as shown and other things are done to the width that must be 400 to 500 dp. 
           

          Handling Screen Configuration Changes

           
          As we have already learned, handing various devices or screens revolves around the layouts. If we are using a separate activity to implement separate parts of your interface then you need to keep in it may be necessary to react to certain configuration changes and result in the interface consistency.
           
          Java code
          1. package com.example.android.newsreader;    
          2.     
          3. import android.content.res.Configuration;    
          4. import android.os.Bundle;    
          5. import android.support.v4.app.FragmentActivity;    
          6.     
          7. public class ArticleActivity extends FragmentActivity {    
          8. // The news category index and the article index for the article we are to display    
          9.    int mCatIndex, mArtIndex;    
          10.     
          11.    @Override    
          12.    protected void onCreate(Bundle savedInstanceState) {    
          13.    super.onCreate(savedInstanceState);       
          14.    mCatIndex = getIntent().getExtras().getInt("catIndex"0);    
          15.    mArtIndex = getIntent().getExtras().getInt("artIndex"0);    
          16.     
          17. // If we are in two-pane layout mode, this activity is no longer necessary    
          18.     if (getResources().getBoolean(R.bool.has_two_panes)) {    
          19.        finish();    
          20.        return;    
          21. }    
          22.     
          23. // Place an ArticleFragment as our content pane    
          24.     ArticleFragment f = new ArticleFragment();    
          25.     getSupportFragmentManager().beginTransaction().add(android.R.id.content, f).commit();    
          26.     
          27. // Display the correct news article on the fragment    
          28.     NewsArticle article = NewsSource.getInstance().getCategory(mCatIndex).getArticle(mArtIndex);    
          29.     f.displayArticle(article);    
          30.    }    
          31. }   

            Summary

             
            This article tells us about the adaptation of user interfaces using various layouts and devices. An application adapts to the various screen resolutions and configuration changes on the device.


            Similar Articles