Programming for Android with multiple screens

4

When I program to Android , I think of portability with the most diverse devices on the market. Seeing the layout of my Activity principal , I noticed that on small-screen devices (I did the 2-inch screen test) the views are HUGE and on large-screen devices (I did the 10-inch screen test) views are MINIATURES.

How do I fix all this? When I wrote code HTML , I used in CSS the% drive to solve the problem.

    
asked by anonymous 06.06.2015 / 01:49

2 answers

1

Hello,

You need to be careful with screen sizes right from the start of development, because backing up in this direction is even more laborious.

The correct way to handle this is by using multiple resources for each screen configuration.

Notice what the documentation says about it:

  

Using configuration qualifiers

     

Android supports several configuration qualifiers that allow you to   control how the system selects your alternative resources based on the   characteristics of the current device screen. A configuration   qualifier is the string that you can append to a resource directory in   your Android project and specifies the configuration for which the   resources inside are designed.

     

To use a configuration qualifier:

     

Create a new directory in your project / directory and name it   using the format: - is the   standard resource name (such as drawable or layout). is a   configuration qualifier from table 1, below, specifying the screen   configuration for which these resources are to be used (such as hdpi   or xlarge). You can use more than one at a time-simply   separate each qualifier with a dash. Save the appropriate   configuration-specific resources in this new directory. The resource   files must be named exactly the same as the default resource files.   For example, xlarge is a configuration qualifier for extra-large   screens. When you append this string to a resource directory name   (such as layout-xlarge), it indicates to the system that these   resources are to be used on devices that have an extra-large screen.

That is, you will have to have within your project folders within resources specific to each screen configuration following the <resources_name>-<qualifier> pattern.

Something similar to this:

Formoreinformationaccessthelinkbelow.Itcontainsallthenecessaryinformationonthissubject.

link

I hope I have helped.

Hug.

    
04.08.2015 / 22:16
1

Gabriel

We have some ways to do this manipulation of layouts on different devices.

Drawable

Provides the following sizes

  • ldpi (low) ~ 120dpi
  • mdpi (medium) ~ 160dpi
  • hdpi (high) ~ 240dpi
  • xhdpi (extra-high) ~ 320dpi
  • xxhdpi (extra-extra-high) ~ 480dpi
  • xxxhdpi (extra-extra-extra-high) ~ 640dpi

With this, each file within these folders will treat a different screen size.

Dimens.xml

This xml that should be in res/values/dimens.xml allows us to make customizations such as:

  • Size of views
  • Font size
  • Spaces between margins (see example xml below)
  • Among others.

     <resources>
    
        <!-- Default screen margins, per the Android Design guidelines. -->
        <dimen name="activity_horizontal_margin">16dp</dimen>
        <dimen name="activity_vertical_margin">16dp</dimen>
    
     </resources>
    

Note: Briefly SP for typography and DP for everything else.

Fragments

We have since 3.0 HoneyComb the use of Fragments for creation and dynamic layouts, thinking about the different screen sizes that currently exist (Tablets and cell phones with different screens).

Note : You can use the support library for lower versions, careful when import should be:

android.support.v4.app.Fragment

Theaboveimagedemonstratesthefragmentsinaction,forexampleintabletsthescreencancompletelydisplayitslayout,whenyouclickonanyiteminthelist,itwillbeloadednexttoitwithoutchangingtheiterationofthescreens,inthefigureonthesiderightwehavetheuseoffragmentsondeviceswithsmallerscreenswherewehaveFragmentAthatwhenclickeddirectstofragmentB,Corhowmanyareneededtomountyourscreen.

Exampletoinflateyourfragmenttakenfrom documentation

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

In order to inflate the Fragments we need to override the onCreateView method and return the inflated view, but nothing prevents us from using the OnCreate method. Just keep an eye on the Fragment lifecycle.

Fragments Life Cycle

onAttach (activity) - This method is called soon after the fragment is associated with the activity

onCreate (bundle) - This method is called only once and when the fragment is being created. It will receive the Bundle that was saved during the onSaveInstanceState (state) method

onCreateView (inflater, viewgroup, bundle) - In this method, the fragment needs to create the view that will be inserted into the activity > layout

onActivityCreated (bundle) - This method is called shortly after the activity's onCreate () has been terminated.

onDestroyView () - This method is called when the fragment view has been removed and no longer belongs to the fragment.

onDestroy () - Called to indicate that the fragment is no longer being used.

OnDetach () - Opposing the onAttach (activity) method.

    
05.08.2015 / 01:48