Cho Hee
Cho Hee

Reputation: 165

Android Fragment Tabs using - android.support.v4.app.Fragment

I am trying to create a app with 3 tabs Fragments but I want to use the new android.support.v4.app.Fragment in Android. But I can't get it to work.

I tried this example implementing-fragment-tabs-in-android. it works but the problem is that android.app.Fragment; only works with API 11 and above. and I Want to target API 8 and above.

Here is my code:

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

    public static final String TAG = MainActivity.class.getSimpleName();

    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2, Tab3;
    Fragment fragmentTab1 = new FragmentTab1();//ERROR = Type mismatch: cannot convert from FragmentTab1 to Fragment
    Fragment fragmentTab2 = new FragmentTab2();//ERROR = Type mismatch: cannot convert from FragmentTab1 to Fragment
    Fragment fragmentTab3 = new FragmentTab3();//ERROR = Type mismatch: cannot convert from FragmentTab1 to Fragment


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Hide Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();


        // Hide Actionbar Icon
        actionBar.setDisplayShowHomeEnabled(false);
        // Hide Actionbar Title
        actionBar.setDisplayShowTitleEnabled(false);
        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("Tab1");//.setIcon(R.drawable.tab1);
        Tab2 = actionBar.newTab().setText("Tab2");
        Tab3 = actionBar.newTab().setText("Tab3");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        Tab3.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
        actionBar.addTab(Tab3);


    }//-----end onCreate




    //implements ActionBar.TabListener --------------------------
    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


    //Action bar of AppCombat ---------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}//--end body

Thanks for your help. :)

Upvotes: 2

Views: 1426

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

import android.app.Fragment;

your import(s) have to be consistent. If you use the support library all the related imports should be from the support library.

Upvotes: 1

Related Questions