Sunday, August 26, 2012

Set Button Visibility By clicking with only one Button ID


package com.demo.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class DemoButtonVisibilityActivity extends Activity
{

Button mButton;
Button mButton2;
Boolean mBoolean=true;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton=(Button)findViewById(R.id.btn1);
        mButton.setClickable(false);

        mButton2=(Button)findViewById(R.id.btn2);
        mButton2.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
       mButton.setClickable(true);
       mButton.setOnClickListener(new View.OnClickListener()
       {

@Override
public void onClick(View v)
{
if(mBoolean)
{
mBoolean=false;
mButton.setBackgroundResource(R.drawable.icon2);
}
else if (mBoolean==false)
{
mBoolean=true;
mButton.setBackgroundResource(R.drawable.icon);
}

}
});
}
return false;
}
});
     
    }
}