Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8285509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:29:11+00:00 2026-06-08T11:29:11+00:00

At the moment when I press a button the image is always the phone

  • 0

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;
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:29:13+00:00Added an answer on June 8, 2026 at 11:29 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a question game and I want to change the answer button pressed
At moment I want to implement picture upload without using any plug-ins. My upload
At the moment all I want this program to do is run without any
for the moment the script does like this: click on the link edit the
Our webapp has a form with fields and values that change depending on values
I want to be able to do a combination of keypresses and mouseclicks simultaneously,
I have a ListView, and I want that when I press an item, go
I'm trying to implement this application. At the moment I have designed tabs on
This is a very naive issue. I want to call two different class from
What's happening: I open my app and then press the home button to leave

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.