0

I am basically complete noob to android programming, just took 1 week of class and now I have to do a mini project.

I decided to build an app that can display the Zodiac signs of an user, when they click their DOB on the calendar.

The else if statements for displaying the name of each zodiac sign perfectly works. Now, I m just trying to implement a feature where it displays the Zodiac image along with the zodiac name.

Please look at the code below. (Sorry if the code looks messy)

package com.example.zodiac;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView maintitle;
    TextView subtitle;
    CalendarView calendar;
    TextView zodiactext;
    ImageView image;

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

        maintitle = (TextView) findViewById(R.id.mainTitle);
        subtitle = (TextView)  findViewById(R.id.subTitle);
        calendar = (CalendarView) findViewById(R.id.calendar);
        zodiactext = (TextView) findViewById(R.id.zodiacText);
        image = (ImageView) findViewById(R.id.image);


        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {

                int imageResource = getResources().getIdentifier("@drawable/aquaris", null, getPackageName());
                Drawable res = getResources().getDrawable(imageResource);
                image.setImageDrawable(res);

                String zodiacSign="";
                if(month==0){
                    if (dayOfMonth<20){
                        zodiacSign="Capricorn";
                    }else {
                        zodiacSign="Aquarius";
                    }
                }else if(month==1){
                    if (dayOfMonth<19){
                        zodiacSign="Aquarius";
                    }else {
                        zodiacSign="Pisces";
                    }
                }else if(month==2){
                    if (dayOfMonth<21){
                        zodiacSign="Pisces";
                    }else {
                        zodiacSign="Aries";
                    }
                }else if(month==3){
                    if (dayOfMonth<20){
                        zodiacSign="Aries";
                    }else {
                        zodiacSign="Taurus";
                    }
                }else if(month==4){
                    if (dayOfMonth<21){
                        zodiacSign="Taurus";
                    }else {
                        zodiacSign="Gemini";
                    }
                }else if(month==5){
                    if (dayOfMonth<22){
                        zodiacSign="Gemini";
                    }else {
                        zodiacSign="Cancer";
                    }
                }else if(month==6){
                    if (dayOfMonth<23){
                        zodiacSign="Cancer";
                    }else {
                        zodiacSign="Leo";
                    }
                }else if(month==7){
                    if (dayOfMonth<23){
                        zodiacSign="Leo";
                    }else {
                        zodiacSign="Virgo";
                    }
                }else if(month==8){
                    if (dayOfMonth<23){
                        zodiacSign="Virgo";
                    }else {
                        zodiacSign="Libra";
                    }
                }else if(month==9){
                    if (dayOfMonth<24){
                        zodiacSign="Libra";
                    }else {
                        zodiacSign="Scorpio";
                    }
                }else if(month==10){
                    if (dayOfMonth<23){
                        zodiacSign="Scorpio";
                    }else {
                        zodiacSign="Sagittarius";
                    }
                }else if(month==11){
                    if (dayOfMonth<22){
                        zodiacSign="Sagittarius";
                    }else {
                        zodiacSign="Capricorn";
                    }
                }
                zodiactext.setText(zodiacSign);

            }
        });


    }
}

XML file -

<?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"
    android:background="@drawable/space"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="286dp">

    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="28dp"
        android:layout_marginTop="148dp"
        android:background="@color/design_default_color_background"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/mainTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:text="Zodiac Finder"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/subTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="41dp"
        android:layout_marginBottom="40dp"
        android:text="Pick your date and month to find your Zodiac Symbol"
        app:layout_constraintBottom_toTopOf="@+id/calendar"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/zodiacText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:text="Your Zodiac symbol is - "
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/calendar" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="148dp"
        android:layout_height="127dp"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.963"
        android:src="@drawable/scorpio"
        tools:srcCompat="@drawable/scorpio" />

</androidx.constraintlayout.widget.ConstraintLayout>

I am completely stuck at this point. Don't know how to implement these zodiac images along with its names. I am pretty sure I have done lots of mistakes in the ImageView codes, cuz I haven't learned anything about using them yet.

This is how the app works now

Please help me with this issue.

  1. All the imageviews I added in the XML file, by default displays on the app, even when there is no function to it in java file.
  2. So, I want it to be blank when the app is freshly opened. And when the user clicks a date in the calendar, the app should display that respective zodiac image only, along with the zodiac name (If else conditions).

Apologies if there's any mistake in my question, and I hope someone would be able to help me out. If anything isn't clear, please ask me.

Thank you

Edit: Updated the code with small changes.

7
  • For more how to set image as a drawable to dynamically This answer should help you out Commented Aug 30, 2022 at 19:34
  • @MalikSaifullah Thanks for your reply. I have followed what you said. Just kept one imageview at xml file And added this on the java file. int imageResource = getResources().getIdentifier("@drawable/aquaris", null, getPackageName()); Drawable res = getResources().getDrawable(imageResource); image.setImageDrawable(res); But however, the image doesnt show up when i run the app. What am I doing wrong? Commented Aug 30, 2022 at 19:51
  • My bad my first comment was not tested as hence was wrong. let me write a detailed answer and could please updated your code with what you have tried. Commented Aug 30, 2022 at 19:58
  • @MalikSaifullah Ok, I have updated the code. Please check Commented Aug 30, 2022 at 20:03
  • @MalikSaifullah Also, after adding those 3 lines of code in the java file, the app keeps crashing when i click a date on the calendar. Commented Aug 30, 2022 at 20:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.