0

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 ?

Abhinav Arora
  • 537
  • 1
  • 7
  • 23
  • you can use monitor.bat under `sdk\tools` to check whether the view's hierarchy is correct. – zzy Dec 23 '14 at 12:32
  • Can it be because of the difference in the font styles ? – Abhinav Arora Dec 23 '14 at 12:43
  • maybe, you could give a smaller size to see whether it will show or not – zzy Dec 23 '14 at 13:05
  • If I set it too low like 1/12 instead of 5/12 * screen_height it gets displayed. Also, I have checked by setting the font style through java but still the same result - working on phone but not on emulator – Abhinav Arora Dec 23 '14 at 13:11

2 Answers2

0

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.

Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • Sir, but when I am setting the text size of the textView I am using pixels and not using dp at all. textview.setTextSize(**TypedValue.COMPLEX_UNIT_PX**,text_size); – Abhinav Arora Dec 23 '14 at 13:07
  • that's not related to setting it with PX,SP,or DIP . it's because the screen width will be a huge value in Nexus devices while it will be a reasonable value with others, SO, to make your values all reasonable make this conversion. – Muhammed Refaat Dec 23 '14 at 13:14
  • Sir, can you please explain a little further or refer me to a page/topic for this – Abhinav Arora Dec 23 '14 at 13:20
  • kindly have a look at the link attached in the last of my answer – Muhammed Refaat Dec 23 '14 at 13:22
  • Sir I am new to android, maybe that's the reason I am unable to understand you. But, I already know that dpi affects the pixel size. I am unable to figure out that - Suppose there is any screen with 12 pixels in the height. Now, the height of my textview will be (5/12)*12 = 5 pixels. And now I am setting the textsize as 5 pixels which should be completely fine. Thanks for your efforts. – Abhinav Arora Dec 23 '14 at 13:30
  • let's suppose there are two screens, each one is 7'inches , the first one is 160 dpi and the second is 320 dpi, So, if the first screen height is 12, the second will be 24, and although they have the same size (7'inch) your Text will be doubled in size in the other one – Muhammed Refaat Dec 23 '14 at 13:37
  • But my text size will be doubled in pixels. That means for the 320dpi I will have 5*24/12 = 10 pixels and for 160dpi I will have 5 pixels. But 10 pixels in 320dpi and 5 pixels in 160dpi will occupy same physical height. And that's all I want – Abhinav Arora Dec 23 '14 at 13:41
  • conversion between 320dpi and 160dpi doesn't happen by just dividing by 2, as the physical size of inches will be constant, and that will affect the whole size driven, that's why we have to use the formula I attached in my answer, and you don't even told me if you tried that and it worked with you or not – Muhammed Refaat Dec 23 '14 at 13:55
  • Sir, now its not even working on my phone now. I did int new_screen_height = (int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screen_height, res.getDisplayMetrics())); final int text_size = (int) (((new_screen_height*5.0)/12.0)); textview.setTextSize(TypedValue.COMPLEX_UNIT_PX,text_size); – Abhinav Arora Dec 23 '14 at 14:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67551/discussion-between-abhinav-and-muhammed-refaat). – Abhinav Arora Dec 23 '14 at 14:06
0

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

Abhinav Arora
  • 537
  • 1
  • 7
  • 23