ncg33
ncg33

Reputation: 1

OnTouch Change Background Image

I am working on changing the background image of my app when the user presses a quarter of the screen. I have posted what I have done so far below, but is not working. I am also not sure how to reference the png picture that I have saved into my resource folder. Thanks.

    public class TouchtwoActivity extends Activity implements OnTouchListener {
     /** Called when the activity is first created. */
   float x,y;
   TextView tv = (TextView)findViewById(R.id.textv);
   View vv;
         @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.eight); 

    vv.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    x = event.getX();
    y = event.getY();
  if(x > getWindowManager().getDefaultDisplay().getWidth()/2) 
  {
      changeImage(); 
  }
    return true;        
}

public void changeImage()
{
     getWindow().setBackgroundDrawable(Drawable.createFromPath("name of png from drawable));
}

}

Upvotes: 0

Views: 862

Answers (2)

ShineDown
ShineDown

Reputation: 1879

First add the view (vv) u created as child of a layout u have created in the XML. Then set height and width to the view (vv) using layout params.

Or u can create a view in XML and get it using findViewById just like u did to the text view.Then write vv.setOnTouchListener(this);

Then in onTouch try

getWindow().setBackgroundDrawableResource(R.drawable.background_2);

Upvotes: 2

April Smith
April Smith

Reputation: 1830

Try this

getWindow().setBackgroundDrawableResource(R.drawable.background_2);

Upvotes: 0

Related Questions