4

Since starting android development I have have been using the following drawable folder structure...

drawable-mdpi drawable-hdpi drawable-xhdpi drawable-xxhdpi

now my problem is this, Im testing on 2 different devices

Lenovo Tab S8 - 1980*1200 - xxhdpi
Nexus 5 - 1080*1920 - xxhdpi

The lenovo tabs physical screen-size is about twice that of the Nexus, but they both pull their images from drawable-xxhdpi.

This means that if I initially build my app for the nexus, then when deployed on the Tab it will be surrounded by lots of blank unused space. But If I develop to fit nicely on the Tab it appears completely blown up on the Nexus.

enter image description here enter image description here

I have read here

(http://www.techotopia.com/index.php/Handling_Different_Android_Devices_and_Displays)

to add a drawable folder with the name

drawable-sw200dp
drawable-sw600dp
drawable-sw800dp

to handle different screen sizes but this just crashes my app on the Nexus. Tab is fine.

Can anybody explain why this is and/or a workaround to handle different sizes within the xxhdpi category?

UPDATE:- XML layout that appears to be causing the proble (fragment_tutorial2.xml)...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
                android:paddingTop="20dip">
    <TextView
        android:id="@+id/hint"
        style="@style/TutorialTitle1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/tut_text2" />

    <TextView
        android:id="@+id/hint2"
        style="@style/TutorialTitle2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/hint"
        android:text="@string/tut_text3"/>

    <ImageView
        android:id="@+id/documentlist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/documentlist" />

</RelativeLayout>

Styles....

<style name="TutorialTitle1" parent="AppBaseTheme">
        <item name="android:textStyle">bold</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">50sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingTop">5dp</item>
    </style>

    <style name="TutorialTitle2" parent="AppBaseTheme">
        <item name="android:textStyle">bold</item>
        <item name="android:gravity">center</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingBottom">25dp</item>
    </style>

Logcat when publishing to Nexus with a folder called drawable-sw200dp ...

2243-2243/uk.co.danbd.heswresources E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: uk.co.danbd.heswresources, PID: 2243
    android.view.InflateException: Binary XML file line #21: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:633)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at uk.co.danbd.heswresources.TutorialFragment.onCreateView(TutorialFragment.java:105)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
            at android.view.View.measure(View.java:17547)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
            at android.view.LayoutInflater.createView(LayoutInflater.java:607)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at uk.co.danbd.heswresources.TutorialFragment.onCreateView(TutorialFragment.java:105)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
            at android.view.View.measure(View.java:17547)
            at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
            at android.view.View.measure(View.java:17547)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.OutOfMemoryError: Failed to allocate a 7607

TutorialFrament.java (within onCreateView() ) ....

switch (page) {
            case 0 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial1, container, false);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView1),R.anim.fadescalein,1000);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView2),R.anim.fadescalein,2000);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView3), R.anim.fadescalein, 2100);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView4), R.anim.fadescalein, 2200);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView5), R.anim.fadescalein, 2300);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView6), R.anim.fadescalein, 2400);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView7), R.anim.fadescalein, 2500);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView8), R.anim.fadescalein, 2600);
                animateImageView((ImageView) rootView.findViewById(R.id.imageView9), R.anim.fadescalein, 2700);
                break;
            case 1 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial2, container, false); // line 105
                break;
            case 2 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial3, container, false);
                break;
            case 3 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial4, container, false);
                break;
            case 4 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial5, container, false);
                break;
            case 5 :
                rootView = (ViewGroup) inflater.inflate(R.layout.fragment_tutorial6, container, false);
                listView = (ListView) rootView.findViewById(R.id.list);
                if (prefs.getString("type", "Acute") == "Acute") {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.tutorial_layout_row, valuesAcute);
                    listView.setAdapter(adapter);
                } else {
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.tutorial_layout_row, valuesMentalHealth);
                    listView.setAdapter(adapter);
                }
                listView.setOnItemClickListener(listPairedClickItem);
                break;
        }
        return rootView;

fragment_tutorial1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
                android:paddingTop="20dip" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/logo_margin_top"
        android:src="@drawable/doctorslogo" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ecg" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView2"
        android:layout_alignTop="@id/imageView2"
        android:src="@drawable/handwash" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/imageView2"
        android:layout_alignRight="@id/imageView2"
        android:src="@drawable/chart" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/imageView2"
        android:layout_alignTop="@id/imageView2"
        android:layout_marginTop="-40dp"
        android:src="@drawable/cross" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView5"
        android:layout_toLeftOf="@id/imageView2"
        android:src="@drawable/steph" />    

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/imageView2"
        android:layout_alignBottom="@id/imageView2"
        android:layout_marginBottom="-49dp"
        android:src="@drawable/hygieia" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView2"
        android:layout_alignLeft="@id/imageView2"
        android:src="@drawable/hospital" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView2"
        android:layout_below="@id/imageView2"
        android:src="@drawable/droplet" />

</RelativeLayout>
AndrewBramwell
  • 494
  • 1
  • 7
  • 26
  • as you can see, both devices proportions are not equal so seems your layouts are working for different sizes but are not well defined.... please share your xml layouts definition to see what's happening... – Jordi Castilla Aug 25 '15 at 08:26
  • 2
    Read through https://developer.android.com/guide/practices/screens_support.html entierly please. There are a LOT more resource qualifiers than "xxhdpi" like you said: "sw620dp" etc. If there is a crash, post the logcat here. Also post your XML. – shkschneider Aug 25 '15 at 08:26
  • thanks for the responses, i will have a read @shkschneider also i will try your suggestion user123456 – AndrewBramwell Aug 25 '15 at 08:45
  • `InflateException: Binary XML file line #21: Error inflating class `? Please post also `fragment_tutorial1.xml` – shkschneider Aug 25 '15 at 08:47

3 Answers3

2

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 7607

is the reason, it seems that your bitmaps too large.

Viktor Yakunin
  • 2,927
  • 3
  • 24
  • 39
2

@AndrewBramwell

Whats Your logcat throws

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 7607

http://developer.android.com/reference/java/lang/OutOfMemoryError.html

Too big for Android and it goes out of memory.So,best to resize the images into smaller size.

And add this in your ImageView

android:adjustViewBounds="true"

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

Must be a boolean value, either "true" or "false".

You may set android:largeHeap="true" in your manifest Application Tag .

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Is this resizing the image to fit in the image view? or resizing the Image view to correctly wrap the image. To my understanding the latter should already be happening as layout_width and height are set to wrap_content – AndrewBramwell Aug 25 '15 at 09:26
  • @AndrewBramwell Did you set Scale Type property (imageview)?? – IntelliJ Amiya Aug 25 '15 at 09:27
  • 1
    Im accepting your answer is it gave me the most help!, scale type helped loads with a small change to position my images below the last element. (instead of centering vertically) – AndrewBramwell Aug 25 '15 at 10:43
1

Supporting different device screens is a challenge for Android developers until you know what to do.

To support different devices, you can do the following (drawables):

In a normal project, you have the following folders for drawables - remember that this is regardless of what devices you want to support in your application:

  • drawable/
  • drawable-mdpi/
  • drawable-hdpi/
  • drawable-xhdpi/
  • drawable-xxhdpi/
  • drawable-xxxhdpi/

Once you have generated the appropriately sized images for the above folders, your app, android will automatically load the correct resources for the corresponding device (e.g Nexus 4, 5 inch, 7 inch, 10 inch).

Provide Support For Different Screen Sizes (Layouts)

Using your development device for instance (could be a Nexus 4), you can then create layout folders for other screen sizes you want to support. For instance, I would create the following folders:

  • layout-sw300dp/
  • layout-sw400dp/
  • layout-sw600dp/
  • layout-sw800dp/

In the above layout folders, you can then style your xml files/views respectively so the users who have different screens will still see your app as you want them to view.

Again, the android system will load the correct resources.

I hope this helps and let me know how it goes! Good luck!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • 1
    Really appreciate the response but im unsure how this will help im not wanting to change the layout at all. But if you look at my screenshots above if I make the documentlist image any bigger within the xxhdpi folder it will completely cover the screen of the nexus leaving no room for the titles. It basically needs the image from the xhdpi folder instead. I'm unaware of a way to change the image size within a layout folder / dont think its possible. – AndrewBramwell Aug 25 '15 at 09:01