• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Saturday 29 March 2014

Posted by Unknown
1 comment | 10:54
Hello guys, Today we are going to see how to load a bunch of websites as the object in Swipe Screen in Android. So, lets get started..

1) First of all, create an Android Application Project and name it anything you like. For me its "Blog3" .

2) Then go to "res/layout/activity_main.xml" and add the below code.

<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=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/click" />

</RelativeLayout>

In this code we are simply creating a button in the middle of the android screen and on clicking the button our new Activity will start.

3) Next go to "src/your_package_name/MainActivity.java" and add the below piece of code.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
    Button click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click = (Button) findViewById(R.id.click);

        click.setOnClickListener(this);
    }

    @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 void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.click:
            Intent i = new Intent(this, Website.class);
            startActivity(i);
            break;
        }

    }

}

In this java file we are implementing the OnClickListener for the click button that we have previously created. And we are assigning some activity to be performed on clicking the click button.

4) Go to "src/your_package_name/" and right click there and create the new java file "Website.java" . Here we are creating our new activity which will show the bunch of websites.

5) Now go to "res/layout/" and right click there and create a new xml file, and name it as "pager.xml".

6) Open pager.xml and paste the code below.

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->
    <android.support.v4.view.PagerTitleStrip android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:textColor="#fff"
        android:paddingTop="4dp"
        android:paddingBottom="4dp" />

</android.support.v4.view.ViewPager>

7) Now go to "res/layout/" and right click there and create a new xml file, and name it as "result.xml".

8) Open "result.xml" and paste the code below.

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/web" >
 
</WebView>


9) Next open the "Website.java" file which you have created previously and copy paste the below code. Necessary   comments have been added there.

 import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Website extends FragmentActivity {
    DemoCollectionPagerAdapter mDemoCollectionPagerAdapter;
    /**
     * The {@link android.support.v4.view.ViewPager} that will display the
     * object collection.
     */
    ViewPager mViewPager;
    static WebView w;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.pager);
        mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(
                getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mDemoCollectionPagerAdapter);
        final ActionBar actionBar;
        actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @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) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // go to previous screen when app icon in action bar is clicked
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class DemoCollectionPagerAdapter extends
            FragmentStatePagerAdapter {

        public DemoCollectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new DemoObjectFragment();
            Bundle args = new Bundle();
            args.putInt(DemoObjectFragment.ARG_OBJECT, i); // Our object is just
                                                            // an integer :-P
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // For this contrived example, we have a 100-object collection.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            if (position == 0) {
                return "Google";
            } else if (position == 1) {
                return "Facebook";

            } else {
                return "Bing";
            }

        }
    }

    public static class DemoObjectFragment extends Fragment {

        public static final String ARG_OBJECT = "object";

        View rootView;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.result, container, false);
            Bundle args = getArguments();
            w = (WebView) rootView.findViewById(R.id.web);
            w.getSettings().setJavaScriptEnabled(true);
            w.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
            });
            final Activity activity = getActivity();

            w.setWebChromeClient(new WebChromeClient() {

                public void onProgressChanged(WebView view, int progress) {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    if (progress == 100)
                        activity.setTitle("TekArt");
                }
            });

            String url = "";
            int i = args.getInt(ARG_OBJECT);
            if (i == 0) {
                url = "http://www.google.com";
                w.loadUrl(url);
            } else if (i == 1) {
                url = "http://www.facebook.com";
                w.loadUrl(url);
            } else {
                url = "http://www.bing.com";
                w.loadUrl(url);
            }

            return rootView;

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            setRetainInstance(true);
        }

    }

}

In this class we are creating objects fragment and then in the fragment we are loading the different webpages in the objects.

10) Next go to "AndroidManifest.xml" and add the following permissions and activity.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <activity android:name="your_package_name.Website" />

Now we are done.


Post you problems in the comments. I will be very happy to handle those.

Thank you

1 comment:

  1. hey bro can you send me the eclipse source code for this as i cant get it to work

    ReplyDelete