Tuesday, November 1, 2022

PROGRAM – 02 -- Create an application that will change wall paper time by time

 

Code:-

XML Code:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/homelayout"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:background="@color/white"
   
tools:context=".MainActivity">

    <
TextView
       
android:id="@+id/viewtime"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="200dp"
       
android:gravity="center"
       
android:textStyle="bold"
       
android:text="This is Current Time "
       
android:textSize="30sp" />
    <
Button
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/viewtime"
       
android:layout_marginTop="30dp"
       
android:id="@+id/getDateTime"
       
android:text="Get Date and  Time"
       
/>
</
RelativeLayout>

 

Java Code:-

 

package com.example.mansamcapro2;

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

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
   
Calendar calendar;
   
SimpleDateFormat simpleDateFormat;
   
String Date;
   
String Time;
   
TextView showDateTime;
   
Button btndatetime;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_main);

       
showDateTime = (TextView)findViewById(R.id.viewtime);
       
btndatetime = (Button)findViewById(R.id.getDateTime);

       
Calendar c = Calendar.getInstance();
       
simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
       
Date=simpleDateFormat.format(c.getTime());
       
//Time=simpleDateFormat.format(c.getTime());
       
int timeOfDay = c.get(Calendar.HOUR_OF_DAY);
       
int dateOfMonth = c.get(Calendar.DAY_OF_MONTH);
       
btndatetime.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {

               
showDateTime.setText(Date);
               
Toast.makeText(MainActivity.this,"Day: "+dateOfMonth+"\n"+"Time:"+timeOfDay,Toast.LENGTH_SHORT).show();
               }
        });

       
RelativeLayout homeLayout = findViewById(R.id.homelayout);

       
if(timeOfDay >= 8 && timeOfDay < 10 && dateOfMonth >= 1 && dateOfMonth < 8){

           
homeLayout.setBackgroundResource(R.drawable.c);
        }
else if(timeOfDay >= 10 && timeOfDay < 12 && dateOfMonth >= 8 && dateOfMonth < 20){

           
homeLayout.setBackgroundResource(R.drawable.d);

        }
else if(timeOfDay >= 13 && timeOfDay < 18 && dateOfMonth >= 20 && dateOfMonth < 25){

           
homeLayout.setBackgroundResource(R.drawable.b);
        }
else if(timeOfDay >= 18 && timeOfDay < 24 && dateOfMonth >= 25 && dateOfMonth < 30){

           
homeLayout.setBackgroundResource(R.drawable.a);
        }
    }
}

Output:-




No comments:

Post a Comment

PROGRAM 05 : AIM: - To understand Activity , Intent

  Code:- XML Code:   Activity_main.xml <? xml version ="1.0" encoding ="utf-8" ?> < androidx.constrain...