0

My problem is that I want to display one image at a time on screen and every day my image will change. I am using imageView but it's showing all the images of notification.xml. I have searched a lot and I think I am missing some simple things. Plz help.

Notification.java is:

    package com.example.customcalendarandroid;
    import java.io.File;



    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.app.Activity;
    import android.graphics.drawable.Drawable;

    public class NotificationView extends Activity{
       @Override
       public void onCreate(Bundle savedInstanceState)
       {
          super.onCreate(savedInstanceState);
         setContentView(R.layout.notification);
          //setContentView(R.layout.newxml);

         int i=10;
         if(i==10){
             ImageView   imgView = (ImageView) findViewById(R.id.ekadashi_name2);
             imgView.setImageResource(R.drawable.prabhu2);
         }else
         {
             ImageView   imgView = (ImageView) findViewById(R.id.ekadashi_name3);
             imgView.setImageResource(R.drawable.prabhu3); 
         }
         }
and 

notification.xml is

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    >

    <ImageView
         android:id="@+id/ekadashi_name"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/krishna" />

  <ImageView
         android:id="@+id/ekadashi_name1"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhupada" />
     <ImageView
         android:id="@+id/ekadashi_name2"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu2" />
     <ImageView
         android:id="@+id/ekadashi_name3"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu3" />
     <ImageView
         android:id="@+id/ekadashi_name4"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu4" />
     <ImageView
         android:id="@+id/ekadashi_name5"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu5" />
     <ImageView
         android:id="@+id/ekadashi_name6"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhu6" />




    <ImageView
         android:id="@+id/ekadashi_name"
        android:layout_width="match_parent"
        android:layout_height="20sp"

        android:src="@drawable/prabhupada1" />


</LinearLayout>
1
  • Please put you xml file here Commented Jun 5, 2014 at 11:35

4 Answers 4

1

You can use Array of images like

    private int[] imagesArray = {
    R.drawable.img_00,
    R.drawable.img_01,
    R.drawable.img_02,
    };

Then u can do

    Drawable d = getResources().getDrawable(imagesArray[i]);
    yourImageView.setImageDrawable(d);

just change the int i value every time when u want to load image

And in xml keep only one Image View that will work..u kept different image view for differentimage

hope that will help to u

Sign up to request clarification or add additional context in comments.

1 Comment

This is the approach I'd go for
0

This is always true:

int i=10;
if(i==10){
}

This program will always show ekadashi_name2. You have to change i to be a random number.

Random rn = new Random();
i = 1 + Random.nextInt(9);

With this, every time the application opens, a new image will be displayed.

Comments

0

take out android:src="@drawable/prabhupada" parameter from all ImageView in xml .. then other image will not be set. By default you are providing imagesource to them.

or you can assign all other imageView to null

imageView.setImageResource(android.R.color.transparent);  // to set the image background null

Comments

0

for each day, take any random image by findViewByID and set it's visibility to visble and show it and access all other images using findViewById and set their visibility either invisible or gone, as per your requirement! Also, instead of adding all images in xml, add only one image in your xml and do net set any source to it. Set source from java code, not from xml.

Comments

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.