About the basics of Android development

Introduction

Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well. I received a request this time, so I will answer the request and write about the basics of Android development in an easy-to-understand manner.

Target person

--A person who can write Java somehow. --People who want to develop Android. --Beginner in Android development.

Do not write this time

--About the installation of Android Studio.

What to make

When you enter a name and press the button, the one entered in the display area below and the character string "-san, nice" are displayed.

screen

I will make a screen with an xml file

activity_button_click_sample.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_name" />

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>

    <Button
        android:id="@+id/btClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bt_click"/>

    <TextView
        android:id="@+id/tvOutput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:text=""
        android:textSize="25dp"/>

</LinearLayout>

Commentary

tag

--LinearLayout: Layout used when arranging parts vertically or horizontally. --TextView: Create a display area --EditText: Create an input area --Button: Make a button

attribute

Common attributes

①android:layout_width="〜〜〜"
②android:layout_height="〜〜〜"

① Width attribute ② Height attribute


A value that goes into ~ ~ ~

--match_parent: Expand to fill the screen. --wrap_content: It will be displayed in an appropriate size.

LinearLayout attributes

android:orientation="〜〜〜"


A value that goes into ~ ~ ~

--vertical: Arrange vertically --horizontal: side by side

Attributes used other than LinearLayout

android:text="value value"
* Usually "string".Specify the content to be output in "xml" and read the image from there

Example


android:text="@string/bt_click"
android:id="@+id/ID (name) for acquiring parts in the activity"
→ R value: The file in the res folder and the "" of that file@ + id/Since the value of "" is a management target, it is an int type integer that identifies the file or value.
It is automatically generated.

Example


android:id="@+id/tvOutput"

Attributes used in EditText

android:inputType="〜〜〜"

~ ~ ~: Type of inputType

** ** none Cannot be entered. text Ordinary text. textCapCharacters When typing in all uppercase letters. textCapWords When entering the beginning of a word in uppercase. textCapSentences When entering the beginning of a sentence in uppercase. textAutoCorrect To automatically correct the character input. textAutoComplete When completing characters. textMultiLine When entering multiple lines of characters. textImeMultiLine When multiple input is not allowed during normal character input and multiple line input is set by IME. When entering the textUri URL. textEmailAddress When entering an email address. textEmailSubject When entering the subject of an email. textShortMessage When entering a short message. textLongMessage When entering a long message. textPersonName When entering a person's name. textPostalAddress When entering an address. textPassword When entering a password. textVisiblePassword When entering the password characters by showing them. textWebEditText When entering HTML. textFilter Enter characters filtered by other data. textPhonetic When entering phonetic symbols. number When entering a numerical value. numberSigned When entering a signed number. numberDecimal When entering decimal numbers. phone When entering a phone number. datetime When entering a date time. date When entering a date. time When entering the time.

Activity (processing)

I will write it in java.

ButtonClickSampleActivity.java


public class ButtonClickSampleActivity extends AppCompatActivity {

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

        Button button = findViewById(R.id.btClick);
        ButtonClickListener listener = new ButtonClickListener();
        button.setOnClickListener(listener);
    }

    /**
     *A member class that describes what happens when a button is pressed
     */
    private class ButtonClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            EditText input = findViewById(R.id.etName);
            String inputStr = input.getText().toString();

            TextView output = findViewById(R.id.tvOutput);
            output.setText(inputStr + "San, nice! !!" );
        }
    }
}

Commentary

Inherit AppCompatActivity.

onCreate () method

Acquisition of screen parts

Use ** findViewById () ** and specify the R value (ID attached to the part) of the part as an argument.

EditText input = findViewById(R.id.etName)

Get a string

Use ** getText (). ToString () **.

input.getString().toString()

Embed a string

Use ** setText ("embed string") **.

TextView output = findViewById(R.id.tvOutput);//Parts acquisition
output.setText(inputStr + "San, nice! !!" );

Event listener

--Event: The user performs something on the screen. --Event handler: Processing performed in response to an event. --Listener: Validate this event.

Listener setting procedure

(1) Create a listener class corresponding to each event as a member class. (2) Write the process in the method defined in the interface. ③ Set the listener by "new" the listener class.

Example) (button click listener)

Button button = findViewById(R.id.btClick);
ButtonClickListener listener = new ButtonClickListener();
button.setOnClickListener(listener);

private class ButtonClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            EditText input = findViewById(R.id.etName);
            String inputStr = input.getText().toString();

            TextView output = findViewById(R.id.tvOutput);
            output.setText(inputStr + "San, nice! !!" );
        }
}

You can also write using anonymous functions (reference books are often written in this way). What they are doing is the same.

findViewById(R.id.btClick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText input = findViewById(R.id.etName);
                String inputStr = input.getText().toString();

                TextView output = findViewById(R.id.tvOutput);
                output.setText(inputStr + "San, nice! !!" );
            }
});

that's all. It was the basis of Android development. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.

Recommended Posts

About the basics of Android development
Understand the basics of Android Audio Record
About the handling of Null
Docker monitoring-explaining the basics of basics-
About the description of Docker-compose.yml
Understand the basics of docker
The basics of Swift's TableView
About the Android life cycle
I saw the list view of Android development collectively
About the behavior of ruby Hash # ==
About truncation by the number of bytes of String on Android
The basics of SpringBoot + MyBatis + MySQL
About the current development environment (Java 8)
About the role of the initialize method
Think about the 7 rules of Optional
Summary about the introduction of Device
About the log level of java.util.logging.Logger
The basics of the process of making a call with an Android app
I thought about the strategy of introducing Combine in iOS development
Preparation for Android development of POCOPHONE f1
About the version of Docker's Node.js image
What is testing? ・ About the importance of testing
Now, I've summarized the basics of RecyclerView
About the operation of next () and nextLine ()
[day: 5] I summarized the basics of Java
About the initial display of Spring Framework
Looking back on the basics of Java
About the error message Invalid redeclaration of'***'
About the treatment of BigDecimal (with reflection)
About the number of threads of Completable Future
About the mechanism of the Web and HTTP
Basics of Ruby
About the method
[Ruby basics] About the role of true and break in the while statement
Android Development App_Preparation
About the package
Android development, how to check null in the value of JSON object
What is JSP? ~ Let's know the basics of JSP !! ~
[Ruby] Summary of class definitions. Master the basics.
Think about the combination of Servlet and Ajax
[Android] [Java] Manage the state of CheckBox of ListView
About the official start guide of Spring Framework
About the description order of Java system properties
About the idea of anonymous classes in Java
I understood the very basics of character input
Improve the performance of your Docker development environment
The basics of the App Store "automatic renewal subscription"
The story of tuning android apps with libGDX
Android Studio development for the first time (for beginners)
About next () and nextLine () of the Scanner class
Summarize the life cycle of Java objects to be aware of in Android development
Output about the method # 2
About the StringBuilder class
About Android basic grammar
The world of clara-rules (2)
[SPA development with Rails x Vue] Learn the basics of Vue.js (Overview of vue.js, template syntax)
Commentary: About the interface
About disconnect () of HttpURLConnection
[For beginners] DI ~ The basics of DI and DI in Spring ~
About the asset pipeline
Android development link summary