The Wayback Machine - https://web.archive.org/web/20241001193950/https://www.geeksforgeeks.org/elastic-view-in-android/
Open In App

Elastic View in Android

Last Updated : 06 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, ElasticView is added in android. The ElasticView is a regular CardView, which can flex from user touches. OnClickListener and other various important methods can also be added to the child view of ElasticView. It makes the user interface more attractive thereby enhancing the user experience.

Approach:

Step 1: Add the support library in build.gradle file and add the dependency in the dependencies section.  It will allow us to add the ElasticView directly in the XML file. 

XML




dependencies {        
       implementation 'com.github.armcha:ElasticView:0.2.0'   
}


Step 2: Add the following code in the activity_main.xml file. In this file, Elastic View with its child view as ImageView is added to the layout. 

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <io.armcha.elasticview.ElasticView
        app:flexibility="7"
        android:layout_margin="20dp"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:cardElevation="5dp">
 
        <ImageView
            android:id="@+id/imageView"
            android:src="@drawable/gfg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
 
    </io.armcha.elasticview.ElasticView>
 
</LinearLayout>


Step 3: Add the following code in the MainActivity.kt file. In this file, OnClickListener is added to ImageView so whenever the user clicks on it, the OnClickListener function gets invoked automatically.

Kotlin




import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
 
 
class MainActivity : AppCompatActivity() {
    var imageView: ImageView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        imageView = findViewById(R.id.imageView);
        imageView.setOnClickListener(View.OnClickListener {
            Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show()
        })
    }
}


Java




import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
 
public class MainActivity extends AppCompatActivity {
    // references
    private ImageView imageView;
 
    @Override
    protected void
    onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        imageView = findViewById(R.id.imageView);
        imageView.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    Toast.makeText(this, "Click",
                                  Toast.LENGTH_SHORT)
                        .show();
                }
            });
    }
}


Output:

Refer to the official documentation for more information.



Similar Reads

Android | Creating a Calendar View app
This article shows how to create an android application for displaying the Calendar using CalendarView. It also provides the selection of the current date and displaying the date. The setOnDateChangeListener Interface is used which provide onSelectedDayChange method. onSelectedDayChange: In this method, we get the values of days, months, and years
3 min read
Android - Line Graph View with Kotlin
Graphs are used in android applications to display vast amounts of data in an easily readable form. There are different types of graphs used such as BarGraph, GroupBar graph, point, and line graph to represent data. In this article, we will take a look at How to use Line Graph View in Android using Kotlin. A sample video is given below to get an id
3 min read
How to implement View Shaker in Android
View Shaker is an animation in which, the UI of screen vibrates for a limited period of time. This can be implemented on the whole layout or some particular widget. It is a very common effect that developers use, especially to show incorrect credentials. View Shaker helps us to animate the widgets. Various effects can be added to it. Effects Attent
3 min read
How to Add a Custom View Inside ActionBar in Android?
In Android, an Action Bar, or an app bar is a dedicated space at the top of the application on the screen that indicates the name of the application or activity. An Action bar helps users navigate or locate themself in the application. A sample Action Bar is shown in the below image. In this article, we will show you how you could customize the Act
3 min read
How to add KenBurns View in Android?
In this article, we will learn about how to add KenBurns View in android using java. KenBurns View is a useful library that is an extension of ImageView. It creates an immersive experience by animating its Drawable. We can use RandomTransitionGenerator to change the duration and acts as a interpolator of transitions. If we want to have more control
2 min read
View Recycling in Android with ListView
Memory management is a crucial aspect of app development. Since mobile devices have very limited memory, it is necessary to use it carefully in our applications. One of the best practices involved in doing so is "view recycling". This article is about view recycling in Android and then a simple app is created which implements the practice of view r
4 min read
Double-Tap on a View in Android
Detecting a double tap i.e. whenever the user double taps on any view how it is detected and according to the view a response can be added corresponding to it. Here an example is shown in which the double tap on the view is detected and corresponding to it a response is added in the form of toast. Step 1: Create an Empty activity in Android Studio.
2 min read
Triple-Tap on a View Using onTouchListener Object in Android
To detect a triple tap i.e. whenever the user triple taps on any view how it is detected and according to the view a response can be added corresponding to it. Here an example is shown in which the user triple taps on the view, it is detected and corresponding to the view response is added in the form of toast. Step 1: Create an Empty activity in A
2 min read
Count the Number of Taps (Multi-Tapping) on a View in Android
In this article, the number of taps that user does in a short span of time on a particular view is counted, It can be used for adding different response with different taps. For example one tap on the word will show the meaning of the word while double tap will show its synonyms in a dictionary app. Below is an example in which the number of taps d
3 min read
MVC (Model View Controller) Architecture Pattern in Android with Example
Developing an android application by applying a software architecture pattern is always preferred by the developers. An architecture pattern gives modularity to the project files and assures that all the codes get covered in Unit testing. It makes the task easy for developers to maintain the software and to expand the features of the application in
8 min read
MVP (Model View Presenter) Architecture Pattern in Android with Example
In the initial stages of Android development, learners do write codes in such a manner that eventually creates a MainActivity class which contains all the implementation logic(real-world business logic) of the application. This approach of app development leads to Android activity gets closely coupled to both UI and the application data processing
12 min read
Different Ways to View Contents of Database File in Android Studio
Data is a group of information and it can be any kind of information - text, numbers, image, video. An organized collection of this data is called a Database. It is made so that the data can be accessed and managed easily. Data can be organized into tables, rows, columns as that make it easier to handle the data. The main purpose of having a databa
4 min read
MVVM (Model View ViewModel) Architecture Pattern in Android
Developers always prefer clean and structured code for projects. Organizing the codes according to a design pattern helps in the maintenance of the software. By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features. Further, design patterns also assure that all the codes get covered in U
7 min read
Zigzag View in Android
Zig zag View is another best feature to beautify the user Experience in an Android app. You can get to see this Zig zag view in most of the eCommerce app. The main use of this Zig zag view is to make attractive banner ads in an app. Or to display images and text in this view. In this article, we are going to see how to implement Zig zag View in And
2 min read
View Binding in Android Jetpack
View Binding is one of the best features which provides the views to bind with the activity which is ongoing. Replacing the findViewById() method, hence reducing the boilerplate code, generated the instances of the views of the current layout. And most important feature about the View Binding is it's always null safe. In this article detailed it's
4 min read
View Binding with Fragments in Android Jetpack
In the Previous article View Binding in Android Jetpack, it's been discussed why acquiring the ViewBinding feature in the Android project provides a lot of benefits. But when it comes to ViewBinding with fragments the scenario changes. Because the lifecycle of the Fragment is different and that of activity is different Here also things go the same
6 min read
How to View and Locate SQLite Database in Android Studio?
SQLite is an open-source relational database that is used to perform database operations on android devices such as storing, manipulating, or retrieving persistent data from the database. In this article, we will learn how to view and locate SQLite database in Android Studio using device file explorer. Note: Package name is found at the top of any
2 min read
How to Capture Screenshot of a View and Save it to Gallery in Android?
In this article, we will capture a screenshot of a view and store the image in the gallery. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. [video mp4="https://media.geeksforgeeks.org/wp-content/uploads/20210208210909/capture_view
4 min read
Different Ways to View Method Information in Android Studio
Easy access to documentation written in comments when hovered over the text is like a cherry on a cake. In Android Studio and other JetBrains editors, there is an option that you can enable to display function types and docs on mouse hover. But first, let's get familiar with what methods are in Android Studio? According to the official site of Andr
3 min read
How to Use View Binding in RecyclerView Adapter Class in Android?
View Binding is a part of Android Jetpack which provides the views to bind with the classes by replacing the findViewById() method in a null safe way. In this article, we will see its implementations in the Adapter class of RecyclerView. Note that we are going to implement this project using the Kotlin language. Step by Step Implementation Step 1:
5 min read
Difference Between View and ViewGroup in Android
In Android Layout is used to describe the user interface for an app or activity, and it stores the UI elements that will be visible to the user. An android app's user interface is made up of a series of View and ViewGroup elements. In most cases, android apps will have one or more operations, each of which is a single screen of the app. Multiple UI
5 min read
How to View and Locate Realm Database in Android Studio?
Realm’s mobile database is an open-source, developer-friendly alternative to CoreData and SQLite. Realm Database is a service which is provided by MongoDb which is used to store data in users device locally. With the help of this data can be stored easily in users' devices and can be accessed easily. We can use this database to store data in the us
2 min read
How to Apply View Animations Effects in Android?
Android View Animations are used to apply amazing animations on TextView and EditText in the android application. Such animations provide the app with a smooth look in a new way. In this article, we are going to develop the Android View Animation effect in Android Studio. What we are going to build in this article? In this article, we will develop
3 min read
How to View Data Stored in Shared Preferences in Android Studio?
Shared Preferences in Android is local storage where we can save our data using a key and value pair. It is generally used to store data in users' devices. Shared Preferences is also used for session management in Android apps where we are using login functionality. In this article, we will take a look at how we can view the data store in Shared Pr
4 min read
How to Create WhatsApp Stories View in Android?
Stories are now becoming one of the most seen features in many different apps such as WhatsApp, LinkedIn, Instagram, and many more. In this article, we will take a look at creating a similar type of view in our Android App. What we are going to build in this article? We will be building a simple application in which we will be creating a stories vi
7 min read
How to Create Your Own Custom View in Android with Kotlin?
In this article, we're going to talk about how we can create our own custom view in Android step by step. We all know that in the beginning android platform provides us some basic views for example - TextView, ImageView, EditText, Button, ImageButton, RadioButton, etc. But, sometimes we need a whole new type of view where our prebuilt layouts and w
4 min read
Pulse Animation View in Android
In this article, we are going to see the Pulse Animation feature. This feature can be used if we are downloading something from the server. Then till the time, it is downloading we can add this feature. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using th
4 min read
Android View Evolution - PlaceHolderView
Those days are gone when ListView, GridView, RecyclerView, and Pesky Adapters ruled the arena. Say Hello to PlaceHolderView ???? Adding listeners to each RecyclerView and implementing dynamic content is a tedious operation. Let's not even talk about animation; if we have to perform it for nested Recyclers, it'll be a disaster. What if we could repl
4 min read
Animate Movements of View using SpringAnimation in Android
In Android, we can add animations to an object's movement. Instead of just updating the position of an object from one point to another, we can add some cool animations for those movements. Android provides a lot of ways that allow you to change the position of a view object on the screen with animations. But, here we are going to have a look at Sp
5 min read
RecyclerView Multiple View Types in Android with Example
We've all used RecyclerView in Android to display a list in our applications. RecyclerView is used to display emails in the Gmail app, feeds on Facebook and Instagram, and messages in WhatsApp, among other things. However, as being an Android Developer you would have come across a fact that when we define a view and the list items are only displaye
4 min read
Article Tags :