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