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

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.

Kursus Ionic 2021

Kursus terbaharu IONIC-5 Pengenalan sepintas lalu pembangunan apps Ionic WhatsApp kami di   http://laravel.wasap.my Dapatkan tarikh kursus terkini di  fb.com/khirulnizam Kolej Komuniti Temerloh September 2020 ILP Selandar Ogos 2020 Berikut senarai tutorial yang sedang kami bangunkan; HARI 1 & 2 Pengenalan IONIC dan cara pasang dalam Windows10 (   http://fstm.kuis.edu.my/blog/ionic-pengenalan/   ). Versi  PDF pengenalan & install IONIC Pasang plugin TypeSript (skrip utama dalam Ionic) dalam Sublime. Struktur asas Page dalam projek Ionic. Sharing intent kepada media sosial. Plugin Kamera dalam apps Ionic ( http://fstm.kuis.edu.my/blog/ionic-kamera/ ) HARI 2 & 3 IONIC + FIREBASE Firebase Ionic CRUD – masukkan rekod baharu ( download slides ) Firebase Ionic CRUD – baca rekod dan senaraikan ( download slides ) Firebase Ionic CRUD – perbaharui dan pada rekod (download slides) Kod contoh lengkap S-CRUD IONIC+FIREBASE di  GITHUB.com/khirulnizam/ionmasji...

The Challenges of Handling Proverbs in Malay-English Machine Translation – a research paper

*This paper was presented in the 14th International Conference on Translation 2013 ( http://ppa14atf.usm.my/ ). 27 – 29 August 2013, Universiti Sains Malaysia, Penang. The PDF version is here: www.scribd.com/doc/163669571/Khirulnizam-The-Challenges-of-Automated-Detection-and-Translation-of-Malay-Proverb The test data is here: http://www.scribd.com/doc/163669588/Test-Data-for-the-Research-Paper-the-Challenges-of-Handling-Proverbs-in-Malay-English-Machine-Translation Khirulnizam Abd Rahman, Faculty of Information Science & Technology, KUIS Abstract: Proverb is a unique feature of Malay language in which a message or advice is not communicated indirectly through metaphoric phrases. However, the use of proverb will cause confusion or misinterpretation if one does not familiar with the phrases since they cannot be translated literally. This paper will discuss the process of automated filtering of Malay proverb in Malay text. The next process is translation. In machine translatio...