Problem with Android tabHost and tabWidget

I am working on an Android application that uses bookmarked icons downloaded from the Internet and the icon size is 30x30.

for(int i = 0; i < titleNames.size(); i++) { intent = new Intent().setClass(context, Gallery.class); sp = tabHost.newTabSpec(titleNames.get(i)).setIndicator(titleNames.get(i), res.getDrawable(R.drawable.icon)).setContent(intent); tabHost.addTab(sp); } 

If I use this code above (icon from resources) to set the indicator text and icon, it works well enough and the icon is suitable for the tab widget.

 for(int i = 0; i < titleNames.size(); i++) { intent = new Intent().setClass(context, Gallery.class); sp = tabHost.newTabSpec(titleNames.get(i)).setIndicator(titleNames.get(i), Drawable.createFromPath(path+iconNames.get(i))).setContent(intent); tabHost.addTab(sp); } 

But if I use this code (an image downloaded from the Internet and its internal memory) instead of the previous one, the icons seem so small, and even the height and width values ​​are the same for both icons. I do not scale the icons when downloading them from the Internet, and I save them as PNG. Does anyone know what the problem is?

Here is a tab with icons from resources

Here is a taboo with icons downloaded from the Internet

DECISION:

Instead of adding objects to the tab node with my previous code, now I use the code below and it works quite well. The difference between the two new ones is the use of a layout that has an image view and a text view to indicate the icon and text below to set an indicator of intent. Thus, I can call a method from the image view so that the image matches its borders, which is defined in the XML file. Here's how to do it with View.

  private void addTab(String labelId, Drawable drawable, Class<?> c) { tabHost = getTabHost(); intent = new Intent(this, c); spec = tabHost.newTabSpec("tab" + labelId); View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); TextView title = (TextView) tabIndicator.findViewById(R.id.title); title.setText(labelId); ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); icon.setImageDrawable(drawable); icon.setScaleType(ImageView.ScaleType.FIT_CENTER); spec.setIndicator(tabIndicator); spec.setContent(intent); tabHost.addTab(spec); } 

And here is the layout with the image and type of text.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:orientation="vertical" android:padding="5dp" android:weightSum="55" > <ImageView android:id="@+id/icon" android:layout_width="match_parent" android:layout_height="0dp" android:src="@drawable/icon" android:adjustViewBounds="false" android:layout_weight="30" /> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="25" android:gravity="center_horizontal" /> </LinearLayout> 

Thanks to @Venky and @SpK for giving me the idea.

+6
source share
2 answers

Instead of adding objects to the tab node with my previous code, now I use the code below and it works quite well. The difference between the two new ones is the use of a layout that has an image view and a text view to indicate the icon and text below to set an indicator of intent. Thus, I can call a method from the image view so that the image matches its borders, which is defined in the XML file. Here's how to do it with View.

 private void addTab(String labelId, Drawable drawable, Class<?> c) { tabHost = getTabHost(); intent = new Intent(this, c); spec = tabHost.newTabSpec("tab" + labelId); View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); TextView title = (TextView) tabIndicator.findViewById(R.id.title); title.setText(labelId); ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); icon.setImageDrawable(drawable); icon.setScaleType(ImageView.ScaleType.FIT_CENTER); spec.setIndicator(tabIndicator); spec.setContent(intent); tabHost.addTab(spec); } 

And here is the layout with the image and type of text.

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dip" android:layout_height="55dip" android:layout_weight="1" android:orientation="vertical" android:padding="5dp" android:weightSum="55" > <ImageView android:id="@+id/icon" android:layout_width="match_parent" android:layout_height="0dp" android:src="@drawable/icon" android:adjustViewBounds="false" android:layout_weight="30" /> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="25" android:gravity="center_horizontal" /> </LinearLayout> 

Thanks to @Venky and @SpK for giving me the idea.

+11
source

Tab icon sizes for high density screens (hdpi): Full asset: 48 x 48 px Icon: 42 x 42 px

Tab icon sizes for medium density screens (mdpi): Full asset: 32 x 32 px Icon: 28 x 28 px

Tab icon sizes for low density screens (ldpi): Full asset: 24 x 24 px Icon: 22 x 22 px

you can see this: http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html

+3
source

Source: https://habr.com/ru/post/915964/


All Articles