Native Mobile App Development With Java

Image, button transition left to right, right to left

04-Oct-2024

Image, button transition left to right, right to left


1/  Create A New Project . Design the xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/RL1"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/transitionButton"
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Transition Button"

android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:visibility="visible"
android:src="@drawable/img1" />

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Just Click The Buttons 👇"
android:layout_below="@id/transitionButton"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:fontFamily="sans-serif"
android:textSize="50px"/>

<Button
android:id="@+id/leftToRight"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="left To Right"
android:layout_below="@id/textView1"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"/>

<Button
android:id="@+id/RightToLeft"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Right To Left"
android:layout_below="@id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"/>



</RelativeLayout>



2/  Create Res -> anim -> slide_in_left.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="-100%"
android:toXDelta="100%" />
</set>


3/  Create Res -> anim -> slide_in_right.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="100%"
android:toXDelta="-100%" />
</set>


4/  inplement the java code


package com.example.imageslider;

import android.animation.ObjectAnimator;
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.ImageButton;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;



public class MainActivity extends AppCompatActivity {


Button transitionButton, leftToRight, RightToLeft;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);


transitionButton = findViewById(R.id.transitionButton);
leftToRight = findViewById(R.id.leftToRight);
RightToLeft = findViewById(R.id.RightToLeft);

// Slide in from left
// Animation slideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
//transitionButton.startAnimation(slideInLeft);

leftToRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Animation object = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_in_left);
transitionButton.startAnimation(object);


}
});


RightToLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Animation object = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_out_right);
transitionButton.startAnimation(object);

}
});



}
}


5/  Run and See the output




Comments