3

This is my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/day_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="4dip"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:padding="2dip" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/marca_bike_event" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip" >

            <ImageView
                android:id="@+id/imageView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/marca_bike_event" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/imageView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dip"
                android:src="@drawable/marca_bike_event" />

            <ImageView
                android:id="@+id/imageView18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/marca_bike_event" />
        </LinearLayout>

        <TextView
            android:id="@+id/day_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dip"
            android:gravity="right|bottom"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

and this is my custom view

import java.util.Calendar;

public class DayView extends LinearLayout {

    private final boolean belongsToMonth;
    private final TextView numberView;
    private int racesNum = 0;
    private final ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8, iv9, iv10,
    iv11, iv12, iv13, iv14, iv15, iv16, iv17, iv18;

    public DayView(final Context context, final Calendar moving,
            final SingleCalendar calendar) {
        super(context);
        View.inflate(context, R.layout.calendar_week_day, this);
        iv1 = (ImageView) findViewById(R.id.imageView1);
        iv2 = (ImageView) findViewById(R.id.imageView2);
        iv3 = (ImageView) findViewById(R.id.imageView3);
        iv4 = (ImageView) findViewById(R.id.imageView4);
        iv5 = (ImageView) findViewById(R.id.imageView5);
        iv6 = (ImageView) findViewById(R.id.imageView6);
        iv7 = (ImageView) findViewById(R.id.imageView7);
        iv8 = (ImageView) findViewById(R.id.imageView8);
        iv9 = (ImageView) findViewById(R.id.imageView9);
        iv10 = (ImageView) findViewById(R.id.imageView10);
        iv11 = (ImageView) findViewById(R.id.imageView11);
        iv12 = (ImageView) findViewById(R.id.imageView12);
        iv13 = (ImageView) findViewById(R.id.imageView13);
        iv14 = (ImageView) findViewById(R.id.imageView14);
        iv15 = (ImageView) findViewById(R.id.imageView15);
        iv16 = (ImageView) findViewById(R.id.imageView16);
        iv17 = (ImageView) findViewById(R.id.imageView17);
        iv18 = (ImageView) findViewById(R.id.imageView18);
        final ImageView[] views = new ImageView[] { iv1, iv2, iv3, iv4, iv5,
                iv6, iv7, iv8, iv9, iv10, iv11, iv12, iv13, iv14, iv15, iv16,
                iv17, iv18 };
        for (final ImageView v : views) {
            v.setVisibility(View.INVISIBLE);
        }

        numberView = (TextView) findViewById(R.id.day_number);
        numberView.setText(Integer.toString(moving.get(Calendar.DAY_OF_MONTH)));
        setTag(moving.clone());
        belongsToMonth = (moving.get(Calendar.MONTH) == calendar.current
                .get(Calendar.MONTH));
        final Resources res = getResources();
        if (!belongsToMonth) {
            numberView.setTextColor(res
                    .getColor(R.color.calendar_unbelonging_dayview_text));
            setBackgroundResource(R.drawable.unbelonging_dayview);
        } else {
            numberView.setTextColor(res
                    .getColor(R.color.calendar_belonging_dayview_text));
            setBackgroundResource((DateParseUtils.areEqualDays(moving,
                    calendar.today)) ? R.drawable.selected_dayview
                            : R.drawable.unselected_dayview);
        }
    }

    protected void onMeasure(final int widthMeasureSpec,

    public boolean belongsToMonth() {

    public Calendar getCalendar() {

    public void addRace(final ParseObject carrera) {
        final ImageView[] views = new ImageView[] { iv1, iv2, iv3, iv4, iv5,
                iv6, iv7, iv8, iv9, iv10, iv11, iv12, iv13, iv14, iv15, iv16,
                iv17, iv18 };


        if (racesNum < views.length) {
            final ImageView iv = views[racesNum];
            final int id = getRaceId(carrera);
            iv.setImageDrawable(getContext().getResources().getDrawable(id));
            for (int i = 0; i < views.length; i++) {
                final ImageView iv2 = views[i];
                if (i <= racesNum) {
                    iv2.setVisibility(View.VISIBLE);
                } else {
                    iv2.setVisibility(View.INVISIBLE);
                }
            }
            racesNum++;
        }

    }

    private int getRaceId(final ParseObject carrera) {

        final String disciplina = carrera.getString("disciplinaId");
        if (disciplina.equals(SettingsActivity.ID_BIKE_EVENT)) {
            return R.drawable.marca_bike_event;
        } else if (disciplina.equals(SettingsActivity.ID_BMX)) {
            return R.drawable.marca_bmx;
        } else if (disciplina.equals(SettingsActivity.ID_ENDURO)) {
            return R.drawable.marca_enduro;
        } else if (disciplina.equals(SettingsActivity.ID_FREESTYLE)) {
            return R.drawable.marca_freestyle;
        } else if (disciplina.equals(SettingsActivity.ID_MOTO_GP)) {
            return R.drawable.marca_moto_gp;
        } else if (disciplina.equals(SettingsActivity.ID_MOTOCROSS)) {
            return R.drawable.marca_motocross;
        } else if (disciplina.equals(SettingsActivity.ID_MOTORBIKE_RALLY)) {
            return R.drawable.marca_mc_rally;
        } else if (disciplina.equals(SettingsActivity.ID_MTB_4X)) {
            return R.drawable.marca_mtb_4x;
        } else if (disciplina.equals(SettingsActivity.ID_MTB_DH)) {
            return R.drawable.marca_mtb_dh;
        } else if (disciplina.equals(SettingsActivity.ID_MTB_ENDURO)) {
            return R.drawable.marca_mtb_enduro;
        } else if (disciplina.equals(SettingsActivity.ID_QUADCROSS)) {
            return R.drawable.marca_quad_cross;
        } else if (disciplina.equals(SettingsActivity.ID_SUPERBIKE)) {
            return R.drawable.marca_superbike;
        } else if (disciplina.equals(SettingsActivity.ID_SUPERMOTO)) {
            return R.drawable.marca_supermoto;
        } else if (disciplina.equals(SettingsActivity.ID_TRIAL)) {
            return R.drawable.marca_trial;
        } else if (disciplina.equals(SettingsActivity.ID_URBAN_DH)) {
            return R.drawable.marca_urban_dh;
        } else if (disciplina.equals(SettingsActivity.ID_XC_RALLIES)) {
            return R.drawable.marca_xc_rallies;
        }
        return R.drawable.marca_other_event;
    }

}

addRace is getting called from the OnPostExecute method of an AsyncTask. However, it won't make any ImageViews visible. I've debugged it putting a breakpoint on the setVisibility line, and it actually calls setVisibility on the main thread, but it just does not work. Trying to change the ImageView drawable won't work either.

I've tried setting its visibility and changing the drawable on the constructor, and it works. It just seems like the setVisibility method is just doing nothing.

Am I missing something?

2
  • do you got any issues in background while set-visibility get calls ? Commented Jan 3, 2013 at 13:45
  • Nope. I just make a background call to server, then on response I set the visibility of my views. The call is done alright, the only awkward thing is the views don't change their visibility or respond at all outside the View constructor. Commented Jan 4, 2013 at 8:40

3 Answers 3

1

Ok, solved it.

My DayViews are dynamically created on an ArrayAdapter<Calendar>. All I had to do was do all the view manipulation in the getView method of the ArrayAdapter and then call adapter.notifyDataSetChanged() instead of trying to modify them directly.

Also, I had to use ViewHolder pattern in order to get the modifications done right.

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

Comments

0

You trying to set visibility for ImageView from background Async thread. You can't direct access views from background threads!

You can use Handler, and update visibility of your views in next method

public void handleMessage(Message msg) {
    super.handleMessage(msg);
    // update views here
}

or you can use next sample code direct in your code:

    imageView.post(new Runnable() {
        @Override
        public void run() {
            imageView.setVisibility(View.VISIBLE));
        }
    });

3 Comments

No, I'm doing it from the Main thread in the onPostExecute method of AsyncTask, and debugger actually says I'm on the Main thread.
I'm on it, I'll post results when tests are finished ;-)
Need to find the time to test your code on my computer after work
0

Last resort that always worked for me in cases where

.setVisibility(VISIBLE/INVISIBLE/GONE) ;

failed was to add/remove my layout to/from the ViewGroup that is attached to. Post this to the View thread and it will do the job:

m_viewIsVisible=/false/true; //set before hand

public void run()
{    
    try
    {
       if (!m_viewIsVisible)
           RootView.rootGroop().removeView(currentLayout);
       else
           RootView.rootGroop().addView(currentLayout);

    } catch (Exception ex)
    {
        Log.e(TAG, "-Add/Remove layout throws exception:: " + ex.toString());
    }
}

Here currentLayout is the stubborn layout that you want to show/hide. RootView.rootGroop() gets me the ViewGroup the currentLayout belons to. You should apply your own logic to holding and getting a reference to the parent ViewGroup.

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.