Friday, June 24, 2011

SlideShow of images using android

Here in this session we learn how to create SlideShow using some set of images.Here it is a sample program only.Create a new project with name "SlideShowEx" in eclipse.

                                    





As shown above image first open main.xml file
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:text="Start Slide Show" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>

So from the above code LinearLayout tag is common and in the linearlayout create textview and Button as shown above .Here button is used when we click on button the slide show starts.

//MainActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
    Button button;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
         super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
                Intent intent=new Intent(MainActivity.this,SlideShow.class);
                startActivity(intent);
                //startDisplay();
            }
        });
    }
    
}
Here in MainActivity.java file first we get the Button object from main.xml file using findViewById(..) function write the code like when user clicks the button it has to move to next activity using intent.Here nothing is special but in the next activity i.e in SlideShow.java we wrote the code for slide show.

create one more xml file in layout folder with the name slideshow.xml(res/layout/slideshow.xml)
//slideshow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"
 
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:id="@+id/slideshow_layout"
  >

    <TextView android:text="10"
              android:id="@+id/textView1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"></TextView>
   <ImageView android:id="@+id/imageView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginTop="80px"
              android:layout_marginLeft="65px"
              ></ImageView>

             
</LinearLayout>

From the above code two things are important one is TextView tag and ImageView tag.These two object's are used in SlideShow.java file.
//SlideShow.java
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

public class SlideShow extends Activity{
    private TextView txtStatus;
    private ImageView imageView;
    int i=0;
    int imgid[]={R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,
            R.drawable.sample_6,R.drawable.sample_7};
    RefreshHandler refreshHandler=new RefreshHandler();
    
    class RefreshHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            SlideShow.this.updateUI();
        }
        public void sleep(long delayMillis){
            this.removeMessages(0);
            sendMessageDelayed(obtainMessage(0), delayMillis);
        }
    };
    public void updateUI(){
        int currentInt=Integer.parseInt((String)txtStatus.getText())+10;
        if(currentInt<=100){
            refreshHandler.sleep(2000);
            txtStatus.setText(String.valueOf(currentInt));
            if(i<imgid.length){
                imageView.setImageResource(imgid[i]);
                
                // imageView.setPadding(left, top, right, bottom);
                i++;
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slideshow);
        this.txtStatus=(TextView)this.findViewById(R.id.textView1);
        this.imageView=(ImageView)this.findViewById(R.id.imageView);
        updateUI();
    }

}Please see the above SlideShow.java code once again here  i am using threading concept using the threading concept only i am able to do slide show of images.Here my technique is threading only.Because if we display anything on the static screen once we cannot change the component's on screen in dynamical way.But by using threading condept we can change the view's state on the screen.
when the SlideShow.java starts executing first it touches onCreate(...) method i think every one know's about this in the onCreate(..) method i wrote the code for take the TextView,ImageView object's are taken from slideshow.xml file.

          Generally threading concept is handled by Handler class in android.Here i kept some sample images we can use any images and keep them in drawable folder.

    Don't forget to add this following statement in Manifest.xml file
      <activity android:name=".SlideShow" ></activity>    
 
If u face any problems please give me replay in terms of  comment to this tutorial.








23 comments:

  1. No funciona, al parecer falta algo

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. YEAH IT'S WORKING PROPERLY.............. AT FIRST ATTEMPT IT STARTS TO WORK. I JUST COPIED EACH PROG IN SEPARATE FILE. IT IS RUNNING PROPERLY :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) :-) THANK U RESPECTED MAM/SIR.

    ReplyDelete
  4. easy to understand... no one can give this much of explanation... even first day beginners also can understand this prog and execute successfully... but the fact is no one read the passage. one concentrate on coding. plz frns read the concept, waht they r saying in tat code. then implement that according to their procedure. sure 100% its working frns....

    ReplyDelete
  5. Tnx Dude for such a nice tutorial it helped me a lot in doing my dynamic image change task !!!!!!!

    @ramanabv it works properly.

    ReplyDelete
  6. good one dude if we want to show clickable image then how we get image value ???
    and how to redirect another page according to that image ??

    ReplyDelete
  7. Wrong...

    class RefreshHandler extends Handler should be static to prevent memory leaks.

    See: http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler

    ReplyDelete
    Replies
    1. You are right.
      But when changed to static class RefreshHandler extends Handler, error occurs on SlideShow.this.updateUI(); What should I do?

      Delete
    2. error : No enclosing instance of the type SlideShow is accessible in scope.

      Plz, help me!

      Delete
    3. you should create instance for SlideShow then,

      SlideShow show = new SlideShow();
      show.updateUI();

      Delete
  8. Its very useful,but i need to pause the slideshow and again to start the slideshow,can you please guide me

    ReplyDelete
  9. Its very useful, but i want to pause and start the slideshow by using onKeyListener ,could youplease give me the reply

    ReplyDelete
  10. Very Useful one . Thank'z for your code Yaar.

    ReplyDelete
  11. thanx yaar.......its working ...can we add extra animation in the case of transition of slides????

    ReplyDelete
  12. can you link the full source code???

    ReplyDelete
  13. Guys..plz help me..I did all the things as shown above.no errors were shown in java or xml files..But when i press the "Start Slide Show" button an error is appeared saying
    "Soory! The application SlideShowEx hs stopped unexpectedly.please try again" But try again and again also gives the same result..

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. I am using pre-built database for images and artist name. I am storing data into arraylist. I am not able to show the records in slideshow. I would really appreciate your help.


    ArrayList qa = db.GetAllValues();
    for (Qa qu : qa) {

    artist.setText(qu.getArtist());
    ImageView1.setImageResource(qu.getImage());

    //String log = "ID:" + qu.getID() + " Name: " + qu.getArtist();

    }


    ReplyDelete
  16. thanks yaar... very happy created my first android app!

    ReplyDelete
  17. Thank you for the code! How do I loop this slideshow?

    ReplyDelete
  18. Titanium Easy Flush - TITanium Easy Gourmet
    Shop Tinne Silvertip Silvertip Silvertip at TITanium. Find the best prices to buy in titanium 4000 India and buy titanium curling wand for titanium aftershokz delivery or titanium easy flux 125 amp welder in-store pick up 4x8 sheet metal prices near me at the best

    ReplyDelete