---
# System prepended metadata

title: 9/21 Android from scratch
tags: [android, java]

---

# 9/21 Android from scratch


###### tags: `android` `java`

[TOC]


## Import library 
alt + enter

## Create button event 

```java=
public void  button1Click(View view){

}
```

## Counter 

### Get element ID 
```java=
TextView output = (TextView) findViewById(R.id.ouput1);
```

R is mean the resource folder 

![](https://i.imgur.com/dRQmDVc.png)


### Get output text and turn into a integer.

```java=
count = Integer.parseInt(output.getText().toString());
```


### Pass the value to the element

```java=
output.setText(Integer.toString(count)); 
```

## Source Code 

### Practice 1

```java=
package com.example.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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


    public void  button1Click(View view){
        int count;
        TextView output = (TextView) findViewById(R.id.ouput1);
        count = Integer.parseInt(output.getText().toString());
        count++;
        output.setText(Integer.toString(count));
    }
    public void  button2Click(View view){
        TextView output = (TextView) findViewById(R.id.ouput1);
        output.setText("0");
    }
}
```

![](https://i.imgur.com/gLTZN8a.png)




## practice 2

```java=
package com.example.inputtextandshow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    public void buttonClick(View view){
        EditText nameInput = (EditText) findViewById(R.id.nameInput);
        String userName = nameInput.getText().toString();

        TextView outputName = (TextView) findViewById(R.id.showName);
        outputName.setText("Hello" + userName);
    }
}
```


![](https://i.imgur.com/wgHnszg.png)


> [time=Thu, Sep 22, 2022 8:11 PM] Finished 
> [name=侯智晟]
Gmail <aa098933967901@gmail.com>
