Open In App

How to Build a Palindrome Checker App in Android Studio?

Last Updated : 21 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be building a Palindrome Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered word is Palindrome or not, if the entered word is a Palindrome then a toast will be displayed having the message “Entered word is Palindrome” otherwise Toast’s message will be “Entered word is not a Palindrome”.

Step by Step Implementation

Step 1: Create a New Project  

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Before going to the coding section first you have to do some pre-task

Add Colors: Add dark green and white color in your colors.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="white">#DFFAF7</color>
    <color name="DarkGreen">#216A33</color>
</resources>


Change Theme: Change the theme to NoActionBar in the styles.xml file.

XML




<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>


Make Gradient Background:  create a new Drawable Resource file and name it “background.xml” inside the drawable directory and write the below code in it. Please refer to gradient background in android.

XML




<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  
    <item>
        <shape>
            <gradient android:angle="90" 
                android:startColor="#ABE89E"
                android:centerColor="#388E3C"
                android:endColor="#00796B"
                />
        </shape>
    </item>
</selector>


Step 3: Working with the activity_main.xml file

This file contains a TextView which displays the title of our app, an EditText view which takes the input from the user, and a Button view, on clicking which the app will check whether the entered word is Palindrome or not. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GFG Palindrome Checker"
        android:textColor="@color/white"
        android:textSize="35sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.548"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="128dp"
        android:gravity="center_vertical"
        android:hint="Enter the Word"
        android:textColor="@color/white"
        android:textColorHint="@color/white"
        android:textSize="40sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.659"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
      
    <Button
        android:id="@+id/ButtonCheck"
        android:layout_width="128dp"
        android:layout_height="58dp"
        android:background="#fff"
        android:text="Check"
        android:textColor="@color/white"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        app:layout_constraintVertical_bias="0.095" />
  
</androidx.constraintlayout.widget.ConstraintLayout>


Step 4: Working with the MainActivity.kt file

Inside the class MainActivity, we will create a function “ispalindrome()” which will take a string value as a parameter and returns a Boolean value, it will return True if the string is Palindrome and if the string is not a Palindrome it will return False. Now, inside the onCreate function, we will call a setOnClickListener method on ButtonCheck, inside it, we pass the text value of editText to “ispalindrome()” function as an argument, if the returned value is True, we will display a Toast having message “Entered word is Palindrome” and if the returned value is False, we will display a Toast having the message “Entered word is not a Palindrome”. Below is the code for the MainActivity.kt file.

Kotlin




import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        ButtonCheck.setOnClickListener {
            val text = editText.text.toString()
            if (ispalindrome(text)) {
                Toast.makeText(this, "Entered word is palindrome ", Toast.LENGTH_SHORT).show()
  
            } else {
  
                Toast.makeText(this, "Entered word is not a Palindrome", Toast.LENGTH_SHORT).show()
  
            }
        }
    }
  
    private fun ispalindrome(text: String): Boolean {
        val reverseString = text.reversed().toString()
        return text.equals(reverseString, ignoreCase = true)
    }
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads