Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choose image based on switch and case for notifcation

Tags:

android

At the moment when I press a button the image is always the phone but what I want is to be able to change this picture depending on what button is pressed.

eg. button imageButton1 is pressed set the notification image to the car

NotificationManager nm;
static final int uniqueID = 101;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button stat = (Button) findViewById(R.id.btnSet);
    stat.setOnClickListener(this);
    ImageButton img1 = (ImageButton) findViewById(R.id.imageButton1); 
    img1.setOnClickListener(this);
    ImageButton img2 = (ImageButton) findViewById(R.id.imageButton2); 
    img2.setOnClickListener(this);
    ImageButton img3 = (ImageButton) findViewById(R.id.imageButton3); 
    img3.setOnClickListener(this);
    ImageButton img4 = (ImageButton) findViewById(R.id.imageButton4); 
    img4.setOnClickListener(this);
    ImageButton img5 = (ImageButton) findViewById(R.id.imageButton5); 
    img5.setOnClickListener(this);
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.cancel(uniqueID);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    int picture = R.drawable.phone;
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.imageButton1:
        // do something
        picture = (R.drawable.car);
        break;
    case R.id.imageButton2:
        // do something else
        picture = (R.drawable.clock);
        break;
    case R.id.imageButton3:
        // do something else
        picture = (R.drawable.globe);
        break;
    case R.id.imageButton4:
        // do something else
        picture = (R.drawable.little_tv);
        break;
    case R.id.imageButton5:
        // do something else
        //setImageResource(R.drawable.phone);
        break;
    case R.id.btnSet: 
     // do something else
        EditText etT = (EditText) findViewById(R.id.etTitle);
        EditText etB = (EditText) findViewById(R.id.etBody);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        String setTitle = etT.getText().toString();
        String setBody = etB.getText().toString();
        String body = setBody;
        String title = setTitle;
        Notification n = new Notification(picture, body, System.currentTimeMillis());
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_VIBRATE;
        nm.notify(uniqueID, n);
        finish();
     break;
    }
}

}

like image 608
owenoliver1 Avatar asked Jul 27 '12 10:07

owenoliver1


1 Answers

remove declaration & initialization of 'picture' from onClick function and set it as class variable.

For better understanding refer below

NotificationManager nm;
static final int uniqueID = 101;
int picture;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Add below line here
    picture = R.drawable.phone;

    ...... All your code

}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // Remove below line
    // int picture = R.drawable.phone;

    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.imageButton1:
        // do something
        picture = (R.drawable.car);
        break;
    case R.id.imageButton2:
        // do something else
        picture = (R.drawable.clock);
        break;
    case R.id.imageButton3:
        // do something else
        picture = (R.drawable.globe);
        break;
    case R.id.imageButton4:
        // do something else
        picture = (R.drawable.little_tv);
        break;
    case R.id.imageButton5:
        // do something else
        //setImageResource(R.drawable.phone);
        break;
    case R.id.btnSet: 
     // do something else
        EditText etT = (EditText) findViewById(R.id.etTitle);
        EditText etB = (EditText) findViewById(R.id.etBody);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        String setTitle = etT.getText().toString();
        String setBody = etB.getText().toString();
        String body = setBody;
        String title = setTitle;
        Notification n = new Notification(picture, body, System.currentTimeMillis());
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_VIBRATE;
        nm.notify(uniqueID, n);
        finish();
     break;
    }
}

In your code whenever btnSet get click the "picture" variable get initialize with R.drawable.phone and then according to switch case it directly execute "case R.id.btnset"

I think you got my point

like image 95
rajpara Avatar answered Nov 05 '22 08:11

rajpara