Fill in the Android tab with a custom pattern and / or color

Hello everyone i created google exmaple tab

HelloTabWidget.java:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                  res.getDrawable(R.drawable.ic_tab_artists))
              .setContent(intent);
tabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                  res.getDrawable(R.drawable.ic_tab_albums))
              .setContent(intent);
tabHost.addTab(spec);

intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                  res.getDrawable(R.drawable.ic_tab_songs))
              .setContent(intent);
tabHost.addTab(spec);

tabHost.setCurrentTab(2);

}

I know how to create my own icon to use instead of my gray and white microphone, but I cannot figure out how to make my custom icon fill the entire tab. When a tab is selected, it is light gray with my pattern in the middle and when it is not dark gray with my pattern in the middle. I prefer that my own pic fill the entire tab and for the more knowledgeable; my own choice of color to fill the tab.

+3
source share
1 answer

setIndicator(View view) .

...

ImageView imgView = new ImageView(this);

// Use imgView.setImageDrawable(Drawable drawable) or
// imgView.setImageBitmap(Bitmap bitmap) to load
// the image you want into imgView

spec = tabHost.newTabSpec("artists").setIndicator(imgView).setContent(intent);

, , , () View , ImageView .

EDIT:

...

    BitmapDrawable bmd = new BitmapDrawable();
    bmd = (BitmapDrawable) res.getDrawable(R.drawable.ic_tab_artists_grey);
    imgView.setImageDrawable(bmd);
+3

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


All Articles