The concept here is the long click event in any widget in any android. For example long click events in:
- ListView
- GridView
- RecyclerView
Feel free to post any example.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Oclemy
Android Custom ListView – LongClick Events
This is an android tutorial to explore how to handle longclick events in a custom listview with images and text.
Let’s go.
1. Create Basic Activity Project
Basic activity will have a toolbar and floating action button already added in the layout
Normally two layouts get generated with this option:
In this example I used a basic activity.
The activity will automatically be registered in the android_manifest.xml. Android Activities are components and normally need to be registered as an application component.
If you’ve created yours manually then register it inside the
<application>...<application>
as following, replacing theMainActivity
with your activity name:You can see that one action and category are specified as intent filters. The category makes our MainActivity as launcher activity. Launcher activities get executed first when th android app is run.
Advantage of Creating Basic Activity project
You can optionally choose empty activity over basic activity for this project.
However basic activity has the following advantages:
Generated Project Structure
AndroidStudio will generate for you a project with default configurations via a set of files and directories.
Here are the most important of them:
build/
R.java
file.R.java
file normally holds the references to application resources.libs/
src/main/
src/main/java/
src/main/res/
src/main/res/drawable/
src/main/res/layout/
src/main/res/menu/
src/main/res/values/
AndroidManifest.xml
build.gradle
2. Dependencies
We really have no external dependencies. Instead we simply specify a couple of support library dependencies.
3. Create User Interface
User interfaces are typically created in android using XML layouts as opposed by direct java coding.
This is an example fo declarative programming.
Advantages of Using XML over Java
Here are our layouts for this project:
(a). activity_main.xml
Here are some of the widgets, views and viewgroups that get employed”
android.support.design.widget
android.support.design.widget
<android.support.v7.widget
android.support.design.widget
(b). content_main.xml
Let’s add our ListView here.
(c). model.xml
This layout will define the custom row model for our ListView.
In this case our ListView will have images and text.
Let’s now come to our Java classes:
4. Movie.java
Our data object.
5. ItemLongClickListener.java
Our long click listener interface.
6. MyViewHolder.java
Will hold our widgets to be recycled.
7. CustomAdapter.java
Will inflate our
model.xml
and bind data to resultant views.8. MainActivity.java
Our Main activity.
Download Code
[/sociallocker]
Best Regards,
Oclemy.