Source to display character array with numberPicker in Android studio (Java)

As the title says. Since it is a memo for myself, I will omit the explanation. There were many places where I got stuck, so I hope it will be useful and I will share it.

Development environment: Android Studio 3.1.3 (Mac) Language: java

MainActivity.java


package com.example.ktakeda.numberpicker;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.TextView;

import android.content.res.Resources;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

import static com.example.ktakeda.numberpicker.R.array;

public class MainActivity extends Activity {

    NumberPicker numPicker;
    Button button1;
    TextView textView1;

    //Within initViews, strings.Change to read from xml
    // final String[] pref = {"tokyo", "chiba", "kanagawa"};


    //Generate view?
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
    }
    //Get element of v
    private void findViews(){
        numPicker = (NumberPicker)findViewById(R.id.numPicker);
        button1 = (Button)findViewById(R.id.button1);
        textView1 = (TextView)findViewById(R.id.textView1);
    }

    //processing
    private void initViews(){

        // getResources
        Resources res = getResources();
        final String[] pref = res.getStringArray(R.array.pref_array);

        //Put null first, prevent the app from crashing
        numPicker.setDisplayedValues(null);
        //Setting
        numPicker.setDisplayedValues(pref);
        numPicker.setMinValue(1);
        numPicker.setMaxValue(pref.length);


        button1.setOnClickListener(new OnClickListener(){
            //Are you there?
            @Override
            public void onClick(View v) {
                int idx  = numPicker.getValue();
                textView1.setText(pref[idx - 1]);
            }
        });
    }
}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:textSize="30dp"/>

    <NumberPicker
        android:id="@+id/numPicker"
        android:layout_width="200sp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OK"/>

</LinearLayout>

strings.xml


<resources>
    <string name="app_name">numberPicker</string>
    <string-array name="pref_array">
        <item>Tokyo</item>
        <item>Kanagawa</item>
        <item>Chiba</item>
        <item>Okinawa</item>
    </string-array>
</resources>

Recommended Posts

Source to display character array with numberPicker in Android studio (Java)
Java to C and C to Java in Android Studio
Refer to C ++ in the Android Studio module (Java / kotlin)
Use Java included with Android Studio to build React Native
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
How to use ExpandableListView in Android Studio
I want to ForEach an array with a Lambda expression in Java
3 ways to import the library in Android Studio
How to display a web page in Java
Display text character by character in Android Surface View
Android Studio memo that you want to display Toast characters in large size
How to call functions in bulk with Java reflection
[Android Studio] How to change TextView to any font [Java]
Comments in Java source
[Android Studio] [Java] How to fix the screen vertically
[Android Studio] I want to set restrictions on the values registered in EditText [Java]
[For beginners] Minimum sample to display RecyclerView in Java
How to convert a file to a byte array in Java
How to query Array in jsonb with Rails + postgres
Notes in Android studio
Gzip-compress byte array in Java and output to file
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
How to take a screenshot with the Android Studio emulator
How to decompile apk file to java source code with MAC
Convert a Java byte array to a string in hexadecimal notation
Java source sample (Oracle Database + java) to SELECT and display CLOBs
[Android, Java] Convenient method to calculate the difference in days
Read WAV data as a byte array in Android Java
You are currently using Java 6. Solution in Android Studio Gradle
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
How to make an app using Tensorflow with Android Studio
How to implement one-line display of TextView in Android development
Convert 2D array to csv format with Java 8 Stream API
[Java] UTF-8 (with BOM) is converted to 0xFFFD (REPLACEMENT CHARACTER)
Java to play with Function
[Java] How to display Wingdings
Connect to DB with Java
Connect to MySQL 8 with Java
Java in Visual Studio Code
[Java] Convert ArrayList to array
Seasonal display with Java switch
How to initialize Java array
[Java] How to search for a value in an array (or list) with the contains method
Android: How to deal with "Could not determine java version from '10 .0.1'"
How to switch Java version with direnv in terminal on Mac
[For beginners] Minimum sample to update RecyclerView with DiffUtils in Java
Cast an array of Strings to a List of Integers in Java
I tried to make an Android application with MVC now (Java)
[Java] I want to perform distinct with the key in the object
[Android] Convert Map to JSON using GSON in Kotlin and Java
How to call with One Touch (without confirmation) on Android (Java)
How to encrypt and decrypt with RSA public key in Java
Determining if a custom keyboard is enabled in Android Studio (Java)
Try implementing Android Hilt in Java
Guess the character code in Java
Java to learn with ramen [Part 1]
Morphological analysis in Java with Kuromoji
Android Development-Try to display a dialog-
Multithreaded to fit in [Java] template
Display Firestore data in RecyclerView [Java]
How to enclose any character with "~"