Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Double Tap Buttons
2.
Custom Buttons
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Double Tap And Custom Buttons

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Double Tap Buttons

Double Tap buttons are those buttons that only respond when they are clicked on twice. This section will look at an example to understand how to create Double Tap buttons using Kotlin in Android development.

activity_main.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:layout_height="match_parent"
tools:context=".mainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Double Tap"
/>
</RelativeLayout>

In the XML file, we add a Button to detect the Double Tap.

 

mainActivity.kt

package app.com.doubleTap
import android.widget.TextView
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import android.widget.Toast
class mainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dBtn1 = findViewById<TextView>(R.id.btn1)
dBtn1.setOnClickListener(object : DoubleClickListener() {
override fun onDoubleClick(v: View?) {
Toast.makeText(applicationContext,"Double Click",Toast.LENGTH_SHORT).show()
}
})
}
abstract class DoubleClickListener : View.OnClickListener {
var lastClickTime: Long = 0
override fun onClick(v: View?) {
val clickTime = System.currentTimeMillis()
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
onDoubleClick(v)
}
lastClickTime = clickTime
}


abstract fun onDoubleClick(v: View?)


companion object {
private const val DOUBLE_CLICK_TIME_DELTA: Long = 300
}
}
}

We created a Kotlin file in the above code and added an abstract class to the Kotlin file. We then called the onClickListener function to look for mouse clicks.

Custom Buttons

In this section, we will discuss creating custom buttons for Android development. Developers can create custom buttons of various shapes, sizes, and colors. Now we will look at an example to create custom buttons using Java.

rectangle.xml

<?xml version="1.0" encoding="UTF-8"?> -  
<shape xmlns:android="http://schemas.android.com/apk/res/android">  
    <stroke android:color="#000000" android:width="3dp" />  
    <solid android:color="#ffffff" />   
</shape>  

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#80121a" />
    <corners android:radius="999dp"/>
</shape>

First, we will create the rectangle.xml file in res>drawable>rectangle.xml. In the XML file, we specify the shape and color of the button.

 

activity_main.xml

<?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"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/customButton"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/rectangle"
        android:text="Rectangle button"/>
    <Button
        android:id="@+id/circle"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/customButton"
        android:layout_marginTop="24sp"
        android:background="@drawable/circle"
        android:text="My Custom button"/>
</RelativeLayout>

Second, we create the main_activity.xml file in the res>layout>main_activity.xml. We add the custom button to the interface.

 

MainActivity.java

package app.com.custombutton;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

Third, we create the MainActivity.java file, and we set the app's content as activity_main.xml using the setContentView function.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

  1. What are Double Tap Buttons?
    The buttons that only respond to being clicked twice are known as Double Click buttons. These buttons don’t respond to being clicked once.
     
  2. What is Kotlin?
    Kotlin is a general-purpose, cross-platform programming language. It is used for Android development. It is designed such that it interoperates efficiently with Java.
     
  3. What is Java?
    Java is an object-oriented, class-based, high-level programming language. It is based on the concept of WORA (write once use anywhere) for developer ease.
     
  4. What are Custom Buttons?
    Developers can create custom buttons using Java or Kotlin. Developers can create buttons of various sizes, shapes, and colors.
     
  5.  What can I do with Android Studio?
    Android Studio is a unified platform for Application Development. It provides structured code modules for independent testing, debugging, and building.

Key Takeaways

This blog covered all the necessary points about Double Tap and Custom Buttons in Android Development. We further looked at the codes to learn how to implement these buttons.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Previous article
Floating Action Button
Next article
Styles and Themes in Android
Live masterclass