user782104 user782104 - 6 months ago 99
Android Question

FragmentTabHost return null when I get the tag of the tab

Here is how I create the tabhost, what I would like to do is get the tab when the user click on the tab. The problem is , it return null instead of the tag value.

tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.meun_home)),
HomeFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("form").setIndicator("",
getResources().getDrawable(R.drawable.meun_register)),
FormFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("calculator").setIndicator("",
getResources().getDrawable(R.drawable.meun_calculator)),
CalculatorFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("news").setIndicator("",
getResources().getDrawable(R.drawable.meun_call)),
HomeFragment.class, null);

tabHost.getTabWidget().setDividerDrawable(
getResources().getDrawable(R.drawable.border));

for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.transparent));
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT, 1f);
tabHost.getTabWidget().getChildAt(i).setLayoutParams(params);
tabHost.getTabWidget().getChildAt(i).setPadding(0, 0, 0, 0);
tabHost.getTabWidget().getChildAt(i).setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_UP) {
Log.d("test1",""+v.getTag());
return true; // doesnt allow tab change
}
return false;
}
});
}


You can find the line
Log.d("test1",""+v.getTag());
which return null. Which expected is return some string like "home" , "form", etc..... Thanks a lot

Answer

because you do not set any tag to your View of your newTabSpec. you set drawable icon but you do not set any Tag. So you can do it in two ways:

1) first create an imageView from your drawable icon:

imageView.setImageBitmap(bitmap);
imageView.setImageResource(R.drawable.my_image);
imageView.setImageDrawable(drawable);

and then set Tag to your imageView and use setIndicator(View view) to add it to your newTabSpec.

2) or after you add your drawable you can use getChildTabViewAt() to get the View and then set the Tag for it.

in summary you have not set any tag so it is obviously null.

the doc says:

TabHost.TabSpec:

A tab has a tab indicator, content, and a tag that is used to keep track of it. This builder helps choose among these options. For the tab indicator, your choices are: 1) set a label 2) set a label and an icon For the tab content,

so that string is just a label, the tag and the label is two different things, tag is memory of a view that you can store any object in to it that object can be a string or anything else like ViewHolder design pattern that used in creating listview.