Skip to main content

Flash-card app using FragmentPagerAdapter in Android with swipe and Sound

This is yet another flash-card app with swipe navigation and sound play for Android.

Flash-card app using swipe FragmentPagerAdapter 1Flash-card app using swipe FragmentPagerAdapter 2

***Important – am using ADT Bundle of the release 20130917 (adt-bundle-windows-x86_64-20130917). The older version on ADT might use slightly different approach. Download here http://developer.android.com/sdk ***

Create a new Android project.

On the File menu choose New> Android Application Project. Name the Android Application and change the minimum required SDK to API 11.

minimum-required-sdk-api-11-swipe

On the window that provides the navigation type, choose Scrollable Tabs + Swipe.

navigation-scrollable-tabs-swipe

These are the modifications to be made;

1. Copy the materials (images to drawable, mp3 sound files to assets)

copy-resources-swipe-flash-card

2. Prepare the page layout.

Using the latest IDE, you will get the following layout file named fragment_main_dummy.xml . In the project we are using these IDs; ImageView imgnumber to hold the image number, ImageButton btnsound for the user interaction of playing the sound for the number’s pronunciation.

fragment_main_dummy

the fragment_main_dummy.xml

<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$DummySectionFragment" >

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
android:id="@+id/imgnumber"
android:layout_width="wrap_content"
android:layout_height="320dp"
android:layout_weight="1"
android:src="@drawable/no1" />

</TableRow>

<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageButton
android:id="@+id/btnsound"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/sound" />

</TableRow>
</TableLayout>

</RelativeLayout>


3. Changing the page titles – open the file res>values>strings.xml, and modify accordingly.


adjust-page-title-name


4. The page numbers – find the method getCount, and change accordingly.


public int getCount() {
// Show 3 total pages.
return 3;
}


5. Make sure the DummySectionFragment class implements OnClickListener – because the sound button need this.


public static class DummySectionFragment extends Fragment implements OnClickListener


6. Declare the necessary UI – just before the method onCreateView inside the DummySectionFragment class.


//put all the mp3 filename as String array
private String[] mp3={"1.mp3","2.mp3","3.mp3"};
private ImageView imgnumber;
private ImageButton btnsound;
private View rootView;
//get the current screen number
private int skrinno;


7. Make the necessary amendment to the onCreateView method.


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
skrinno= getArguments().getInt(ARG_SECTION_NUMBER);

btnsound=(ImageButton)rootView.findViewById(R.id.btnsound);
btnsound.setOnClickListener(this);

//set the screen image
imgnumber=(ImageView)rootView.findViewById(R.id.imgnumber);
if(skrinno==1){
imgnumber.setImageResource(R.drawable.no1);
}
else if(skrinno==2){
imgnumber.setImageResource(R.drawable.no2);
}
else if(skrinno==3){
imgnumber.setImageResource(R.drawable.no3);
}

return rootView;
}


8. This is the onClick method to handle btnsound click.


@Override
public void onClick(View v) {
//button onClick handler
if(v.getId()==R.id.btnsound){
playSound(mp3[skrinno-1]);//array starts from 0
}
}//end onClick


9. This is the playsound method to play the mp3 sound file.


private MediaPlayer mp;
//the playSound function - copy from blog.kerul.net
public void playSound(String soundName){
Boolean mpPlayingStatus=false;
mp=new MediaPlayer();

try{//try to check MediaPlayer status
mpPlayingStatus=mp.isPlaying();
}
catch (Exception e){
mpPlayingStatus=false;
}
//if the MediaPlayer is playing a sound, stop it to play new sound
if (mpPlayingStatus==true){
mp.stop(); //stop the sound
mp.release(); //remove sound from the memory
}
else{
try{
mp = new MediaPlayer();
//this is to load the mp3 sound from the assets folder
AssetFileDescriptor afd = rootView.getContext().getAssets().openFd(soundName);
//set the sound source file
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
mp.prepare(); // prepare for playback
mp.start(); //play the sound
afd.close();//release the file descriptor
}//try block
catch(IOException e) {
//display the error message in debug
Log.i("Error playing sound: ", e.toString());
}
}
}//end playSound


The complete code is available for download here –> http://bit.ly/android-swipe


Try the app at the Google Play –> https://play.google.com/store/apps/details?id=net.kerul.flash_swipe_numbers_123





*** Do bookmark/share this in you G+ / FB / Twitter …

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...

Pemasangan Joomla! 1.7 pada pelayan web komputer anda

Latihan ini akan memasang sistem pengurusan kandungan laman web ke dalam pelayan web yang anda telah pasang sebelum ini . LANGKAH 1: Aktifkan Pelayan Web dan Pangkalan Data Aktifkan XAMPP Control Panel, melalui “ Start->All Programs->ApacheFriends->XAMPP Control Panel ”. Rajah 2.1 Pastikan pelayan web Apache dan pelayan pangkalan data MySQL diaktifkan dengan klik butang START. -> Rajah 2.2

searchdelete.php (list records of table to delete)

< form name = 'search' method = 'get' action = '' > Insert the firstname < input type = 'text' name = 'txtname' > < input type = 'submit' value = 'search name' > </ form > < ?php //database connect $db = mysqli_connect ( "localhost" , "root" , "" , "mycompanyhr" ); //checking database connection if ( $db == false ) { echo "Connect failed: " . mysqli_connect_error ( $db ); exit (); } else { echo "Connection successful" ; } $n = $_GET [ 'txtname' ]; $sql = "select EMPNO, FIRSTNAME, LASTNAME, WORKDEPT, PHONENO from employee where FIRSTNAME like '%$n%' " ; $rs = mysqli_query ( $db , $sql ); if ( $rs == false ){ echo ( "Query cannot be executed!<br>" ); echo ( "SQL Error : " . mysqli_error ( $db )); } else { echo "<table border=1>...

ViewFlipper Example–a simple FlashCard

UPDATE: Improved with Fling gesture (Sept 2012) UPDATE: ViewFlipper with Flip-In and Flip-Out Animation (August 2012) This tutorial is to demonstrate the ViewFlipper layout that is almost similar to CardLayout (in Java). The app will produce a simple Flash card that provide several screens with different picture for each card. Flip-in and Flip-out animation provided. Added in Sept 2012 – an improvement to support Fling gesture – enjoy… The amendment is only on the coding part. Some how the layout design (main.xml) is quite long. Later I’ll produce separated screen by including several XML layout from outside files. Screenshots;

Installing Google AdMob into Android Apps

Previously I wrote on why ads are needed to help maintaining an app. Read the article here http://blog.kerul.net/2011/05/generating-revenue-from-free-mobile.html . ---This is quite an old article. You may find the latest supporting AdMob 6.x in here http://blog.kerul.net/2012/08/example-how-to-install-google-admob-6x.html --- This is quite a long tutorial, there are 3 major steps involved. The experiment is done using Windows 7, Eclipse Helios and AdMob SDK 4.1.0 (which currently is the latest-during time of writing). STEP 1: Get the ads from AdMob.com To display the AdMob ads in your Android mobile apps, you need to register first at the admob.com . After completing the registration, login and Add Site/App. Refer to Figure 1. Figure 1 Choose the desired platform and fill in the details (as in Figure 2). Just put http:// in the Android Package URL if your app is not published in the market yet. And click Continue. Figure 2 Download the AdMob Android SDK, and save the zip fil...