erik
erik

Reputation: 4948

Listener not firing in my activiy

I am trying to add a click listener and a long click listener to my Main Activity but its not firing.. below is my code, don't know why its not firing, can someone tell me what i am missing? Is it even possible to add a listener to the entire activity?

public class HomeFavesActivity extends Activity implements OnClickListener, OnLongClickListener{

    private static final String TAG = "HomeFavesCatovoty";
    private ArrayList<Integer> mIcons = new ArrayList<Integer>();
    IconTray iconTray; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.v(TAG, "CREATED");

       DataBaseManager db = new DataBaseManager(this);
        /*db.getWritableDatabase();
        db.AddHomeScreenIcon(1);
        db.AddHomeScreenIcon(2);
        db.AddHomeScreenIcon(3);
        db.AddHomeScreenIcon(4);
        db.AddHomeScreenIcon(5);
        db.AddHomeScreenIcon(6);
        db.AddHomeScreenIcon(7);*/

        getScreenIcons(db);

    }

    private void getScreenIcons(DataBaseManager db){
        mIcons = db.getScreenIcons("TestScreenIcons");
        Log.v(TAG, "List Length:"+  mIcons.size());

        RelativeLayout rl = (RelativeLayout)findViewById(R.id.main);


        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        int screenWidth = metrics.heightPixels;
        int screenHeight = metrics.widthPixels;

        iconTray = new IconTray(this, mIcons, null, screenWidth, screenHeight);
        /*
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
        params.addRule(rl.CENTER_IN_PARENT);
        iconTray.setLayoutParams(params);*/
        rl.addView(iconTray);

        rl.setOnLongClickListener(this);

        rl.setOnClickListener(this);


    }

    public void onClick() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "woot", 
                Toast.LENGTH_SHORT).show();
    }

    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        iconTray.longClickMode();
        return false;
    }

    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        iconTray.clickMode();
    }




}

OK so now the LongClick Listener works but when if add the line for the click it won't publish because it generates an error..

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (HomeFavesActivity)

Upvotes: 0

Views: 256

Answers (1)

dymmeh
dymmeh

Reputation: 22306

You need to associate a view with the onclick listener.

ex.

Button button = (Button)findViewById(R.id.my_button);
button.setOnClickListener(this);

then whenever you click your button it will call the listener you implement. If you want to have your entire activity be selectable you can select the parent layout (like a LinearLayout or RelativeLayout in your main.xml file) and follow the same code above but for that type of view. Like so:

LinearLayout parent = (LinearLayout)findViewById(R.id.parent_layout);
parent.setOnClickListener(this);

Upvotes: 1

Related Questions