0% found this document useful (0 votes)
9 views2 pages

Program 15

The document contains XML and Java code for an image switcher Android application. The XML layout defines a text view, image switcher, and button. The Java code sets up the image switcher to cycle through images on button click using animations.

Uploaded by

aherrani757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Program 15

The document contains XML and Java code for an image switcher Android application. The XML layout defines a text view, image switcher, and button. The Java code sets up the image switcher to cycle through images on button click using animations.

Uploaded by

aherrani757
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Activity_main.

xml

<?xml version="1.0" encoding="utf-8"?>


<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Switcher Example"

android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="28dp"
android:layout_marginTop="40dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/button"
android:layout_marginBottom="47dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

After design xml file download images and store in drawable folder in android studio.

Mainactivity.java

package com.example.program15;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {


ImageSwitcher imageSwitcher;
Button nextButton;
int imageSwitcherImages[] =
{R.drawable.cpp, R.drawable.c_sarp, R.drawable.jsp, R.drawable.mysql};
int switcherImageLength = imageSwitcherImages.length;
int counter = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
nextButton = (Button) findViewById(R.id.button);

imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView switcherImageView = new ImageView(getApplicationContext());
switcherImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
switcherImageView.setImageResource(R.drawable.jsp);
return switcherImageView;
}
});

Animation aniOut = AnimationUtils.loadAnimation(this,


android.R.anim.slide_out_right);
Animation aniIn = AnimationUtils.loadAnimation(this,
android.R.anim.slide_in_left);

imageSwitcher.setOutAnimation(aniOut);
imageSwitcher.setInAnimation(aniIn);

nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if (counter == switcherImageLength){
counter = 0;
imageSwitcher.setImageResource(imageSwitcherImages[counter]);
}
else{
imageSwitcher.setImageResource(imageSwitcherImages[counter]);
}
}
});
}
}

You might also like