Published Date : 2021年2月8日17:25

1. 期待値を計算するAndroidアプリケーションの作成
1. Creating an Android Application to Calculate Expectations

This blog has an English translation


This is a summary blog post about a video I uploaded to NicoNico.


Please refer to the video for details.


Table of Contents

① 動画の説明
① Video Description


This is the Android app we are going to make this time.

Responsive image

Responsive image

Responsive image

Android Studioの環境設定方法は以前の動画を参考にしてください。

See my previous video for how to set up the Android Studio.


This is the Android version of the app that calculates expectations I created in the last video using Flask in Python.


This time, we added another page that displays the probability and amount entered, the number multiplied by that number, and the expected value as a table.


I've shown you how to make an Android app a few times before, so check it out.

ではいつも通りにAndroid Studioを立ち上げてから、空のアクティビティを選択して新しいプロジェクトを作成してください。

Start Android Studio as usual, then select an empty activity to create a new project.


Name the project as you like. An easy to understand name might be good.


Place [ScrollView] in the [activity_main.xml] file in the [layout] folder.

    android:layout_margin="10dp" >



This is because the display space for the [ListView] that you add later becomes larger as you enter the probability or amount, so you need to scroll the screen so that you can see all of it.


Next, place [LinearLayout] in [ScrollView], and then place the app title and description text vertically on the screen by specifying [vertical] in the [orientation] property of [LinearLayout].

    android:isScrollContainer="false" >


In the [strings.xml] file in the [values] folder, create and register the text, etc. to be used for the entire project separately.


To use the text registered in this [strings.xml] in the layout file, add an [at sign] to the beginning of the character and specify the name registered in [name attribute].

<string name="Title">Expectation calculator</string>

[res]フォルダ内の[drawable]フォルダで右クリックして、[new]ー>[Drawable Resource File]の順に選択して新しいリソースファイルを作成します。

Right-click the [drawable] folder in the [res] folder to create a new resource file by selecting [new] ー> [Drawable Resource File].


Name the file [border.xml] and use [shape] to create the border.


In the [colors.xml] file in the [values] folder, create and register the color to be used for the entire project separately.


Uses hex decimals to specify colors.


The color you created is displayed on the left, so you can see the color you specified.


You then return to the [activity_main.xml] file and place the application title and description text inside it.


Next, in the [LinearLayout], set the [orientation] property to [horizontal] so that the text view which is a descriptive label for edit text for the probability and amount is placed horizontally on the screen.


The edit text for entering the two values should also be horizontal to the screen.


Because there is more than one EditText, entering a number in the first EditText automatically switches the display of the [Enter] symbol on the keyboard to the symbol that moves to the next EditText.


I think that specification is good for the first EditText, but it is troublesome that I have to fold the keyboard screen to complete each entry for the last EditText.


Therefore, I set the [imeOptions] property to [actionDone] to end the input with a single EditText.


It then places a button that adds each value entered to the ListView.


Set [textAllCaps] to false so that all letters are not uppercase and that lowercase letters are also displayed.


It then arranges the ListViews horizontally by placing [LinearLayout] and setting the [orientation] property to [horizontal].


To change the display height of the ListView to correspond to the added value, you need to create your own customized ListView.

[Java]フォルダ内にある、[]等があるパッケージフォルダで右クリックして、[new]を選択して[Java Class]ファイルを作成しましょう。

In the [Java] folder, right click on the package folder that contains [] and select [new] to create the [Java Class] file.


Create your own custom ListView that inherits from ListView.

public class MaxHeightListView extends ListView {


Import the required libraries.

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;


Prepare the constructor.

public MaxHeightListView(Context context) {

public MaxHeightListView(Context context, AttributeSet attrs) {
    super(context, attrs);

public MaxHeightListView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);


Override the [onMeasure] method.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, View.MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);
    ViewGroup.LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();


In this method, the height and width of the child view are calculated from the height and width of the parent view.


The height of the child view, the listview, changes each time a value is added.


If the height of the child view changes, the height of the parent view also changes.


Each time you want to expand the height of a child view in a scroll view, you must explicitly specify the size to pass to the child view based on the value of the parent view.


All you have to do is specify the name of the custom view you created in the layout file to use.


That's all. Thank you for your hard work.