Programming for Android with multiple screens

Asked

Viewed 312 times

4

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

How do I fix all this? When I wrote code HTML, used in the CSS the unit % for solving the problem.

  • How are you setting the size of views? Post an example of a layout.

  • 1

    Give a study in Fragments ;) http://blog.caelum.com.br/layouts-simples-com-android-fragments/

  • @Gabriel, any way out?

2 answers

1

Hello,

You need to be careful with screen sizes from the beginning of development, because retroacting in this direction is even more laborious.

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

Note what the documentation says about this:

Using Qualifiers Configuration

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 a 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 the Qualifier Configuration:

Create a new directory in your project’s res/ 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 specific Resources for each screen configuration following the default <resources_name>-<qualifier>.

Something like that:

Eu geralmente uso qualificadores de densidade nos meus projetos

For more information visit the link below. It contains all necessary information about this theme.

http://developer.android.com/guide/practices/screens_support.html

I hope I’ve helped.

Hug.

1

Gabriel

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

Drawable

Offers 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 inside these folders will treat a different screen size.

Dimens.xml

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

  • View size
  • Font size
  • Spaces between margins (see xml example 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 version 3.0 HoneyComb the use of Fragments for the creation and dynamic layouts, thinking of the various screen sizes that exist currently(Tablets and mobile phones with different screens).

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

android.support.v4.app.Fragment

inserir a descrição da imagem aqui

The image above demonstrates the Fragments in action, for example on the tablets the screen can fully display its layout, when you click on any item in the list, will be loaded beside without needing to change the iteration of the screens, in the figure on the right we have the use of Fragments in devices with smaller screens where we have Fragment A which when clicked directs to the Fragment B, C or how many are needed to mount your screen.

Example to inflate your Fragment taken from 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);
    }
}

To inflate the Fragments we need to override the method onCreateView and return the view inflated, but nothing prevents you from using also the method OnCreatejust need to stay on top of Fragment’s life cycle.

Life cycle of Fragments

onAttach(Activity) - This method is called right after Fragment is associated with Activity

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

onCreateView(Inflater, viewgroup,) - In this method, Fragment needs to create the view that will be inserted in the Activity layout>

onActivityCreated(Bundle) - This method is called right after Activity onCreate() has been finalized.

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

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

Ondetach() - Opposite of the onAttach(Activity).

  • The answer quoting Fragments meets the concern of AP, but it also implies that the size of the views should be proportional to the physical dimension of the screen, and this can be obtained independent of the use of Ragments. See the other answer.

  • @Piovezan I agree with you, there are two possibilities, which approach do you find most effective? work with all drawable or go straight to the use of Fragments?

  • First I think the author of the question needs to clarify exactly what he wants. What I think is missing in the answer is that in addition to the drawable it is possible to define several dimensions (view size, spaces between them, fonts, etc.) according to the screen size (in res/values/dimens.xml), dimensions to be defined in dp or sp as appropriate.

  • @Piovezan, I gave a compliment, if it appears and explain a little more I could leave much better the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.