ImageView imageView = findViewById(R.id.my_image_view); int id = imageView.getId(); Log.d("Image View ID", "The ID of the Image View is: " + id);
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.example.myapp.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView image1 = findViewById(R.id.image_one); int id1 = image1.getId(); ImageView image2 = findViewById(R.id.image_two); int id2 = image2.getId(); // Do something with the IDs... } }This example demonstrates how to obtain the IDs of two ImageView widgets by finding them using findViewById(). The IDs are then assigned to the variables id1 and id2, respectively. This code is part of an Android app and uses the AppCompatActivity and Bundle classes, as well as the R package to access resources.