Android MainActivity with Edge-to-Edge Support

This snippet demonstrates the implementation of an Android 'MainActivity' that utilizes edge-to-edge features and custom padding for system insets. The 'EditText' field is initialized for user input. The code also showcases using 'ViewCompat' to handle window insets for a more immersive experience.
 avatar
unknown
java
a month ago
1.3 kB
2
Indexable
package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    EditText oneName;
    String str_oneName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        oneName = (EditText) findViewById(R.id.oneName);
    }

    public void next(View v) {
        str_oneName = oneName.getText().toString();
        Intent goToPage2 = new Intent(this, Page2.class);
        goToPage2.putExtra("str_oneName", str_oneName);
        startActivity(goToPage2);
    }
}
Editor is loading...
Leave a Comment