FragmentTabHost and custom tabs - android

I really need to access the images of a tab at runtime for scaling.
Therefore I made a custom tab layout with an imageview and a textview.
But if i want to add the custom tab to my tabhost I a "did you forget to call 'public void setup(localactivitymanager activitygroup)'" exception.
Thx in advance for any solutions ;D
Ps. I cannot use
spec = tabHost.newTabSpec("2").setIndicator(res.getString(R.string.tabname2),
res.getDrawable(R.drawable.tabimage2));
tabHost.addTab(spec, FragmentTwo.class, null);
because i need to scale the image before adding.
Here is my class:
public class MyTabActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabmanager_layout);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
addCustomTab(getApplicationContext(),"Meldungen", getResources().getDrawable(R.drawable.messagewhite), NewsFragment.class, mTabHost);
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#3b6c8d")); //unselected
}
}
private void addCustomTab(Context context,String labelId, Drawable drawable, Class<?> c, FragmentTabHost fth ) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_customtab, null);
ImageView image = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.tabtitle);
image.setImageDrawable(drawable);
text.setText(labelId);
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
fth.addTab(spec);
}

You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.
Replace
TabHost.TabSpec spec = fth.newTabSpec(labelId);
Intent i = new Intent(this,c);
spec.setContent(i);
spec.setIndicator(view);
with
TabHost.TabSpec spec = fth.newTabSpec(labelId);
spec.setIndicator(view);
fth.addTab(spec, c, null);

Related

There is no indicator in custom tabhost

I want to create a custom tabhost, which contains an icon also. For the purpose I need to use a method. This method works fine, but the tab indicator is disappeared from the tab.
The code for the tab indicator is:
private View getTabIndicator(Context context, String title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(title);
return view;
}
Main Activity is :
public class MainActivity extends AppCompatActivity {
private FragmentTabHost mTabHost;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up a toolbar for the navigation purpose.
toolbar = (Toolbar)findViewById(R.id.app_bar);
toolbar.setTitle(" Call History Control");
toolbar.setLogo(R.mipmap.ic_launcher);
setSupportActionBar(toolbar);
// The fragments management is done here
// mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
// mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//
// mTabHost.addTab(
// mTabHost.newTabSpec("home").setIndicator(" HOME"),
// Home.class, null);
// mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(" SETTINGS"),
// Settings.class, null);
//
// mTabHost.addTab(mTabHost.newTabSpec("about").setIndicator(" ABOUT"),
// About.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("home").setIndicator(getTabIndicator(getApplicationContext(),"HOME",R.drawable.ic_home)),
Home.class, null);
mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator(getTabIndicator(getApplicationContext(),"SETTINGS",R.drawable.ic_settings)),
Settings.class, null);
mTabHost.addTab(mTabHost.newTabSpec("about").setIndicator(getTabIndicator(getApplicationContext(),"ABOUT",R.drawable.ic_account)),
About.class, null);
}
The screeen shot is:

Android could not change tab icon

In my Android application, I am using FragmentTabHost. I have added 3 tabs in the tabhost now my problem is I could not change the tab icon dynamically when choose each tab.
My requirement is if I select a tab other tabs icon should be changed, I got null pointer exception when try to change the tab icon. This is the line I got null pointer exception. the following code under onTabChanged method
ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.icon);
v1 is return null (checked by if condition)
public class MainActivity extends FragmentActivity implements
OnTabChangeListener {
private static final String TAB_1_TAG = "FindTab";
private static final String TAB_2_TAG = "AddPopUpTab";
private static final String TAB_3_TAG = "MyAccoutTab";
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitView();
}
private void InitView() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(
setIndicator1(MainActivity.this,
mTabHost.newTabSpec(TAB_1_TAG), R.drawable.tab1),
Tab1Container.class, null);
mTabHost.addTab(
setIndicator1(MainActivity.this,
mTabHost.newTabSpec(TAB_2_TAG), R.drawable.tab2),
Tab2Container.class, null);
mTabHost.addTab(
setIndicator1(MainActivity.this,
mTabHost.newTabSpec(TAB_3_TAG), R.drawable.tab3),
Tab3Container.class, null);
mTabHost.getTabWidget().setDividerDrawable(null);
mTabHost.setOnTabChangedListener(MainActivity.this);
}
private TabSpec setIndicator1(Context ctx, TabSpec spec, int genresIcon) {
View v = LayoutInflater.from(ctx).inflate(R.layout.tab_item, null);
ImageView img = (ImageView) v.findViewById(R.id.img_tabtxt);
img.setBackgroundResource(genresIcon);
return spec.setIndicator(v);
}
#Override
public void onTabChanged(String arg0) {
Log.e("tab change string", arg0);
ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
.findViewById(android.R.id.icon);
if (v1 == null) {
Log.e("v1", "null"); // Line
} else {
Log.e("v1", " Not null");
}
if (getResources().getDrawable(R.drawable.tab3) == null) {
Log.e("resource", "null");
} else {
Log.e("resource", "not null");
}
v1.setImageDrawable(getResources().getDrawable(R.drawable.tab3));
ImageView v2 = (ImageView) mTabHost.getTabWidget().getChildAt(1)
.findViewById(android.R.id.icon);
v2.setImageDrawable(getResources().getDrawable(R.drawable.tab1));
ImageView v3 = (ImageView) mTabHost.getTabWidget().getChildAt(2)
.findViewById(android.R.id.icon);
v3.setImageDrawable(getResources().getDrawable(R.drawable.tab2));
}
}
Layout for tab item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_tabtxt"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I think you need to you Actionbar is good.
If you want using Action bar then.
You need to try this
Change Tab icon in Action bar

How to add Run Time Tabs into TabHost in Android?

Hello I am developing an application
Which requires to add Run Time tabs in the android And all the tabs should add dynamically.
My Requirement is:
I want to add Tabhost with 5 tabs Which Should display the Screen.
But some Time it require only 3 tabs in the screen then design should not change.
Same if I want to add more then 5 tabs then tab should scroll Run time The main Thing is Design should not change.
Can any one tell me How can i do that?
Currently I am using Following Code:
public class Story_List extends ActivityGroup
{
ListView list_stories;
TabHost tab_stories;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.story_list);
tab_stories=(TabHost)findViewById(R.id.tabhoststories);
tab_stories.setup(this.getLocalActivityManager());
setupTab1(new TextView(this), "Album 1");
setupTab2(new TextView(this), "Album 2");
setupTab3(new TextView(this), "Album 3");
}
private void setupTab1(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum1.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab2(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum2.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private void setupTab3(final View view, final String tag)
{
View tabview = createTabView(tab_stories.getContext(), tag);
Intent intent = new Intent().setClass(this, StoryAlbum3.class);
TabSpec tab = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent);
tab_stories.addTab(tab);
}
private static View createTabView(final Context context, final String text)
{
View view = LayoutInflater.from(context).inflate(R.layout.tabs_text, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
try this :
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Tab1");
tabSpec.setContent(R.id.btnTab);
tabSpec.setIndicator("My Tab 1");
tabHost.addTab(tabSpec);
btnAddTab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TabHost.TabSpec spec = tabHost.newTabSpec("Tab"+i);
spec.setContent(new TabHost.TabContentFactory() {
#Override
public View createTabContent(String tag) {
return new AnalogClock(MainActivity.this);
}
});
spec.setIndicator("Clock");
tabHost.addTab(spec);
}
});

FragmentTabHost not creating view inside Fragment in android

I am having an issue getting the view to change on a tabhost - when I select a tab the content stays blank.
From what I can tell, onCreateView is not being called on the child Fragments. onMenuCreate runs fine because the menu changes like it is supposed to.
public class PatientTabFragment extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
NewPatientFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
NoteListFragment.class, null);
return mTabHost;
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
according to the docs:
Special TabHost that allows the use of Fragment objects for its tab
content. When placing this in a view hierarchy, after inflating the
hierarchy you must call setup(Context, FragmentManager, int) to
complete the initialization of the tab host.
(emphasis mine)
So I suggest somethong like this:
public class PatientTabFragment extends Fragment {
private FragmentTabHost mTabHost;
private boolean createdTab = false;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Info"),
NewPatientFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Notes"),
NoteListFragment.class, null);
return mTabHost;
}
public void onResume(){
if (!createdTab){
createdTab = true;
mTabHost.setup(getActivity(), getActivity().
getSupportedFragmentManager());
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
Now we can use TabLayout and ViewPager do those things.This is a good guide to use it.Here is my code:
viewPager=(NonSwipeableViewPager)view.findViewById(R.id.circleresdyn_viewpager);
tabLayout=(TabLayout)view.findViewById(R.id.circleresdyn_tablayout);
if (viewPager != null) {
Adapter adapter = new Adapter(((AppCompatActivity)activity).getSupportFragmentManager());
ContentFragment con=new ContentFragment();
con.setArguments(bundleForFramgnet);
MemberFragment memberFragment=new MemberFragment();
memberFragment.setArguments(bundleForFramgnet);
CirResDynTileFragment cirResDynTileFragment=new CirResDynTileFragment();
cirResDynTileFragment.setArguments(bundleForFramgnet);
adapter.addFragment(cirResDynTileFragment, "Tab1");
adapter.addFragment(con, "Tab2");
adapter.addFragment(memberFragment, "Tab3");
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).select();
}
Check this peace of code. It may help you:
import android.app.Fragment;
public class Change_password extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.change_password, container,false);
setTabs();
return rootView;
}
private void setTabs() {
try {
addTab("Airlines", R.drawable.tab_home, HomeActivity_bkp.class);
addTab("Advance Search", R.drawable.tab_search,
AdvanceSearchAcitivty.class);
addTab("Booking", R.drawable.tab_home, Booking.class);
addTab("Settings", R.drawable.tab_search, SettingAcitivty.class);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
// TODO: handle exception
}
}
private void addTab(String labelId, int drawableId, Class<?> c) {
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec 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.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}

Calling different tabs from normal activity in android

in my app after the SplashScreen i am calling a Tabactivity.
In tha tab activity, from the first tab i am switched over to a another activity called as Float, which is not related to the TabActivity. From this activity when a condition becomes True i want to show the third tab in the TabBar. How to open the third tab from the tab activity.
Following is the code of my Tabactivity class
public class MainTabBar extends TabActivity
{
TabHost tabHost;
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.maintab);
addTab1(Display.class);
addTab2(History.class);
addTab3(Capture .class);
addTab4(AboutUs.class);
}
private void addTab1( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab1");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.hometab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab2( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab2");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.macstab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab3( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab3");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.abouttab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
private void addTab4( Class<?> c)
{
TabHost tabHost = getTabHost();
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("Tab4");
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.contacttab, getTabWidget(), false);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
Is it to the above thing using a flag or any other easy way, pls suggest me friends, i am very new to android
You can use the tab index to show the third tab
tabHost.setCurrentTab(2);

Categories

Resources