Search code examples
androidnullviewgroup

congratulate user when viewgroup is empty


im new to android and ive made a few small activities that show shapes that can be dragged and dropped onto its appropriate place holder if they match the shape stays and if it doesnt it pings back to where it was but i cant figure out how to tell when the view group is empty as null has to be declared and isnt it own state if ive got that right can anyone help all im after is a simple kind of, if/else viewgroup == null do.... animation can somebody shed some light on this for me?

here is what i have

public class PuzzleActivity extends Activity implements OnTouchListener, OnDragListener {
View topLevelLayout;
HorizontalScrollView from;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_puzzle);
    findViewById(R.id.squareImage).setOnTouchListener(this);
    findViewById(R.id.circleImage).setOnTouchListener(this);
    findViewById(R.id.triangleImage).setOnTouchListener(this);
    findViewById(R.id.triangleImage2).setOnTouchListener(this);

    findViewById(R.id.squareImage1).setOnDragListener(this);
    findViewById(R.id.circleImage1).setOnDragListener(this);
    findViewById(R.id.triangleImage1).setOnDragListener(this);
    findViewById(R.id.triangleImage3).setOnDragListener(this);
    topLevelLayout = findViewById(R.id.top_layout);
    from = (HorizontalScrollView) findViewById(R.id.scrollView) ;

    if (isFirstTime()) {
        topLevelLayout.setVisibility(View.INVISIBLE);
    }
}

@Override
public boolean onTouch(View v, MotionEvent e) {
    if (e.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
        v.startDrag(null, shadowBuilder, v, 0);
        return true;
    } else {
        return false;
    }
}

@Override
public boolean onDrag(View v, DragEvent e) {

    if (e.getAction()==DragEvent.ACTION_DROP) {
        View view = (View) e.getLocalState();
        if (from.getChildCount() == 1){ Toast zToast = Toast.makeText(getApplicationContext(),
                "YOU GENIUS :-)", Toast.LENGTH_SHORT); zToast.show(); }

        if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
        {
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackgroundResource(R.drawable.topleftcoat);
            Toast myToast = Toast.makeText(getApplicationContext(),
                    "Well Done! :-)", Toast.LENGTH_SHORT);
            myToast.show();
            return true;
        } else if(view.getId()==R.id.circleImage && v.getId()==R.id.circleImage1){
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackgroundResource(R.drawable.toprightcoat);
            Toast aToast = Toast.makeText(getApplicationContext(),
                    "Well Done! :-)", Toast.LENGTH_SHORT);
            aToast.show();
            return true;
        } else if(view.getId()==R.id.triangleImage && v.getId()==R.id.triangleImage1){
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackgroundResource(R.drawable.bottomleftcoat);
            Toast bToast = Toast.makeText(getApplicationContext(),
                    "Well Done! :-)", Toast.LENGTH_SHORT);
            bToast.show();
            return true;
        }else if(view.getId()==R.id.triangleImage2 && v.getId()==R.id.triangleImage3){
            ViewGroup from = (ViewGroup) view.getParent();
            from.removeView(view);
            v.setBackgroundResource(R.drawable.bottomrightcoat);
            Toast cToast = Toast.makeText(getApplicationContext(),
                    "Well Done! :-)", Toast.LENGTH_SHORT);
            cToast.show();
            return true;

        }

        else {
            Toast newToast = Toast.makeText(getApplicationContext(),
                    "Oops! Try again :-)", Toast.LENGTH_SHORT);
            newToast.show();
            return true;
        }

    }
    return true;
}

public void onClick(View v) {
    switch (v.getId()) {

        case R.id.reset:
            Intent intent = getIntent();
            finish();
            startActivity(intent);



    }
}
public void onClick2(View view){
    Intent intent = new Intent(this, PuzzleActivity2.class);
    finish();
    startActivity(intent);
}
private boolean isFirstTime()
{
    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean ranBefore = preferences.getBoolean("RanBefore", false);
    if (!ranBefore) {

        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("RanBefore", true);
        editor.commit();
        topLevelLayout.setVisibility(View.VISIBLE);
        topLevelLayout.setOnTouchListener(new View.OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                topLevelLayout.setVisibility(View.INVISIBLE);
                return false;
            }

        });

    }
    return ranBefore;

}
}

Solution

  • You could use the getChildCount() method on your ViewGroup instance to determine the number of children left.