Sunday, December 31, 2017

Palindrome in Android

Before the new year will arrive I would like to post my 300 article posting this 2017 I called this program Palindrome in Android which contains two program. The first program in String Palindrome which ask the user to give a string or a word and then our program will check if the given word or string is a Palindrome or not. The second one is number palindrome which ask the user to give a number and then our program will determine and check if the given number is palindrome or not. 

In addition I include about the program page which display about me as the developer of this application. I also added error checking code to avoid our application will crash is blank or empty spaces will be given by our user. I learned a lot in this application that I wrote and I want it to share to other programmers. I hope you will find my work useful.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.





Main Menu Page


Checking Empty Values Page


String Palindrome Page




Number Palindrome Page




About This Program Page


Quit Program Page


Genymotion the Android Emulation that I use in writing this application.



Program Listing

About.java

package com.blogspot.jakerpomperada.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class About extends AppCompatActivity {

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

MainActivity.java

package com.blogspot.jakerpomperada.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;

public class MainActivity extends AppCompatActivity {

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


    public void string_palindrome(View v) {
        startActivity(new Intent(this, String_Palindrome.class));

    }
    public void number_palindrome(View v) {
        startActivity(new Intent(this, Number_Palindrome.class));

    }


    public void about_this_program(View v) {
        startActivity(new Intent(this, About.class));

   }
    public void quit(View v) {
        // Toast.makeText((getApplicationContext()), "This is my Toast message!",Toast.LENGTH_LONG).show();
        new AlertDialog.Builder(this)
                .setMessage("Quit This Program?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("No", null)
                .show();
    }
}


String_Palindrome.java

package com.blogspot.jakerpomperada.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import java.util.*;


public class String_Palindrome extends AppCompatActivity {
 int given_number;
    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_string__palindrome);

    }

   public void clear_edit_text(View v) {
       final EditText value_given = (EditText) findViewById(R.id.editValueGiven);
       final TextView result = (TextView) findViewById(R.id.lblresult);
        result.setText("");
        value_given.setText("");
        value_given.requestFocus();
    }


    public void check_string(View v) {
       final EditText value_given = (EditText) findViewById(R.id.editValueGiven);
       final TextView result = (TextView) findViewById(R.id.lblresult);

      String GetEditText = value_given.getText().toString();

        if(TextUtils.isEmpty(GetEditText)){

            Toast.makeText(this, "Cannot Be Empty. Kindly provide a string or a word.", Toast.LENGTH_SHORT).show();
            result.setText("");
            value_given.setText("");
            value_given.requestFocus();

        } else        {
            try {
                }catch (Exception  e){
                Toast.makeText(this, "Not a String", Toast.LENGTH_SHORT).show();
            }

            String  reverse = "";

            int length = GetEditText.trim().toLowerCase().length();

            for ( int i = length - 1; i >= 0; i-- )
                reverse = reverse + GetEditText.charAt(i);

            if (GetEditText.trim().toLowerCase().equals(reverse.trim().toLowerCase()))   {
                result.setText(GetEditText.toUpperCase() + " is a Palindrome String. ");
            } else {
                result.setText(GetEditText.toUpperCase() + " is not a Palindrome String. ");
            }

        }


        }

    }

Number_Palindrome.java

package com.blogspot.jakerpomperada.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.text.TextUtils;



public class Number_Palindrome extends AppCompatActivity {
 int given_number;
    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number__palindrome);

    }

   public void clear_edit_text(View v) {
       final EditText value_given = (EditText) findViewById(R.id.editValueGiven);
       final TextView result = (TextView) findViewById(R.id.lblresult);
        result.setText("");
        value_given.setText("");
        value_given.requestFocus();
    }


    public void check_number(View v) {
       final EditText value_given = (EditText) findViewById(R.id.editValueGiven);
       final TextView result = (TextView) findViewById(R.id.lblresult);

      String GetEditText = value_given.getText().toString();

        if(TextUtils.isEmpty(GetEditText)){

            Toast.makeText(this, "Cannot Be Empty. Kindly provide a number.", Toast.LENGTH_SHORT).show();
            result.setText("");
            value_given.setText("");
            value_given.requestFocus();

        } else        {
            try {
                given_number = Integer.parseInt(value_given.getText().toString());
            }catch (NumberFormatException e){
                Toast.makeText(this, "Not a number", Toast.LENGTH_SHORT).show();
            }

            int i = 0, reverse = 0;

            int q = given_number;

            for (i = 0; i <= given_number; i++) {
                reverse = reverse * 10;
                reverse = reverse + given_number % 10;
                given_number = given_number / 10;
                i = 0;
            }

            if (reverse == q) {
                result.setText(String.valueOf(q) + " is a Palindrome Number. ");
            } else {
                result.setText(String.valueOf(q) + " is not a Palindrome Number. ");
            }

        }


        }

    }


activity_about.xml

<?xml version="1.0" encoding="utf-8"?><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"    android:background="@android:color/holo_green_light"    tools:background="@android:color/holo_green_light"    tools:context="com.blogspot.jakerpomperada.myapplication.About">

    <RelativeLayout        android:layout_width="368dp"        android:layout_height="495dp"        tools:layout_editor_absoluteX="8dp"        tools:layout_editor_absoluteY="8dp">

        <TextView            android:id="@+id/textView3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignParentStart="true"            android:layout_marginBottom="40dp"            android:layout_marginStart="12dp"            android:gravity="left"            android:lines="10"            android:maxLines="17"            android:text="\nThis program was designed and developed by Mr. Jake R. Pomperada. I live in Bacolod City, Negros Occidental Philippines. \n\nYou can email me at the following email address jakerpomperada@yahoo.com and jakerpomperada@gmail.com  \n\nThank you for using this software."            android:textAppearance="@style/TextAppearance.AppCompat.Medium"            android:textColor="@android:color/background_light"            android:textStyle="bold" />

        <ImageView            android:id="@+id/imageView3"            android:layout_width="193dp"            android:layout_height="188dp"            android:layout_alignParentTop="true"            android:layout_centerHorizontal="true"            android:layout_marginTop="50dp"            app:srcCompat="@drawable/me"            tools:layout_editor_absoluteX="102dp"            tools:layout_editor_absoluteY="55dp" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?><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"    android:background="@android:color/holo_green_light"    tools:context="com.blogspot.jakerpomperada.myapplication.MainActivity">

    <LinearLayout        android:layout_width="368dp"        android:layout_height="495dp"        android:orientation="vertical"        tools:layout_editor_absoluteX="8dp"        tools:layout_editor_absoluteY="8dp">

        <Button            android:id="@+id/button21"            android:onClick="string_palindrome"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="30dp"            android:background="@android:color/background_light"            android:padding="20dip"            android:text="String Palindrome"            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />

        <Button            android:id="@+id/button22"            android:onClick="number_palindrome"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="30dp"            android:background="@android:color/background_light"            android:padding="20dip"            android:text="Number Palindrome"            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />

        <Button            android:id="@+id/button22"            android:onClick="about_this_program"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="30dp"            android:background="@android:color/background_light"            android:padding="20dip"            android:text="About This Program"            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />


        <Button            android:id="@+id/button22"            android:onClick="quit"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="30dp"            android:background="@android:color/background_light"            android:elevation="0dp"            android:padding="20dip"            android:text="Quit This Program"            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>


activity_number_palindrome.xml


<?xml version="1.0" encoding="utf-8"?><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"    android:background="@android:color/holo_green_light"    tools:context="com.blogspot.jakerpomperada.myapplication.Number_Palindrome">

    <RelativeLayout        android:layout_width="368dp"        android:layout_height="499dp"        tools:layout_editor_absoluteX="8dp"        tools:layout_editor_absoluteY="0dp">

        <TextView            android:id="@+id/textView2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentTop="true"            android:layout_alignStart="@+id/editValueGiven"            android:layout_marginTop="25dp"            android:text="Enter a Number"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            android:textColor="@android:color/background_light" />

        <EditText            android:id="@+id/editValueGiven"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@+id/textView2"            android:layout_centerHorizontal="true"            android:layout_marginTop="27dp"            android:ems="10"            android:inputType="number"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            android:textColor="@android:color/background_light" />

        <Button            android:id="@+id/button"            android:layout_width="279dp"            android:layout_height="wrap_content"            android:layout_alignEnd="@+id/editValueGiven"            android:layout_below="@+id/editValueGiven"            android:layout_marginTop="125dp"            android:background="@android:color/background_light"            android:onClick="check_number"            android:text="Check Number"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            tools:layout_editor_absoluteX="53dp"            tools:layout_editor_absoluteY="259dp" />

        <Button            android:id="@+id/button2"            android:layout_width="281dp"            android:layout_height="51dp"            android:layout_alignStart="@+id/button"            android:layout_below="@+id/button"            android:layout_marginTop="60dp"            android:background="@android:color/background_light"            android:onClick="clear_edit_text"            android:text="Clear"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            tools:layout_editor_absoluteX="78dp"            tools:layout_editor_absoluteY="378dp" />

        <TextView            android:id="@+id/lblresult"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button"            android:layout_below="@+id/editValueGiven"            android:layout_marginTop="41dp"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            android:textColor="@android:color/background_light" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>


activity_string_palindrome.xml


<?xml version="1.0" encoding="utf-8"?><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"    android:background="@android:color/holo_green_light"    tools:context="com.blogspot.jakerpomperada.myapplication.Number_Palindrome">

    <RelativeLayout        android:layout_width="368dp"        android:layout_height="499dp"        tools:layout_editor_absoluteX="8dp"        tools:layout_editor_absoluteY="0dp">

        <TextView            android:id="@+id/textView2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentTop="true"            android:layout_alignStart="@+id/editValueGiven"            android:layout_marginTop="25dp"            android:text="Enter a String"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            android:textColor="@android:color/background_light" />

        <EditText            android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"            android:inputType="textFilter"            android:id="@+id/editValueGiven"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@+id/textView2"            android:layout_centerHorizontal="true"            android:layout_marginTop="27dp"            android:ems="10"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            android:textColor="@android:color/background_light" />

        <Button            android:id="@+id/button"            android:layout_width="279dp"            android:layout_height="wrap_content"            android:layout_alignEnd="@+id/editValueGiven"            android:layout_below="@+id/editValueGiven"            android:layout_marginTop="125dp"            android:background="@android:color/background_light"            android:onClick="check_string"            android:text="Check String"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            tools:layout_editor_absoluteX="53dp"            tools:layout_editor_absoluteY="259dp" />

        <Button            android:id="@+id/button2"            android:layout_width="281dp"            android:layout_height="51dp"            android:layout_alignStart="@+id/button"            android:layout_below="@+id/button"            android:layout_marginTop="60dp"            android:background="@android:color/background_light"            android:onClick="clear_edit_text"            android:text="Clear"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            tools:layout_editor_absoluteX="78dp"            tools:layout_editor_absoluteY="378dp" />

        <TextView            android:id="@+id/lblresult"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button"            android:layout_below="@+id/editValueGiven"            android:layout_marginTop="41dp"            android:textAppearance="@style/TextAppearance.AppCompat.Headline"            android:textColor="@android:color/background_light" />

    </RelativeLayout>

</android.support.constraint.ConstraintLayout>



DOWNLOAD SOURCE CODE HERE


DOWNLOAD APK FILE HERE



Wednesday, December 27, 2017

Multiple Button Events in Android

While learning Android programming one of the problem that I encounter is how to assign multiple buttons in an activity and event when I click a particular button. Here is the solution that I wrote I called this application multiple button events in Android. I hope you will find my work useful.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.









Sample Program Output



Program Listing

MainActivity.java

package com.example.jacob.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements        View.OnClickListener {

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


        Button one = (Button) findViewById(R.id.button);
        one.setOnClickListener(this);
        Button two = (Button) findViewById(R.id.button2);
        two.setOnClickListener(this);
        Button three = (Button) findViewById(R.id.button3);
        three.setOnClickListener(this);
        Button four = (Button) findViewById(R.id.button4);
        four.setOnClickListener(this);
        Button five = (Button) findViewById(R.id.button5);
        five.setOnClickListener(this);
    }
        @Override
        public void onClick (View v){

            switch (v.getId()) {

                case R.id.button:
                    Toast.makeText(MainActivity.this, "You have clicked Button 1", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button2:
                    Toast.makeText(MainActivity.this, "You have clicked Button 2", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button3:
                    Toast.makeText(MainActivity.this, "You have clicked Button 3", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button4:
                    Toast.makeText(MainActivity.this, "You have clicked Button 4", Toast.LENGTH_LONG).show();
                    break;

                case R.id.button5:
                    Toast.makeText(MainActivity.this, "You have clicked Button 5", Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }



        }

    }


activity_main.xml

<?xml version="1.0" encoding="utf-8"?><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="com.example.jacob.myapplication.MainActivity">

    <RelativeLayout        android:layout_width="440dp"        android:layout_height="587dp"        tools:layout_editor_absoluteX="16dp"        tools:layout_editor_absoluteY="0dp">

        <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentStart="true"            android:layout_alignParentTop="true"            android:layout_marginStart="80dp"            android:layout_marginTop="44dp"            android:text="Button 1" />

        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button"            android:layout_below="@+id/button"            android:layout_marginTop="61dp"            android:text="Button 2" />

        <Button            android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button2"            android:layout_below="@+id/button2"            android:layout_marginTop="55dp"            android:text="Button 3" />

        <Button            android:id="@+id/button4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignStart="@+id/button3"            android:layout_below="@+id/button3"            android:layout_marginTop="57dp"            android:text="Button 4"            tools:layout_editor_absoluteX="67dp"            tools:layout_editor_absoluteY="0dp" />

        <Button            android:id="@+id/button5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignEnd="@+id/button4"            android:layout_below="@+id/button4"            android:layout_marginTop="44dp"            android:text="Button 5" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>


strings.xml

<resources>
    <string name="app_name">Multiple Buttons in Android</string>
</resources>




DOWNLOAD SOURCE CODE HERE