guness
guness

Reputation: 6636

Animation between tabs while using intents as tabs, how can I do that?

I have a tabHost with 4 different intents in it. I am trying to see animation while traversing between tabs. the code I am using is partially works:

@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    FrameLayout questionsLayout = (FrameLayout)tabHost.findViewById(android.R.id.tabcontent);
    questionsLayout.setAnimation(AnimationUtils.loadAnimation(tabHost.getContext(), R.anim.go_left_in));
}

however it only animates one animation which is an "inAnimation", I also want to add an "outAnimation" too, how can I do that.

By the way, i am using this code to add each tabs:

intent = new Intent().setClass(this, Tabs.class);
intent.putExtra("questions", rawQ);
spec = tabHost.newTabSpec("english").setIndicator(getText(R.string.ingilizce),res.getDrawable(R.drawable.ic_tabs)).setContent(intent);
tabHost.addTab(spec);

Lastly, I am using api version 8.

Last edit, entire code:

public class Questions extends TabActivity implements OnTabChangeListener {

public static final String TAG = "Questions";
private String macAddr;
private String json;
private TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
    Log.v(TAG, "Activity State: onCreate() " + TAG);
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        macAddr = extras.getString("macAddr");
        json = extras.getString("json");
    }




            // Make it fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);




    // parsing json data
    Question[] rawQ = parseJson(json);

    if (rawQ==null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Mac address could not found in database, please add it via control panel.")
                .setCancelable(false)
                .setNegativeButton("Okay",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                Intent i = new Intent(Questions.this, AnrdoinActivity.class);
                                 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                 startActivity(i);

                            }
                        });

        AlertDialog alert = builder.create();
        alert.show();

    } else {

        setContentView(R.layout.questions);

        Resources res = getResources(); // Resource object to get Drawables
        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, Tabs.class);
        intent.putExtra("questions", getLanguageQuestions(rawQ, 1));
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost
                .newTabSpec("kyrgyz")
                .setIndicator(getText(R.string.kirgizca),
                        res.getDrawable(R.drawable.ic_tabs))
                .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Tabs.class);
        intent.putExtra("questions", getLanguageQuestions(rawQ, 2));

        spec = tabHost
                .newTabSpec("turkish")
                .setIndicator(getText(R.string.turkce),
                        res.getDrawable(R.drawable.ic_tabs))
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Tabs.class);
        intent.putExtra("questions", getLanguageQuestions(rawQ, 3));

        spec = tabHost
                .newTabSpec("russian")
                .setIndicator(getText(R.string.rusca),
                        res.getDrawable(R.drawable.ic_tabs))
                .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Tabs.class);
        intent.putExtra("questions", getLanguageQuestions(rawQ, 4));

        spec = tabHost
                .newTabSpec("english")
                .setIndicator(getText(R.string.ingilizce),
                        res.getDrawable(R.drawable.ic_tabs))
                .setContent(intent);
        tabHost.addTab(spec);

        Log.v(TAG, 0+"");
        FrameLayout questionsLayout = (FrameLayout) tabHost.findViewById(android.R.id.tabcontent);
        Log.v(TAG, 1+""+questionsLayout.getId());

        Log.v(TAG, 2+"");
        tabHost.setCurrentTab(0);

        tabHost.setOnTabChangedListener(this);




    }


}
@Override
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    FrameLayout questionsLayout = (FrameLayout) tabHost.findViewById(android.R.id.tabcontent);      
    questionsLayout.setAnimation(AnimationUtils.loadAnimation(tabHost.getContext(), R.anim.go_left_in));
}

// Database related elements
private Question[] parseJson(String text) {
    JSONArray data = null;
    JSONObject groups = null;
    String[][] rawData = null;
    JSONArray[] questions = null;
    Question[] rawQ = null;
    try {
        data = new JSONArray(text);
        questions = new JSONArray[data.length() - 1];

        rawQ = new Question[questions.length];
        groups = data.getJSONObject(0);
        rawData = new String[2][groups.length()];
        Iterator it = groups.keys();
        int index = 0;

        while (it.hasNext()) {
            rawData[0][index] = (String) it.next();
            rawData[1][index] = groups.getString(rawData[0][index]);
            index++;
        }

        for (int i = 0; i < questions.length; i++) {
            questions[i] = data.getJSONArray(i + 1);

            String[] s = new String[6];
            for (int j = 0; j < s.length; j++) {
                s[j] = ((questions[i].getString(3 + j) == null) ? ("")
                        : (questions[i].getString(3 + j)));
            }
            rawQ[i] = new Question(questions[i].getInt(0),
                    questions[i].getInt(1), questions[i].getString(2), s);
        }

        Log.e(TAG, rawQ[1].getQuestion());
        return rawQ;
    } catch (JSONException e) {
        Log.e(ACTIVITY_SERVICE, e.toString());
        return null;
        // ctv.setText(data.toString());
    } catch (ArrayIndexOutOfBoundsException e) {
        Log.e(TAG, e.toString());
        return null;
    }

}
private Question[] getLanguageQuestions(Question[] Qs,int id){
    int count=0;
    for(Question q:Qs)
        count+=((q.getLanguageId()==id)?(1):(0));
    Question [] result = new Question[count];
    int index=0;
    for(Question q:Qs){
        if(q.getLanguageId()==id){
            result[index]=q;
            index++;
        }
    }
    return null;
}

}

Upvotes: 0

Views: 1219

Answers (2)

skyrift
skyrift

Reputation: 743

I've been looking around for ages to try and implement this, and actually wound up getting animations between different tabs (where my tabs are separate activities) by extending the TabHost slightly. In this particular implementation I've separated the animations so that it animates differently if you're going to a tab to the left or right of the old one:

public class MyAnimTabHost extends TabHost {

    public MyAnimTabHost(Context context) {
        super(context);
    }

    public MyAnimTabHost(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setCurrentTab(int index) {

        View currentView= this.getCurrentView();

        if (this.getCurrentTab()< index){

            if (currentView !=null){
                currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_out_to_left));
            }

            super.setCurrentTab(index);

            currentView= this.getCurrentView();

            if (currentView !=null){
                currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_in_from_right));
            }
        } else {

            if (currentView !=null){
                currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_out_to_right));
            }

            super.setCurrentTab(index);

            currentView= this.getCurrentView();

            if (currentView !=null){
                currentView.startAnimation(AnimationUtils.loadAnimation(this.getContext(),R.anim.slide_in_from_left));
            }

        }
    }

}

Adding this as a new class, ADT will automatically add it to the list of custom views in the graphic xml editor, so you can just swap out the TabHost in the layout for this one, and all should be good (make sure you remember to actually implement your different anim xml files).

Upvotes: 1

hackbod
hackbod

Reputation: 91331

Don't use TabActivity et al they are deprecated as described in the documentation.

Use Fragment to do tabs. If you are targeting 3.0+, this is very straight-forward in combination with the action bar. If you want older style tabs, there are samples in ApiDemos showing how to use them with fragments such as FragmentTabs or Support Library Fragment Tabs.

Or use this with ViewPager such as Support Fragment Tabs Pager.

Upvotes: 1

Related Questions