Text visible on the phone but not in Genymotion emulator

724 views Asked by At

I have distributed spaces to the views by allotting them weights in Linear Layout. The height of the textView as can be seen is (Height of the screen*5)/12.

The xml file of the layout is as following-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.greenloop.numbers.Counting_Ten"
android:orientation="vertical"
tools:ignore="MergeRootFrame"
android:background="#FFA500">

<RelativeLayout android:id="@+id/rel_number_fragment"
    android:layout_weight="5"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#800000">

   <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:onClick="onClick"
        android:background="#800080"/>

</RelativeLayout>

<FrameLayout
    android:id="@+id/image_frame"
    android:layout_weight="6"
    android:layout_width="match_parent"
    android:layout_height="0dp">
</FrameLayout>

<fragment android:name="com.example.Navigation_Buttons"
    android:id="@+id/button_fragment"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"/>

 </LinearLayout>

I have set the textsize in the java file as

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screen_height = displaymetrics.heightPixels;

text_size = (int) (((screen_height*5.0)/12.0));
textview.setTextSize(TypedValue.COMPLEX_UNIT_PX,text_size);

Now the problem -: I have run this on my phone and everything was fine. Text size was as big as the text view. But when I run the same thing on GENYMOTION emulator of a 10" tablet and a Nexus 5 the Logcat showed - Font size too large to fit in cache. width, height = 175, 527. I am not able to figure this out. Can it be due to difference in font style ?

2

There are 2 answers

10
Muhammed Refaat On

any android view parameter is a relation between dpi and px of the device not the px only, So, Because of the variance of the dpi value among different devices, the returned values of width and height some times needed to be derived to match this relation, for example Nexus devices usually has dpi of 320, and the other device you test your code it may be it was 160 or 240, So, you have to convert the retrieved width and height to dip so that you can have the same ratio when dealing with different devices, this happen by the following formula:

// getting it regularly like you do
int screen_height = displaymetrics.heightPixels;
// convert the value
Resources res = getResources();
int new_screen_height = (int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screen_height, res.getDisplayMetrics()));
// then do whatever you want with the new_screen_height

for more information refer to this answer which declares that the dpi affects the pixel size.

0
Abhinav Arora On

Finally I solved the problem.

android:layout_height="wrap_content" instead of match_parent

and by adding this ->

textview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

the problem was solved