[Android / Kotlin] Detailed notes 2

Display a dialog fragment by pressing a button

Note: If getSupportFragmentManager () is set to getFragmentManager (), an error will occur.

HogeActivity.java


Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Display a dialog
                DialogFragment dialog = new DialogFragment();
                //View getSupportFragmentManager()Is fixed, sample is an identification tag
                dialog.show(getSupportFragmentManager(), "sample");
            }
        });

Whether to raise an alert dialog or a dialog fragment

--Both can be called from Activity. --Dialog fragment: High degree of freedom in design and processing. --Alert dialog: Design is possible by applying Style.xml. --Alert dialog: It's OK to write in the method chain or divide the process by builder. ~, Your favorite problem.

Apply R.string or R.color to something that is not a View

strings.xml


<resources>
<string name="error_message">Please enter within 20 characters.</string>
</resources>

MainActivity.java


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Resources res = getResources();

        TextView textVIew = findViewById(R.id.text);
        textVIew.setText(res.getString(R.string.error_message));

       }
    }

Specify the number of columns in GridView

--If numColumns = "10" is not specified, only one column will be displayed. ――10 means 10 columns.

hoge.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">
<GridView
    android:id="@+id/grid_view"
    android:layout_width="300dp"
    android:layout_height="242dp"
    android:layout_gravity="center_vertical"
    android:numColumns="10"></GridView>
</LinearLayout>

0 test classes found in paclage Cause of'layout'error

--An error that occurs when you run in a strange place such as drawable or xml. --You can solve this problem by respecifying the activity that starts first in the frame on the left side of Run "▶" at the top of Android Studio.

getView is called only 3 times even though the number of elements is 9 in getCount

--An error that occurred while using Adapter to put elements in GridView. --GetView was called only 3 times because there were only 3 items in the GridView area. --The solution is to maximize the GridView area or reduce the size of the item.

What is setTag / getTag of adapter?

--The contents of ViewHolder setTag / getTag can be referenced by other classes (such as OnItemClickListener of Fragment). --If you don't use Tag, you can only call the method of that View in Adapter. --Hereafter, kotlin sample code

Adapter.java


   /**
     *View drawing method
     */
    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        val itemLayoutView = p1 ?: View.inflate(this.context, R.layout.item_custom_view, null)
        var viewHolder = itemLayoutView.tag
        if (viewHolder == null) {
            viewHolder = ViewHolder()
            viewHolder.customView = itemLayoutView.custom_view as CustomView
            (itemLayoutView.custom_view as CustomView).initView(
                this.values[p0].index()
            )
            itemLayoutView.tag = viewHolder
        }
        return itemLayoutView
    }

/**
 *Viewholder class
 */
class ViewHolder {
    lateinit var customView: CustomView
}

What is the difference between [:] and [=] in Kotlin?

-[=] Is how to write when using type inference. ――When assigning immediately, use [=] because type inference is possible.

(Example)

main.java


//java
int number = 3;
//kotlin
var number = 3

-[:] I don't plan to substitute it immediately, and I use it when I want to substitute it someday, so I can't type infer.

(Example)

main.java


//java
int number;
number = 3;
//kotlin
lateinit var number:Int
number = 3

About? (Elvis operator)

-? Is basically not used. It's troublesome because there are more branches. -? Means to allow null. --It may be judged whether the return value of the method is nullOK.

Recommended Posts

[Android / Kotlin] Detailed notes 2
[Android] Detailed notes
[Android / Kotlin] UI summary
[Android] Notes on xml
[Kotlin / Android] Listener rewrite
Notes in Android studio
Notes for Android application development beginners
Notes on calling Installer on Android App
Notes on Android (java) thread processing
[Android] Convert Android Java code to Kotlin
[Kotlin / Android] Create a custom view
Notes on creating android plugins for Unity