3

How can i implement Swipe between my tabs in tabhost thank you, following is my xml file and java file.

Please help me to implement swipe in this tabhost , Some one know more about tabhost kindly share allthe tabhost options.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bRecharge">

    </FrameLayout>
</TabHost>

java file

package com.veristics.tijo.smartrecharge;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.view.Menu;
import android.widget.TextView;

import com.veristics.san.smartrecharge.net.AsyncNetCall;

import java.util.HashMap;

public class MainActivity extends TabActivity{
float lastX;
AsyncNetCall asyncNetCall;

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


    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();

    TabHost.TabSpec mobile = tabHost.newTabSpec("Mobile");
    mobile.setIndicator("mobile");
    Intent mob = new Intent(MainActivity.this, MobileActivity.class);
    mobile.setContent(mob);

    TabHost.TabSpec dth = tabHost.newTabSpec("DTH");
    dth.setIndicator("DTH");
    Intent d = new Intent(MainActivity.this, DTHActivity.class);
    dth.setContent(d);

    TabHost.TabSpec datacard = tabHost.newTabSpec("Datacard");
    datacard.setIndicator("datacard");
    Intent dc = new Intent(MainActivity.this, DataCardActivity.class);
    datacard.setContent(dc);

    tabHost.addTab(mobile);
    tabHost.addTab(dth);
    tabHost.addTab(datacard);

    ActionBar mActionBar = getActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);

    View mCustomView = mInflater.inflate(R.layout.activity_custom_actionbar, null);

    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);

    new AsyncNetCall(this).execute("getUserBalance");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_account_log:
            Intent account_log = new   Intent(this,AccountLogActivity.class);
            startActivity(account_log);
            return true;
        case R.id.action_recharge_log:
            Intent recharge = new Intent(this,RechargeLogActivity.class);
            startActivity(recharge);
            return true;
        case R.id.action_logout:
            Intent login = new Intent(this, LoginActivity.class);
            login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}
Himanshu
  • 4,327
  • 16
  • 31
  • 39
Tijo John
  • 686
  • 1
  • 9
  • 20

1 Answers1

4

@Tijo John :

Bad Approach

  1. Using Deprecated Code TabActivity.

This class was deprecated in API level 13. New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API

Best Way

  1. Use ViewPager or FragmentTabHost

ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases.

Demo

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198