Build a Palindrome String Checker Android App in Android Studio using Java

In this tutorial, I am going to teach you “How to Build a Palindrome String Checker Android App in Android Studio using Java“. Okay, let’s move on to the article.

Build a Palindrome String Checker Android App in Android Studio and Java

Build a Palindrome String Checker Android App in Android Studio using Java

String class doesn’t have reverse() method so I have used StringBuilder class to reverse the string using the reverse() method. After that, I compared the orginal string with reversed string using equals() method.

MainActivity.java

package com.codingdiksha.palindrome;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText string;
    TextView result;
    Button button;

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

        string = findViewById(R.id.string);
        result = findViewById(R.id.textView);
        button = findViewById(R.id.button);

        button.setOnClickListener(view -> {
            String stringValue = string.getText().toString();
            checkPalindrome(stringValue);
        });
    }

    @SuppressLint("SetTextI18n")
    private void checkPalindrome(String stringValue) {
        String reversed = new StringBuilder(stringValue).reverse().toString();
        if(stringValue.equals(reversed))
            {
                result.setText("Palindrome");
            }
        else
            {
              result.setText("Not Palindrome");
            }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <EditText
        android:id="@+id/string"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:minHeight="48dp"
        android:hint="Enter the String"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.192" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Palindrome"
        app:layout_constraintStart_toStartOf="@+id/string"
        app:layout_constraintEnd_toEndOf="@+id/string"
        app:layout_constraintTop_toBottomOf="@+id/string"
        android:layout_marginTop="30dp"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        app:layout_constraintStart_toStartOf="@+id/button"
        app:layout_constraintEnd_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button"
        android:layout_marginTop="30dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Output

Final Words

I hope this article helps you to “Build a Palindrome String Checker Android App in Android Studio using Java“. If you face any issues please let me know via the comment section. I will help you. Share this article with other Android developers via social networks.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment