Introduction
In this article you will learn how to develop a View Switcher app in Android applications using Android Studio.
Requirements
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.
Step 2
Here, you can create your application name and choose where your project should be stored on the location and click Next button.
Now, we can select the version of Android, which is Target Android Devices.
Step 3
Here, we can add the activity and click Next button.
Now, we can write the activity name and click Finish button.
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.
Here, we can see the image in the Drawable folder.
Now, we can see the Graphical User Interface design.
Step 5
Here, you will build on the design and write the .XML code.
Activity_mai.xml code
- <?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" 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">
- <ViewSwitcher android:id="@+id/simpleViewSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content">
- <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/images" />
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical">
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second view" />
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="- Button 2 -" /> </LinearLayout>
- </ViewSwitcher>
- <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.
Now, we can see MainActivity.java code.
- package xyz.rvconstructions.www.viewswitcher;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.ViewSwitcher;
- public class MainActivity extends AppCompatActivity {
- private ViewSwitcher firstViewSwitcher;
- Button buttonNext;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- buttonNext = (Button) findViewById(R.id.buttonNext);
- firstViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher);
- Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
- Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
- firstViewSwitcher.setInAnimation( in );
- firstViewSwitcher.setOutAnimation(out);
- buttonNext.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- firstViewSwitcher.showNext();
- }
- });
- }
- }
Step 7
Here, you will go to run and select run app option.
Here, you will choose the Emulator or the device, which is Nokia Nokia _X
Step 8
Here, you can view the first viewSwitcher output, as shown below.
Now,you will view the second viewSwitcher.