Create A View Switcher App In Android Application Using Android Studio

Introduction

In this article you will learn how to develop a View Switcher app in Android applications using Android Studio.

Requirements

  • Android Studio 2.1.3

If you want to create a View Switcher app in Android using android studio, you should follow the given steps.

Step 1


Now, open Android Studio and you can choose the File and subsequently New. Afterwards, choose the New Project.

ViewSwitcher

Step 2

Here, you can create your application name and choose where your project should be stored on the location and click Next button.

ViewSwitcher

Now, we can select the version of Android, which is Target Android Devices.

ViewSwitcher

Step 3

Here, we can add the activity and click Next button.

ViewSwitcher

Now, we can write the activity name and click Finish button.

ViewSwitcher

Step 4

Now, open your project and you will go to activity_main.xml. Afterwards, you will build the design, which should be chosen with the toolbox and if you want some option like (ViewSwitcher,button,TextView), it is possible with the help of the drag and drop method. Now, you will add the image on Drawable folder.

ViewSwitcher

Here, we can see the image in the Drawable folder.

ViewSwitcher

Now, we can see the Graphical User Interface design.

ViewSwitcher

Step 5

Here, you will build on the design and write the .XML code.

Activity_mai.xml code

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <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="xyz.rvconstructions.www.viewswitcher.MainActivity">  
  3.     <ViewSwitcher android:id="@+id/simpleViewSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content">  
  4.         <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/images" />  
  5.         <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical">  
  6.             <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second view" />  
  7.             <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- Button 2 -" /> </LinearLayout>  
  8.     </ViewSwitcher>  
  9.     <Button android:id="@+id/buttonNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="150dp" android:background="#005" android:text="NEXT" android:textColor="#fff" android:textStyle="bold" /> </RelativeLayout>  
Step 6

Now, you will go to the MainActivity.java page and build the Java code.

First, you need to declare a file, which is an extension file.

ViewSwitcher

Now, we can see MainActivity.java code.
  1. package xyz.rvconstructions.www.viewswitcher;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.widget.Button;  
  8. import android.widget.ViewSwitcher;  
  9. public class MainActivity extends AppCompatActivity {  
  10.     private ViewSwitcher firstViewSwitcher;  
  11.     Button buttonNext;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         buttonNext = (Button) findViewById(R.id.buttonNext);  
  17.         firstViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher);  
  18.         Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);  
  19.         Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);  
  20.         firstViewSwitcher.setInAnimation( in );  
  21.         firstViewSwitcher.setOutAnimation(out);  
  22.         buttonNext.setOnClickListener(new View.OnClickListener() {  
  23.             public void onClick(View v) {  
  24.                 firstViewSwitcher.showNext();  
  25.             }  
  26.         });  
  27.     }  
  28. }  
Step 7

Here, you will go to run and select run app option.

ViewSwitcher

Here, you will choose the Emulator or the device, which is Nokia Nokia _X

ViewSwitcher

Step 8

Here, you can view the first viewSwitcher output, as shown below.

ViewSwitcher

Now,you will view the second viewSwitcher.

ViewSwitcher  
Ebook Download
View all
Learn
View all