Ever thought of solving an algorithm question on Android Studio?

@madona_syombua   x  @wwcodemobile

Step 1. Download and Install Android Studio

Step 2: Let's create our first project

Step 3: Start a new project, let's use the default empty activity screen.

Step 4: Creating our first App. Language of use Java.

Once everything is set up and ready, we will go ahead and work on the following question.

Quiz 1.

Given a String "Hello" - Reverse it. So output should be "olleH"

So to briefly describe the process.

We will need the following Android Views.

 

  • EditText - where we will enter the string to be reversed.
  • Button - which will have the method "reverseString();" that is the operation
  • TextView - Which will display the reversed String, that is our output. 
  • Button clear which will clear the Edittext and Textview space
Let's go ahead and add some dependencies. Butter Knife Field and method binding for Android views
  //jake wharton
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

Display Screen - Activity_Main.Xml

<android.support.constraint.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">

    <!-- edit text used to reverse String-->
    <EditText
        android:id="@+id/reverse_edit_text"
        android:layout_width="306dp"
        android:layout_height="47dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:fontFamily="monospace"
        android:hint="@string/enter_string"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:targetApi="o" />

    <!-- submit button -->
    <Button
        android:id="@+id/reverse"
        android:layout_width="147dp"
        android:layout_height="48dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="36dp"
        android:background="@color/colorPrimary"
        android:fontFamily="monospace"
        android:text="@string/button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reverse_edit_text" />

    <!-- reverse text view -->
    <TextView
        android:id="@+id/reverse_textView"
        android:layout_width="209dp"
        android:layout_height="30dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="40dp"
        android:fontFamily="monospace"
        android:textSize="16sp"
        android:textColor="@color/colorAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reverse" />

    <!-- Clear button -->
    <Button
        android:id="@+id/clear"
        android:layout_width="144dp"
        android:layout_height="41dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="40dp"
        android:background="@color/colorPrimary"
        android:fontFamily="monospace"
        android:text="@string/clear_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reverse_textView" />

    <!-- Intro Text View-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="36dp"
        android:fontFamily="monospace"
        android:text="@string/intro"
        android:textColor="@color/colorAccent"
        android:textSize="16sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

Preview

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.reverse_edit_text)
    EditText reverseEdittext;

    @BindView(R.id.reverse)
    Button buttonReverse;

    @BindView(R.id.reverse_textView)
    TextView reverseTextView;

    @BindView(R.id.clear)
    Button clearButton;



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

        ButterKnife.bind(this);


        buttonReverse.setOnClickListener(view -> ReverseString());

        //clearing the textView and EditText
        clearButton.setOnClickListener(view -> {
            reverseEdittext.getText().clear();
            reverseTextView.setText(" ");
        });


    }


    static String reverseString(myString string, String str){

        return string.myStringFunction(str);
    }

    //Reverse string
    private void ReverseString(){

        myString reverse = (String str)->{

            int n = str.length() - 1;

            StringBuilder results = new StringBuilder();

            for(int i = n; i >= 0; i--){

                results.append(str.charAt(i));

            }

            return results.toString();
        };

        //appending the reversed String to the text view;
        reverseTextView.append(reverseString(reverse, String.valueOf(reverseEdittext.getText())));

    }

    interface myString{

        String myStringFunction(String str);
    }

}

Part One : Code Snippet - posted on GitHub https://github.com/WomenWhoCode/WWCodeMobile-solving-Java-algorithms

Using BindView to fetch the views on our XML code

 buttonReverse.setOnClickListener(view -> ReverseString());

        //clearing the textView and EditText
        clearButton.setOnClickListener(view -> {
            reverseEdittext.getText().clear();
            reverseTextView.setText(" ");
        });

we set on click listener to the buttonReverse and start the method ReverseString which reverses the entered text on the EditText.

We set on click listener to the clearButton to clear the EditText space and TextView space.

Lambda expressions are added in Java 8 and help make the code more Concise and readable. 

 

we use our interface to store the string to be reversed then loop through the length of the string  -1 , using the StringBuilder we append the characters that were reversed then later append it on the reverseTextView after we return the results to string.

static String reverseString(myString string, String str){

        return string.myStringFunction(str);
    }
    //Reverse string
    private void ReverseString(){

        myString reverse = (String str)->{

            int n = str.length() - 1;

            StringBuilder results = new StringBuilder();

            for(int i = n; i >= 0; i--){

                results.append(str.charAt(i));

            }

            return results.toString();
        };

        //appending the reversed String to the text view;
        reverseTextView.append(reverseString(reverse, String.valueOf(reverseEdittext.getText())));

    }

 Solution: 

Thank you very much for your time, all materials will be shared on Women Who Code GitHub page.

https://github.com/WomenWhoCode/WWCodeMobile-solving-Java-algorithms

 

 Find US

 

Twitter : @WWCodeMobile , @madona_syombua

Instagram : @wwcodemobile , @madona_syombua

Solving an Algorithm question on Android Studio. Women Who Code Mobile Webinar

By Madona Syombua

Solving an Algorithm question on Android Studio. Women Who Code Mobile Webinar

Taking an algorithm question and solving it on Android Studio, that is creating an app with the solution to the question. Educator: Madona Syombua | Android Engineer, Founder of Budgeting Buddy, Grow with Google Recipient, WWCode Mobile Android Lead Twitter: @madona_syombua GitHub: @madonahs

  • 1,115