简体   繁体   中英

Putting tabs in other activities

I have a tabactivity with 5 tabs. Each with their own intents with classes. Now what I am trying to do is to put the same tab in other activities that is not included with the tabhosts tabspec . I am a beginner in Android. Please bear with me.

hometab.java:

public class HomeTab extends TabActivity {

private static final String LOG_TAG = null;
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_home_tab);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
    Resources ressources = getResources(); 

    final TabHost tabHost = getTabHost(); 
    Intent intentHome= new Intent().setClass(this, Home.class);
    TabSpec tabSpecHome = tabHost
      .newTabSpec("Home")
      .setIndicator("", ressources.getDrawable(R.drawable.home))
      .setContent(intentHome);

    Intent intentShopInfo= new Intent().setClass(this, ShopInfo.class);
    TabSpec tabSpecShopInfo = tabHost
      .newTabSpec("ShopInfo")
      .setIndicator("", ressources.getDrawable(R.drawable.home))
      .setContent(intentShopInfo);


    // Android tab  
    Intent intentDiscover= new Intent().setClass(this, MainAct.class);
    TabSpec tabSpecDiscover = tabHost
      .newTabSpec("MainAct")
      .setIndicator("", ressources.getDrawable(R.drawable.discover))
      .setContent(intentDiscover);

    // Apple tab
    Intent intentMode = new Intent().setClass(this, Mode.class);
    TabSpec tabSpecMode = tabHost
      .newTabSpec("Mode")
      .setIndicator("", ressources.getDrawable(R.drawable.mode))
      .setContent(intentMode);

    // Windows tab
    Intent intentExplore = new Intent().setClass(this, Explore.class);
    TabSpec tabSpecExplore = tabHost
      .newTabSpec("Explore")
      .setIndicator("", ressources.getDrawable(R.drawable.explore))
      .setContent(intentExplore);
    Intent intentSearch = new Intent().setClass(this, SearchView.class);
    TabSpec tabSpecSearch = tabHost
      .newTabSpec("SearchView")
      .setIndicator("", ressources.getDrawable(R.drawable.search))
      .setContent(intentSearch);

    // Blackberry tab


    // add all tabs 
    tabHost.addTab(tabSpecHome);
    tabHost.addTab(tabSpecDiscover);
    tabHost.addTab(tabSpecExplore);
    tabHost.addTab(tabSpecSearch);
    tabHost.addTab(tabSpecMode);

     for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#eb8114")); //unselected


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {         
             WifiManager wifi;
            wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
            setTabColor(tabHost);
            ConnectionDetector cd = new ConnectionDetector(getApplicationContext());


            Boolean isInternetPresent = cd.isConnectingToInternet();

            //Checking whether  Wifi is on or off
            if(isInternetPresent)
            {

            }
            else
            {
                if(tabHost.getCurrentTab() == 1)
                {
                Toast.makeText(HomeTab.this,
                          "No Internet Connection Please Enable Wifi Mode to Enjoy Discover Service "  , Toast.LENGTH_LONG)
                          .show();
                tabHost.setCurrentTab(2);

                }

            }
        }       
    });  

    }
public void setTabColor(TabHost tabhost) {


    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
        tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#eb8114")); //unselected

    if(tabhost.getCurrentTab()==0)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);


    }
    else if(tabhost.getCurrentTab()==1)
    {

            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dis);
    }
    else if(tabhost.getCurrentTab()==2)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.exp);
    }
    else if(tabhost.getCurrentTab()==3)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.sea);
    }
    else if(tabhost.getCurrentTab()==4)
    {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mod);
    }
}

 public void Back(View v)
 {
    Toast.makeText(HomeTab.this,
          "No Internet Connection Please Enable Wifi Mode to Enjoy Discover Service "  , Toast.LENGTH_LONG)
          .show();
 }
 public void Next(View v)
 {
    Intent intent = new Intent(HomeTab.this,Dbase.class);
startActivity(intent);
 }

}

XML

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="20dp"
        android:padding="5dp" />
          <TabWidget
        android:id="@android:id/tabs"

        android:layout_marginTop="20dp"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    </RelativeLayout>
</TabHost>
</RelativeLayout>

Thanks very much in advance.

Using this reference. Use ActivityGroup then replace the view with the intent code just like this:

Intent intent = new Intent(Events.this,EventInfo.class);
replaceContentView("activity3", intent);

The method used is:

private void replaceContentView(String id, Intent newIntent) {
        // TODO Auto-generated method stub
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }

That is what solved the problem for me. I hope this helps a lot of people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM