package com.example.p10; import android.os.Bundle; import android.widget.Button; import android.widget.ImageView; 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 { private boolean isImage1 = true; // Flag to track the current image @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_main); // Initialize views ImageView img = findViewById(R.id.imgid); Button btn = findViewById(R.id.btnid); // Set up window insets listener 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; }); // Set click listener for the button btn.setOnClickListener(v -> { if (isImage1) { img.setImageResource(R.drawable.image2); } else { img.setImageResource(R.drawable.image1); } isImage1 = !isImage1; // Toggle the flag }); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter