[Introduction to Android application development] Let's make a counter

Because it is an Android application development beginner's edition

Counter app

** The gateway to app development ** is a counter app (appropriate) Including confirmation of one's own understanding We will create a counter app that does not use global variables. The requirements are as follows.

  1. ** Do not use global variables **
  2. ** Suppress the basics of Android application development **
  3. ** Manufacture a counter that can be + and

Let's make

As prerequisite knowledge

  1. ** Use Android Studio **
  2. ** Know how to create a project **
  3. ** Some programming experience **

For people who have been touching JS until now but want to make a native application

First of all, it is a function, but for the time being it is in the res / layout directory Play with the XML file with the GUI (you can write it in code). Like this. test.png

This is the code

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:id="@+id/MainView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:context="com.example.name.app_test_android.MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="67dp"
        android:layout_height="35dp"
        android:layout_centerHorizontal="true"
        android:layout_marginEnd="134dp"
        android:layout_marginStart="183dp"
        android:layout_marginTop="105dp"
        android:text="0"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        style="@android:style/Widget.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="108dp"
        android:layout_marginStart="54dp"
        android:layout_marginTop="355dp"
        android:onClick="plus" //Additional points
        android:text="+"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        style="@android:style/Widget.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="108dp"
        android:layout_marginEnd="56dp"
        android:layout_marginTop="355dp"
        android:onClick="minus" //Additional points
        android:text="-"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

By arranging Buttons and TextViews appropriately in the GUI, A smart IDE will add object information to XML without permission.

However, this time to add an event. android: onClick = "plus or minus" is added, Other than that, you can play with the GUI. Next, let's write the definition of the method to be called in the Click event. From here, it may be difficult to understand without programming.

By not using global variables, Basically, it parses the value of TextView. ** View-related Value acquisition seems to be a little quirky **, It seems that the TextValue itself of the acquired TextView is converted by toString ().

MainActivity.java


package com.example.name.app_test_android;

import android.support.v7.app.AppCompatActivity;
import java.lang.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class MainActivity extends Activity {

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

    public void plus(View view) {
        TextView set_view = (TextView) findViewById(R.id.textView1);
        //Convert Text Value to String type
        String countView = new String((set_view.getText()).toString());
        if(isNumber(countView) == true) {
            int mCount = Integer.parseInt(countView);
            mCount++;
            set_view.setText(String.valueOf(mCount));
        }
    }
    public void minus(View view) {
        TextView set_view = (TextView) findViewById(R.id.textView1);
        String countView = new String((set_view.getText()).toString());

        if(isNumber(countView) == true) {
            int mCount = Integer.parseInt(countView);
            mCount--;
            set_view.setText(String.valueOf(mCount));
        }
    }
    //String integer judgment
    private static boolean isNumber(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

Click here for reference site https://qiita.com/hys-rabbit/items/1251e86d9d22507d309a https://qiita.com/gabu/items/6ed716bb9af7cc3c26a3

Recommended Posts

[Introduction to Android application development] Let's make a counter
Introduction to Android application development
A newcomer tries to summarize the Android view (beginner Android application development)
A memorandum for android application development beginners
I tried to make a simple face recognition Android application using OpenCV
Introduction to Android Layout
Android application development preparation 7/15
Introduction to Keycloak development
[Android] I want to make QA easier ... That's right! Let's make a debug menu!
Learn while making a WEB server Introduction to WEB application development from the basics
[Introduction] Try to create a Ruby on Rails application
App development beginners tried to make an Android calculator app
Let's make a calculator application in Java ~ Display the application window
Try to introduce OpenCV to Android application
Let's make a book management web application with Spring Boot part1
Try to make a simple callback
Android Development-Try to display a dialog-
How to make a Java container
Let's make a book management web application with Spring Boot part3
How to make a JDBC driver
Try to make a cross-platform application with JRuby (jar file generation)
Let's make a book management web application with Spring Boot part2
Notes for Android application development beginners
I tried to make an Android application with MVC now (Java)
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
Try to make a peepable iterator
How to make a Java array
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
[Android] How to make Dialog Fragment
Java beginner tried to make a simple web application using Spring Boot
[Android application development] How to display in full screen (notification bar hidden)
I tried to make a talk application in Java using AI "A3RT"
Introduction to Java development environment & Spring Boot application created with VS Code
I want to develop a web application!
How to make a Java calendar Summary
Introduction to Slay the Spire Mod Development (1) Introduction
Let's make a Christmas card with Processing!
[Introduction to Java] How to write a Java program
Let's create a Java development environment (updating)
How to make a Discord bot (Java)
Let's make a smart home with Ruby!
Android Easily give a "press" to a button
Introduction to Docker / Kubernetes Practical Container Development
[Introduction to JSP + Servlet] A little animation ♬
[Java] Let's make a DB access library!
Let's migrate to make Java more comfortable
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I tried to make a machine learning application with Dash (+ Docker) part3 ~ Practice ~
Let's make a calculator application with Java ~ Create a display area in the window
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
[Android] I tried to make a material list screen with ListView + Bottom Sheet
Let's get started with Java-Create a development environment ②
A note about adding Junit 4 to Android Studio
[Android] Inherit ImageView to create a new class
Introduction to Recursive Functions: What is a Recursive Function?
[Android] How to convert a character string to resourceId
Make a margin to the left of the TextField
Connect to Aurora (MySQL) from a Java application
I did Java to make (a == 1 && a == 2 && a == 3) always true