Home > Back-end >  How to create a view programmatically at 12 AM every day
How to create a view programmatically at 12 AM every day

Time:08-10

How would I go about creating a TextView at 12 AM every day, regardless if the app is open or not (unbound to app lifetime)? I already have the code to create the TextView, I just need to know how I would execute the code at 12 AM every day.

I'm not going to be opening another activity, everything will be done within the same activity.

Here is my code and my xml:

package com.example.app1;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;

import java.util.ArrayList;
import java.util.Calendar;

public class NotesActivity extends AppCompatActivity implements View.OnClickListener {
    LinearLayout linearLayout;
    ImageButton addNoteButton, infoButton;
    TextView titleTextNotes;
    ArrayList<TextView> notesList;
    SharedPreferences sharedPreferences;
    Calendar calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notes);
        sharedPreferences = this.getSharedPreferences("Notes Preferences", Context.MODE_PRIVATE);

        linearLayout = findViewById(R.id.linearLayout);
        addNoteButton = findViewById(R.id.addNoteButton);
        infoButton = findViewById(R.id.infoButton);
        titleTextNotes = findViewById(R.id.titleTextNotes);
        notesList = new ArrayList<>();

        Bundle extras = getIntent().getExtras();
        String noteTitle = extras.getString("noteTitle");

        if(notesList.size() == 0) {
            notesList.add(getStyledNote(noteTitle));
            linearLayout.addView(notesList.get(notesList.size() - 1));
        }

        /*addNoteButton.setOnClickListener(view -> {
            notesList.add(getStyledNote("My Log"));
            notesList.get(notesList.size() - 1).setOnClickListener(this);
            linearLayout.addView(notesList.get(notesList.size() - 1));
        });*/

        for(int i = 0; i < notesList.size(); i  ) {
            notesList.get(i).setOnClickListener(this);
        }

        titleTextNotes.setOnClickListener(this);

        for(int i = 0; i < sharedPreferences.getInt("Number of notes", 0); i  ) {
            notesList.add(getStyledNote(sharedPreferences.getString("Note "   i, "My Log")));
            linearLayout.addView(notesList.get(notesList.size() - 1));
        }

        infoButton.setOnClickListener(view -> callInfoActivity());
    }

    @Override
    public void onClick(View view) {
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putInt("Number of notes", notesList.size());
        for (int i = 0; i < notesList.size(); i  ) {
            editor.putString("Note "   i, notesList.get(i).getText().toString());
        }
        editor.apply();

        for (int i = 0; i < notesList.size(); i  ) {
            if (view.getId() == notesList.get(i).getId())
                callMainActivity(i);
        }
    }

    public void callMainActivity(int pos) {
        Intent sendInfoBack = new Intent();
        sendInfoBack.putExtra("noteTitleSentBack", notesList.get(pos).getText());
        setResult(RESULT_OK, sendInfoBack);
        overridePendingTransition(R.anim.leave, R.anim.enter);
        finish();
    }

    public void callInfoActivity() {
        Intent intentToLoad = new Intent(NotesActivity.this, InfoActivity.class);
        startActivity(intentToLoad);
        overridePendingTransition(R.anim.enter, R.anim.leave);
    }

    public TextView getStyledNote(String noteTitle) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 175);
        params.gravity = Gravity.CENTER;
        params.setMargins(0, 10, 0, 10);

        TextView note = new TextView(NotesActivity.this);
        note.setId(View.generateViewId());
        note.setLayoutParams(params);
        note.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.note_style, null));
        note.setTextSize(20);
        note.setTextColor(Color.BLACK);
        note.setGravity(Gravity.CENTER);
        note.setTypeface(getResources().getFont(R.font.sfprodisplaybold));
        if(noteTitle == null || noteTitle.equals(""))
            note.setText("My Log");
        else
            note.setText(noteTitle);

        return note;
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".NotesActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@ id/infoButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight=".2"
                    android:background="@android:color/transparent"
                    app:srcCompat="@drawable/info_mark" />

                <TextView
                    android:id="@ id/titleTextNotes"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight=".8"
                    android:fontFamily="@font/sfprodisplaybold"
                    android:gravity="center"
                    android:text="My Logs"
                    android:textColor="@color/teal_200"
                    android:textSize="30sp" />

                <ImageButton
                    android:id="@ id/addNoteButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight=".2"
                    android:background="@android:color/transparent"
                    app:srcCompat="@drawable/bar_chart" />

            </LinearLayout>

            <View
                android:id="@ id/view2"
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="40dp"
                android:layout_weight="1"
                android:background="@color/teal_200" />

            <LinearLayout
                android:id="@ id/linearLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"/>

        </LinearLayout>
    </ScrollView>

</LinearLayout>

CodePudding user response:

You need to schedule a periodic task using Work Manager.

val myUploadWork = PeriodicWorkRequestBuilder<SaveImageToFileWorker>(
             24,TimeUnit.HOURS) // The work is scheduled to repeat after 24 hours.
    .build()

Then you can submit the request to system :

WorkManager
    .getInstance(myContext)
    .enqueue(myUploadWork)

You can get more code and help from here.

  • Related