Build a Palindrome String Checker Android App in Android Studio and Java Full Project For Beginners

 

 

package com.programmerworld.palindromeapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textViewStatus;
    private EditText editTextInputWord;

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

        textViewStatus = findViewById(R.id.textViewStatus);
        editTextInputWord = findViewById(R.id.editTextTextInputWord);
    }

    public void buttonCheckPalindrome(View view){
        char[] charInput = editTextInputWord.getText().toString().toCharArray();
        int intLength = charInput.length;
        boolean isPalindrome = true;

        for (int i=0; i<intLength/2; i++){
            if (charInput[i] != charInput[intLength-1-i]){
                isPalindrome = false;
                break;
            }
        }
        if (isPalindrome){
            textViewStatus.setText("Palindrome");
        }
        else {
            textViewStatus.setText("NOT Palindrome");
        }
    }
}

 

 

<?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">

    <TextView
        android:id="@+id/textViewStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.47" />

    <EditText
        android:id="@+id/editTextTextInputWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="87dp"
        android:layout_marginTop="39dp"
        android:ems="10"
        android:hint="@string/type_your_word_here"
        android:inputType="textPersonName"
        android:textAlignment="center"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="106dp"
        android:layout_marginTop="50dp"
        android:onClick="buttonCheckPalindrome"
        android:text="@string/check_palindrome"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editTextTextInputWord" />

</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply