Skip to main content

Adding XML Layout in the Swipe Views Fragment

swipe-views-navigation-fragment-android

There’s a Swipe Views navigation type in Android project. It comes as a default option when you install the Eclipse + ADT installation bundle (read here to download Eclipse + ADT).

The following and the steps and complete source CODES…

Create a new Android project in your Eclipse + ADT, the minimum required API is Android Level 11.

swipe-views-navigation-fragment-android-minimum-required-sdk

And when you arrive at the navigation option, as in the screen below, choose Swipe Views Navigation.

swipe-views-navigation-fragment-android-navigation-type

The default fragment layout is only contain a TextView, and you cannot have a layout. In the sample code, I suggested we create a XML layout which will provide greater UI flexibility. Add a layout file, by right clicking the project –> New –> Other –> Android XML Layout .

swipe-views-navigation-fragment-android-new-layout

The example shows I use a RelativeLayout in the new layout file.

swipe-views-navigation-fragment-android-relativelayout

These are the images used in this example.

image

And you can put the following UI inside the layout.

swipe-views-navigation-fragment-android-GUI-layout

The fr_relative.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adhome"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
ads:adSize="SMART_BANNER"
ads:adUnitId="738a44d913034b9f" />

<TextView
android:id="@+id/txtscript"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="78dp"
android:gravity="center_vertical|center_horizontal"
android:text="Hijrah Rasul s.a.w." />

<ImageView
android:id="@+id/screenimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/img00" />

<ImageView
android:id="@+id/btninfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/btninfo" />

<ImageView
android:id="@+id/btnfb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/btninfo"
android:src="@drawable/fb" />

<ImageView
android:id="@+id/btnhome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/btninfo"
android:src="@drawable/btnhome" />

</RelativeLayout>


Set the page title that will appear in the title stripe. Use the res>values>strings.xml to define the string values.


<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Hijrah Rasul</string>
<string name="title_section1">Hijrah Rasul oleh Ustaz Nazeer</string>

<string name="title_section2">1</string>
<string name="title_section3">2</string>
<string name="title_section4">3</string>
<string name="title_section5">4</string>
<string name="title_section6">5</string>
<string name="title_section7">6</string>
<string name="title_section8">7</string>
<string name="title_section9">8</string>
</resources>

 


Set the number of pages in the ViewPager. We have 8 images for 8 pages in the sample. Find the method getCount() and change the value.


@Override
public int getCount() {
// Show the number of pages.
return 8;
}


I’ve changed the DummySectionFragment with the new fr_relative.xml layout.


public DummySectionFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create a new TextView and set its text to the fragment's section
// number argument value.

//using XML layout
//v is to load the fragment layout
View v = inflater.inflate(R.layout.fr_relative, container, false);

//invoke admob ads
adhome = (AdView)v.findViewById(R.id.adhome);
adhome.loadAd(new AdRequest());

//get the screen number
int skrinno = getArguments().getInt(ARG_SECTION_NUMBER);

//set the screen image
screenimg=(ImageView)v.findViewById(R.id.screenimg);
setImage(skrinno);

txtscript=(TextView)v.findViewById(R.id.txtscript);
txtscript.setText(skrintxt[skrinno-1]);

//define the btnfb to display fb.com/UstazNazeer
btnfb = (ImageView) v.findViewById(R.id.btnfb);

...

return v;

}
}


There is another important method to add. This method is used to interchange the screen image.


//the function to change screen image
public void setImage(int skrin){
//try to display screen
switch (skrin){
case 0:screenimg.setImageResource(R.drawable.img00);
//decodeSampledBitmapFromResource(getResources(), R.id., 100, 100))
break;
case 1:
screenimg.setImageResource(R.drawable.img01);
break;
case 2:
screenimg.setImageResource(R.drawable.img02);
break;
case 3:
screenimg.setImageResource(R.drawable.img03);
break;
case 4:
screenimg.setImageResource(R.drawable.img04);
break;
case 5:
screenimg.setImageResource(R.drawable.img05);
break;
case 6:
screenimg.setImageResource(R.drawable.img06);
break;
case 7:
screenimg.setImageResource(R.drawable.img07);
break;
case 8:
screenimg.setImageResource(R.drawable.img08);
break;
}
}


The rest are available in the complete source-code available here –> https://docs.google.com/file/d/0B34ZxOOoeSDdOVUtYngwVy1vVGs/edit?usp=sharing


{UPDATE}


java.lang.OutOfMemoryError


java.lang.OutOfMemoryError


OutOfMemory Error when the app has lots of Pages (in ViewPager )


(PagerAdapter caused Out-Of-Memory runtime error)


Last time I developed my storybook app, I came across this OutOfMemory runtime error. It turn out that the default code frame provided does not support pages with images. This happen due to when the next page is swiped, it will create another object to store the image in the memory. If you have let say 10 pages with different images for each page, then most likely your app will crash.


To overcome this problem – the easiest solution (but not the best) is to search the class inheritance named FragmentPagerAdapterState and change it into FragmentStatePagerAdapter. As mentioned in the documentation of the code framework, the FragmentStatePagerAdapter can handle better memory-consuming app because the API calls garbage collector automatically when the page is swiped. Where else FragmentPagerAdapterState does not do that.


public class SectionsPagerAdapter extends FragmentStatePagerAdapter{


/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/

Comments

Popular posts from this blog

Several English proverbs and the Malay pair

Or you could download here for the Malay proverbs app – https://play.google.com/store/apps/details?id=net.kerul.peribahasa English proverbs and the Malay pair Corpus Reference: Amir Muslim, 2009. Peribahasa dan ungkapan Inggeris-Melayu. DBP, Kuala Lumpur http://books.google.com.my/books/about/Peribahasa_dan_ungkapan_Inggeris_Melayu.html?id=bgwwQwAACAAJ CTRL+F to search Proverbs in English Definition in English Similar Malay Proverbs Definition in Malay 1 Where there is a country, there are people. A country must have people. Ada air adalah ikan. Ada negeri adalah rakyatnya. 2 Dry bread at home is better than roast meat home's the best hujan emas di negeri orang,hujan batu di negeri sendiri Betapa baik pun tempat orang, baik lagi tempat sendiri. 3 There's no accounting for tastes We can't assume that every people have a same feel Kepala sama hitam hati lain-lain. Dalam kehidupan ini, setiap insan berbeza cara, kesukaan, perangai, tabia

Contact Us at blog.kerul.net

Powered by EMF HTML Contact Form

Login JSON Android using Login Activity

I’ve been trying to release this tutorial quite a while. At last after a long hard effort. Since HttpClient is not supported any more in Android SDK 23, I have to resort to org.json.JSONObject and java.net.HttpURLConnection library to do online database with JSON. The objective of this tutorial is to log-in from a mobile client with the username and password stored in an online database facility. STEP 1: Create a new Android project, this time choose the LoginActivity .

Bootstrap Template for PHP database system - MyCompanyHR

HTML without framework is dull. Doing hard-coded CSS and JS are quite difficult with no promising result on cross platform compatibility. So I decided to explore BootStrap as they said it is the most popular web framework. What is BootStrap? - Bootstrap is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first web sites. (  http://www.w3schools.com/bootstrap/   ) Available here -  http://getbootstrap.com/ Why you need Flat-UI? Seems like a beautiful theme to make my site look professional. Anyway you could get variety of BootStrap theme out there, feel free to select here  http://bootstraphero.com/the-big-badass-list-of-twitter-bootstrap-resources/ Flat-UI is from DesignModo -   http://designmodo.com/flat/ Web Programming MyCompanyHR – PHP & MySQL mini project (with Boostrap HTML framework) Template 1: Template for the Lab Exercise. This is a project sample of a staff record management system. It has the PHP structured co

Pick a file using Intent.ACTION_GET_CONTENT

This tutorial is tested on Honeycomb 3.0 environment. In order to be used it in the lower level of Android API, I believe you could just change the target in the Android project. Screenshots Pick an image file using Intent.ACTION_GET_CONTENT The intention of this project is to choose an image file available in the device storage, and simply display it in an ImageView. The benefit of using Intent.ACTION_GET_CONTENT is you don’t have to develop open dialog box which is not available in the Android library. ACTION_GET_CONTENT with MIME type */* and category CATEGORY_OPENABLE -- Display all pickers for data that can be opened with ContentResolver.openInputStream() , allowing the user to pick one of them and then some data inside of it and returning the resulting URI to the caller. This can be used, for example, image chooser (as shown in this article).