sssaini
sssaini

Reputation: 405

start activity custom preference android

I have a custom preference consisting of four imageViews. I want to start an activity when just the 4th of the 4 imageViews is clicked.

public class ImagePreference4Locks extends Preference {
public ImagePreference4Locks(final Context context, final AttributeSet attrs,
            final int defStyle) {
        super(context, attrs, defStyle);
        this.setLayoutResource(R.layout.imagepref4locks);
}

@Override
        protected void onBindView(View view) {final Context m = this.getContext();
        super.onBindView(view);

        final ImageView thumb_1 = (ImageView) view.findViewById(R.id.thumb_1);
        final ImageView thumb_2 = (ImageView) view.findViewById(R.id.thumb_2);
        final ImageView thumb_3 = (ImageView) view.findViewById(R.id.thumb_3);
        final ImageView thumb_4 = (ImageView) view.findViewById(R.id.thumb_4);
        .........

if (thumb_4 != null) {

            thumb_4.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
            /////////////////////////////////////////////
            Start Activity here:
}}

How can I start an activity here because I cant get the context.......

Using this wouldnt work:

Intent cc=new Intent(this.getContext(),HomeActivity.class);
this.getContext().startActivity(cc);

Upvotes: 0

Views: 171

Answers (1)

MaxChinni
MaxChinni

Reputation: 1216

In the constuctor you are forced to pass the context, so why don't you create a private variable such as

private static Context mContext;

and then you initialize it there?

public ImagePreference4Locks(final Context context, final AttributeSet attrs,
        final int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
    this.setLayoutResource(R.layout.imagepref4locks);
}

Now this code should work

Intent cc = new Intent(mContext, HomeActivity.class);
mContext.startActivity(cc);

Upvotes: 1

Related Questions