I'm making a program that makes a 365-day button to make a year's calendar, but as I set Layout every week, the coding gets too long if I do it, so I tried to use an arrangement using a for statement, but the application was not executed and it was forced to end. I will attach the XML and Java code.
===============JAVA===========
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ScrollView scrollView;
LinearLayout Cal1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mInflater = getMenuInflater();
mInflater.inflate(R.menu.menu1, menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cal1 = (LinearLayout)findViewById(R.id.calendar1);
LinearLayout linear[] = new LinearLayout[54];
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1;
final Button btn[] = new Button[368];
for (int i = 0; i < 368; i++) {
btn[i] = new Button(this);
btn[i].setWidth(20);
btn[i].setTextSize(11);
btn[i].setBackgroundColor(Color.WHITE);
btn[i].setId(i);
}
for (int i = 0; i<7; i++){
for(int j =1; j<54; j++){
linear[i] = new LinearLayout(this);
linear[j].addView(btn[i + ((j-1) * 7)],params);
linear[i].addView(btn[i], params);
Cal1.addView(linear[i]);
}
}
for (int i = 0; i < 3; i++) {
btn[i].setText("");
}
for (int i = 3; i < 7; i++) {
btn[i].setText("" + (i - 2));
}
for (int i = 7; i < 14; i++) {
btn[i].setText("" + (i - 2));
}
for (int i = 14; i < 21; i++) {
btn[i].setText("" + (i - 2));
}
for (int i = 21; i < 28; i++) {
btn[i].setText("" + (i - 2));
}
for (int i = 28; i < 32; i++) {
btn[i].setText("" + (i - 2));
}
for (int i = 32; i < 34; i++){
btn[i].setText(""+ (i - 2));
}
for (int i = 34; i < 35; i++){
btn[i].setText("");
} //
for (int i = 35; i < 41; i++) {
btn[i].setText("" );
}
for (int i = 41; i < 42; i++) {
btn[i].setText("" + (i - 40));
}
for (int i = 42; i < 49; i++) {
btn[i].setText("" + (i - 40));
}
for (int i = 49; i < 56; i++) {
btn[i].setText("" + (i - 40));
}
for (int i = 56; i < 63; i++) {
btn[i].setText("" + (i - 40));
}
for (int i = 63; i < 70; i++) {
btn[i].setText("" + (i - 40));
}//February
}
}
==================XML=====================
<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="wrap_content"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#000"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp"
android:text="2020 Scroll Calendar"
android:textSize="20dp"
android:textColor="#FFF"/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="Today"/>
<Button
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="View Method"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="20dp"
android:text="January"
android:textColor="#fff">
</TextView>
</LinearLayout>
<!--Day-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="top"
android:background="#eaeae9"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="work"
android:textSize="9sp"
android:textColor="#FF0000" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Month"
android:textColor="#000000"
android:textSize="9sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="hwa"
android:textColor="#000000"
android:textSize="9sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="number"
android:textColor="#000000"
android:textSize="9sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="neck"
android:textColor="#000000"
android:textSize="9sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="Gold"
android:textColor="#000000"
android:textSize="9sp" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="to"
android:textSize="9sp"
android:textColor="#0067a3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/calendar1"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
If you are going to make a calendar, use recyclerview.
I posted a new question I'm sorry that I couldn't delete it because someone else replied.
© 2024 OneMinuteCode. All rights reserved.