본문 바로가기

PROGRAM/Android

MyMemo exam

<?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=".IndexActivity">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_index">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="70dp"
            android:text="@string/app_name"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/text_size_title"
            android:textStyle="bold" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

IndexActivity.java

public class IndexActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                goMainActivity();
            }
        },1500);
    }
    public void goMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

 

MemoItem.java

public class MemoItem {
    private String category;
    private String memo;
    private String regDate;

    public MemoItem(String category, String memo) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.KOREA);
        Date date = new Date();
        this.category = category;
        this.memo = memo;
        this.regDate = formatter.format(date);
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public String getRegDate() {
        return regDate;
    }

    public void setRegDate(String regDate) {
        this.regDate = regDate;
    }
}

 

MemoListAdapter.java

public class MemoListAdapter extends RecyclerView.Adapter<MemoListAdapter.ViewHolder> {
    private Context context;
    private int resource;
    private ArrayList<MemoItem> itemList;

    public MemoListAdapter(Context context, int resource, ArrayList<MemoItem> itemList) {
        this.context = context;
        this.resource = resource;
        this.itemList = itemList;
    }

    public void addItem(MemoItem item) {
        this.itemList.add(0, item);
        notifyDataSetChanged();
    }

    public void addItemList(ArrayList<MemoItem> itemList) {
        this.itemList.addAll(itemList);
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView categoryText;
        TextView memoText;
        TextView dateText;
        public ViewHolder(View itemView) {
            super(itemView);
            categoryText = itemView.findViewById(R.id.category);
            memoText = itemView.findViewById(R.id.memo);
            dateText = itemView.findViewById(R.id.regdate);
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(resource, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final MemoItem item = itemList.get(position);

        holder.categoryText.setText(item.getCategory());
        holder.memoText.setText(item.getMemo());
        holder.dateText.setText(item.getRegDate());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, item.getMemo(), Toast.LENGTH_SHORT).show();
            }
        });

    }
    @Override
    public int getItemCount() {
        return this.itemList.size();
    }
}

 

public class MainActivity extends AppCompatActivity {

    Context context;
    RecyclerView memoList;
    MemoListAdapter memoListAdapter;
    LinearLayoutManager layoutManager;

    Spinner categorySpinner;
    EditText memoEdit;

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

        categorySpinner = findViewById(R.id.category);
        memoList = findViewById(R.id.recyclerview);
        memoEdit = findViewById(R.id.memo);
        Button registerButton = findViewById(R.id.register);
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerMemo();
            }
        });

        layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        memoList.setLayoutManager(layoutManager);

        memoListAdapter = new MemoListAdapter(context, R.layout.row_memo_item, new ArrayList<MemoItem>());
        memoList.setAdapter(memoListAdapter);
        ArrayList<MemoItem> list = getMemoDummyList();
        memoListAdapter.addItemList(list);

    }
    private ArrayList<MemoItem> getMemoDummyList() {
        ArrayList<MemoItem> list = new ArrayList<>();

        MemoItem item1 = new MemoItem("일상", "오늘 점심은 김철수와 함께할 것");
        MemoItem item2 = new MemoItem("회사", "오후 2시에 팀 회의가 있으니 관련 서류 준비할 것");

        list.add(item1);
        list.add(item2);
        return list;
    }

    private void registerMemo() {
        String category = (String) categorySpinner.getSelectedItem();
        String memo = memoEdit.getText().toString();

        if (TextUtils.isEmpty(memo)) {
            Toast.makeText(context, "메모를 입력해주세요", Toast.LENGTH_SHORT).show();
            return;
        }

        MemoItem item = new MemoItem(category, memo);
        memoListAdapter.addItem(item);
        categorySpinner.setSelection(0);
        memoEdit.setText("");

        hideKeyboard();
    }

    private void hideKeyboard() {
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

 

res -> values -> arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="category">
        <item>일상</item>
        <item>회사</item>
        <item>기타</item>
    </array>
</resources>

res -> values -> dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="sapcing_small">4dp</dimen>
    <dimen name="sapcing_large">24dp</dimen>
    <dimen name="text_size_small">12sp</dimen>
    <dimen name="text_size_medium">16sp</dimen>
    <dimen name="text_size_title">36sp</dimen>
</resources>

res -> values -> colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>

 

activity_index.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=".IndexActivity">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_index">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="70dp"
            android:text="@string/app_name"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/text_size_title"
            android:textStyle="bold" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

row_memo_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp">

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

        <TextView
            android:id="@+id/category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="15dp"
            tools:text="일상" />

        <TextView
            android:id="@+id/memo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:textColor="@android:color/black"
            android:textSize="18dp"
            tools:text="메모" />

        <TextView
            android:id="@+id/regdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13dp"
            tools:text="2020.05.29 12:12:13"
            android:layout_gravity="end"/>

    </LinearLayout>
</androidx.cardview.widget.CardView>

atcitvity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:padding="@dimen/sapcing_small"
    tools:context=".MainActivity">

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

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

            <Spinner
                android:id="@+id/category"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:entries="@array/category"/>

            <EditText
                android:id="@+id/memo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/colorPrimary"
                tools:hint="메모 입력" />

        </LinearLayout>

        <Button
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:backgroundTint="@color/colorPrimary"
            android:text="등록"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_medium"
            android:textStyle="bold" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

'PROGRAM > Android' 카테고리의 다른 글

App 처음 만들기 - HelloAndroid  (0) 2020.10.21
Android Studio 4.1 Installation  (0) 2020.10.19
recyclerView Ex2  (0) 2020.05.24
firebase db  (0) 2020.05.23
리사이클러뷰-full  (0) 2020.05.10