Exercise - Find Palindrome

  • Ivan Žarković

Hello reader,

 

I have made simple exercises for Android beginners using Java and Android Studio.

What are we making?

We will make an application for checking if users' word is Palindrome. A palindrome is a word that writes the same backward or forward.

What do we need to know before we start?

We need to be familiar with the basics concepts of programming in Java, installed Android Studio and goodwill.

Let's dive in:

1. Firstly we will make a new Android project using a Create New Project wizard:

 

2. Then we are going to need a new Fragment. After the new project is configured, navigate to the top left of the screen on the Project section find java package with com.example.setofexcerises and right-click on it. In the dropdown menu select New>Fragment>Fragment (Blank) and name it. I've named fragment Palindrom.

 

4. Implement OnFragmentInteractionListener on MainActivity:

public class MainActivity extends AppCompatActivity implements Palindrom.OnFragmentInteractionListener{}

5. After implementing interface we can init our fragment after which we can build and run our app:

public void initHome() {
        try {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.container_body, new Palindrom());
            fragmentTransaction.commitAllowingStateLoss();
        } catch (Exception e){
            Log.d("debug", e.toString());
        }
    }

6. Styling fragment in res>layout>fragment_palindrome.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=".Fragments.Palindrom">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:id="@+id/welcomeText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:text="Is your word palindrome?"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="-96dp"
        android:layout_marginTop="36dp"/>

    <EditText
        android:id="@+id/word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:ems="10"
        android:gravity="center_horizontal"
        android:inputType="textPersonName"
        android:hint="Word"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/checkWord"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/word"
        android:layout_margin="50dp"
        android:text="check"></Button>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@+id/checkWord">

        <TextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/checkWord"
            android:text="Your word is: "
        ></TextView>

        <TextView
            android:id="@+id/isPalindromLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/checkWord"
            android:text=""
            android:textSize="16sp"
            android:layout_marginLeft="8dp"
            android:textColor="@color/colorPrimaryDark"
            ></TextView>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

7. Initialize button, EditText, and TextView inside of Fragment and in onCreateView method:

View v = inflater.inflate(R.layout.fragment_palindrom, container, false);

EditText etWord = v.findViewById(R.id.checkWord);
Button bCheck = v.findViewById(R.id.word);
TextView tvIsPalindrome = v.findViewById(R.id.isPalindromLabel);

And attach onClickListener to bCheck button like this:

bCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sWord = etWord.getText().toString();
                if(isPalindrome(sWord)){
                    tvIsPalindrome.setText("PALINDROME");
                }else {
                    tvIsPalindrome.setText("IS NOT PALINDROME");
                }
            }
        });

8. Lastly, make isPalindrome() method:

private boolean isPalindrome(String word) {
        int i1 = 0;
        int i2 = word.length() - 1;
        while (i2 > i1) {
            if (word.charAt(i1) != word.charAt(i2)) {
                return false;
            }
            ++i1;
            --i2;
        }
        return true;
    }

 

Our exercise is done:

 

Thank you for reading!

Article Subscription