Jack King Jack King - 4 months ago 73
Java Question

tabs with webview - android studio

I want build application in android studio There are inside tabs :tab one | tab two | tab three | . and each tap load webview page ex:tab one load www.google.com tab tow load www.youtube.con –
please help me to build this .. it necessary
sorry for my bad English lang

Answer

Here It Is

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

MainActivity.java

 import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
 import android.os.Bundle;

public class MainActivity extends Activity {

// Declaring our tabs and the corresponding fragments.
ActionBar.Tab bmwTab, fordTab, toyotaTab;
Fragment bmwFragmentTab = new Fragment();
Fragment toyotaFragmentTab = new Fragment();
Fragment fordFragmentTab = new Fragment();

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

    // Asking for the default ActionBar element that our platform supports.
    ActionBar actionBar = getActionBar();

    // Screen handling while hiding ActionBar icon.
    actionBar.setDisplayShowHomeEnabled(false);

    // Screen handling while hiding Actionbar title.
    actionBar.setDisplayShowTitleEnabled(false);

    // Creating ActionBar tabs.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Setting custom tab icons.


    // Setting tab listeners.
    bmwTab.setTabListener((ActionBar.TabListener) new Fragment1());
    toyotaTab.setTabListener((ActionBar.TabListener) new Fragment2());
    fordTab.setTabListener((ActionBar.TabListener) new Fragment3());

    // Adding tabs to the ActionBar.
    actionBar.addTab(bmwTab);
    actionBar.addTab(toyotaTab);
    actionBar.addTab(fordTab);
}
}

PagerAdapter.java

import android.app.ActionBar.Tab;
import android.app.Fragment;
  import android.app.FragmentTransaction;
 import android.app.ActionBar;

    public class PagerAdapter implements ActionBar.TabListener {

private Fragment fragment;

// The contructor.
public PagerAdapter (Fragment fragment) {
    this.fragment = fragment;
}

// When a tab is tapped, the FragmentTransaction replaces
// the content of our main layout with the specified fragment;
// that's why we declared an id for the main layout.
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    ft.replace(R.id.activity_main, fragment);
}

// When a tab is unselected, we have to hide it from the user's view.
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    ft.remove(fragment);
}

// Nothing special here. Fragments already did the job.
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

} activity_fragment1.xml

 <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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ss.tabswithwebview.Fragment1">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView">

</WebView>

Fragment1.java

   import android.os.Bundle;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.webkit.WebView;

   public class Fragment1 extends android.support.v4.app.Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_fragment1, container, false);
    WebView webView=(WebView)rootView.findViewById(R.id.webView);
    webView.loadUrl("www.google.com");
    return rootView;
}

}

Similarly Create Different Fragment As Created Above. Thankyou.

pLease UpVote