Program to change the Background image |Android Program|


Activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity"
   
android:orientation="vertical"
   
android:id="@+id/con">

    <
Button
       
android:id="@+id/button1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginStart="76dp"
       
android:layout_marginLeft="76dp"
       
android:layout_marginTop="240dp"
       
android:text="image1"
       
app:layout_constraintStart_toStartOf="parent"
       
app:layout_constraintTop_toTopOf="parent" />

    <
Button
       
android:id="@+id/button2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="240dp"
       
android:text="image2"
       
app:layout_constraintEnd_toEndOf="parent"
       
app:layout_constraintHorizontal_bias="0.446"
       
app:layout_constraintStart_toEndOf="@+id/button1"
       
app:layout_constraintTop_toTopOf="parent" />
</
androidx.constraintlayout.widget.ConstraintLayout>


MainActivity.java

package com.example.changebackground;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button
b1,b2;
    ConstraintLayout
con;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
b1=findViewById(R.id.button1);
       
b2=findViewById(R.id.button2);
       
con=findViewById(R.id.con);

       
b1.setOnClickListener(this);
       
b2.setOnClickListener(this);

    }

   
@Override
   
public void onClick(View v) {

       
switch (v.getId()){
           
case R.id.button1:
               
con.setBackgroundResource(R.drawable.p1);
               
break;

           
case R.id.button2:
               
con.setBackgroundResource(R.drawable.p2);
               
break;
        }

    }
}

Design


Post a Comment

0 Comments