Sunday, February 18, 2018

Tablayout In Android Example

Video Demo:


TabLayout provides a horizontal layout to display tabs. We can show the number of layout in single screen. In this tutorial I am using the two fragments to shows the tab data. DOWNLOAD SOURCE CODE FROM BELOW:


activity_main.xml:;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.design.widget.TabLayout
        android:id="@+id/tb_fragments"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f96239"
        app:tabIndicatorColor="#000000"
        app:tabSelectedTextColor="#ffffff"
        app:tabTextColor="#000000">

    </android.support.design.widget.TabLayout>

    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
MainActivity.javapackage com.deepshikha.tablayout;
package com.deepshikha.tablayout;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    TabLayout tb_fragments;
    FrameLayout fl_main;
    HomeFragment homeFragment;
    ContactFragment contactFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        init();
    }

    private void init(){
        tb_fragments = (TabLayout)findViewById(R.id.tb_fragments);
        fl_main = (FrameLayout) findViewById(R.id.fl_main);

        TabLayout.Tab firstTab = tb_fragments.newTab();
        firstTab.setText("Home");
        firstTab.setIcon(R.drawable.home);
        tb_fragments.addTab(firstTab);

        TabLayout.Tab secondTab = tb_fragments.newTab();
        secondTab.setText("Email");
        secondTab.setIcon(R.drawable.email);
        tb_fragments.addTab(secondTab);

        homeFragment = new HomeFragment();
        contactFragment = new ContactFragment();

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fl_main, homeFragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();

        tb_fragments.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Fragment fragment = null;
                switch (tab.getPosition()) {
                    case 0:
                        fragment = homeFragment;
                        break;
                    case 1:
                        fragment = contactFragment;
                        break;

                }
                FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.replace(R.id.fl_main, fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {


            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }
}
fragment_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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


</RelativeLayout>
HomeFragment.java:
package com.deepshikha.tablayout;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * Created by deepshikha on 2/2/18.
 */

public class HomeFragment extends Fragment {

    WebView webview;
    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        if (view==null) {
            view = inflater.inflate(R.layout.fragment_home, container, false);
            init();
        }
        return view;

    }

    private void init(){
        webview=(WebView)view.findViewById(R.id.webview);
        startWebView("http://deepshikhapuri.blogspot.in/");
    }

    private void startWebView(String url) {

        WebSettings settings = webview.getSettings();

        settings.setJavaScriptEnabled(true);
        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);

        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setLoadWithOverviewMode(true);

        final  ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e("ERRROr",description);
                Toast.makeText(getActivity(), "Error:" + description, Toast.LENGTH_SHORT).show();

            }
        });
        webview.loadUrl(url);
    }

}
fragment_contactus.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:textColor="#000000"
        android:gravity="center"
        android:padding="10dp"
        android:layout_centerInParent="true"
        android:text="deepshikhapuri.here@gmail.com"/>

</RelativeLayout>
ContactFragment.java:
package com.deepshikha.tablayout;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by deepshikha on 2/2/18.
 */

public class ContactFragment extends Fragment {

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_contactus,container,false);
        return view;

    }
}

DOWNLOAD SOURCE CODE FROM HERE

No comments:

Post a Comment