'How can I automatically set a different text every day to a TextView in Android Studio? (which will set 24 hours and change at 12 pm automatically)
I want to develop quotes of day app project. for this reason I want to show everyday different quote(aphorism) in the textView. Quote will set 24 hours and change 00:00:01 automaticically. Someone who uses the application today will see the 1st quote, and the next day, when they open the application, they will see the 2nd quote. 3rd day is different, 4th day is different..
But my codes doesn't show different quotes. every day show day1's quotes.
My codes below.
For example: day1= to be or not to be. day2= money, money, money..
My strings.xml
<resources>
<string name="app_name">QuotesApp</string>
<string-array name="quotes">
<item>Learn from yesterday, live for today, hope for tomorrow.</item>
<item>If you want to shine like the sun, first burn like the sun.</item>
<item>Time never comes again.</item>
<item>Wealth is the slave of wise man, the master of a fool.</item>
<item>One thing only I know, and that is that I know nothing.</item>
<item>A smooth sea never made a skilled sailor.</item>
</string-array>
</resources>
My Mainactivity.java
TextView dailyGreetings;
String[] mTestArray;
DateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");
Date date=new Date();
SharedPreferences preferences_Shared, text_Shared;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
dailyGreetings=findViewById(R.id.dailyGreetings);
mTestArray=getResources().getStringArray(R.array.quotes);
preferences_Shared=this.getSharedPreferences("PREFERENCE",MODE_PRIVATE);
text_Shared=this.getSharedPreferences("TEXT",MODE_PRIVATE);
Calendar c=Calendar.getInstance();
int timeofDay=c.get(Calendar.DAY_OF_MONTH);
if (timeofDay>=1){
if (preferences_Shared.getBoolean("isFirstRun", true)){
dailyGreetings.setText(mTestArray[(0) %(mTestArray.length)]);
saveDate();
}else{
if (!Objects.equals(preferences_Shared.getString("Date", ""), dateFormat.format(date)))
{
int idx=new Random().nextInt(mTestArray.length);
dailyGreetings.setText(mTestArray[idx]);
text_Shared.edit().putString("TEXT", dailyGreetings.getText().toString()).apply();
saveDate();
}
else {
dailyGreetings.setText(text_Shared.getString("TEXT", ""));
}
}
}
}
private void saveDate() {
preferences_Shared=this.getSharedPreferences("PREFERENCE",MODE_PRIVATE);
editor=preferences_Shared.edit();
editor.clear();
editor.putString("Date",dateFormat.format(date));
editor.apply();
}
Solution 1:[1]
Your condition is to check if isFirstRun is true, and it will always default to true since you never set it to false. That's why you're always getting the first quote.
I see some bugs in your else branch as well. Whenever the app is opened more than once on the same day, it's going to hit that inner else branch and show nothing. And you're using a formatted String date to store, which is error-prone, especially if you don't specify a Locale. Dates and times should always be stored as Longs.
I think the simplest way to do this is to get the current date, count how many days it has been since some constant date (the epoch suffices), and then use the remainder operator with the number of available quotes to get a new quote day by day.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_main)
val dailyGreetings: TextView = findViewById(R.id.dailyGreetings)
val quotes = resources.getStringArray(R.array.quotes)
val daysSinceEpoch = LocalDate.now().toEpochDay()
val quoteIndex = (daysSinceEpoch % quotes.size).toInt()
dailyGreetings.text = quotes[quoteIndex]
}
If you really need to pick a random quote every day instead of going through them in your defined order, with the risk that this sometimes mean the same quote gets randomly picked two or more days in a row, then you can use a Random with the date as a seed, so the same date will always get the same random value:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_main)
val dailyGreetings: TextView = findViewById(R.id.dailyGreetings)
val quotes = resources.getStringArray(R.array.quotes)
val daysSinceEpoch = LocalDate.now().toEpochDay()
val random = Random(daysSinceEpoch)
dailyGreetings.text = quotes[random.nextInt(quotes.size)]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
