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

WebDev

http://blog.kerul.net PHP DEVELOPMENT TOOLS Download the XAMPP latest version from www.apachefriends.org . This installation file contains the Apache web server, PHP 5 and 4 interpreter, and the MySQL 5 Community edition. - download latest version MozillaFireFox (OpenSource web browser firefox) – download latest version Google Chrome – fastest web browser on earcth – fast download chrome here TEACHING PLAN Download the teaching plan here for Web/Internet Programming ( download ) NOTES HTML references HTML Editor -  http://www.sublimetext.com/ Lab 1: HTML Basics -  http://www.w3schools.com/html/ Lab 2: Responsive Design:  http://www.w3schools.com/html/html_responsive.asp Lab 3: HTML Forms  http://www.w3schools.com/html/html_forms.asp Lab 4: HTML 5  http://www.w3schools.com/html/html5_intro.asp Lab 5: Bootstrap for responsive Web -  http://www.w3schools.com/bootstra

Submit your blog address here

Create your own blog and send the address by submitting the comment of this article. Make sure to provide your full name, matrix and URL address of your blog. Refer to the picture below. Manual on developing a blog using blogger.com and AdSense, download here … Download Windows Live Writer (a superb offline blog post editor)

Applications of Web 2.0

Web 2.0 describes the changing trends in the use of World Wide Web technology and web design that aim to enhance creativity , secure information sharing, collaboration and functionality of the web. Web 2.0 concepts have led to the development and evolution of web-based communities and hosted services , such as social-networking sites , video sharing sites , wikis , blogs . Find a website or web application that conform to the criteria of Web 2.0. Put the name of the application and the URL in the comment below. Please provide your full name and matrix number. Make sure the application you choose is not already chosen by your friend in the previous comment.

Contact Us at blog.kerul.net

Powered by EMF HTML Contact Form