0

This is my Code for the action bar i use.

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
        case R.id.icon:
        case R.id.Kur:
            Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
            startActivity(Doviz);
            finish();
            return true;
        case R.id.Hesap:
            Intent Hesap = new Intent(MainActivity.this, Genel.class);
            startActivity(Hesap);
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }

enter image description here

I'm trying to make it so when someone clicks the icon at top ( which is in red circle at picture) it should do the same thing as my "Döviz Kuru" Button at action bar. I have 2 problems 1 is i cant seem to make same Intent work for 2 cases in the menu 2nd is R.id.İcon doesn't reach to icon. I also tried home and the name i give to that .png neither worked.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
        startActivity(Doviz);
        finish();
        return true;
    case R.id.Kur:
        Intent Doviz = new Intent(MainActivity.this, MainActivity.class);
        startActivity(Doviz);
        finish();
        return true;
    case R.id.Hesap:
        Intent Hesap = new Intent(MainActivity.this, Genel.class);
        startActivity(Hesap);
        finish();
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

}

it gives an error abaut "Duplicate local variable Doviz". When i try to use the same Intent for 2 cases. the answer for the making 2 cases with same job is done like this.

case android.R.id.home:
    case R.id.Kur:
        Intent Doviz = new Intent(Genel.this, MainActivity.class);
        startActivity(Doviz);
        finish();
        return true;
Wince
  • 129
  • 1
  • 11
  • If you are going to use the CSS tag, then you should post the CSS, don't you think? Besides, "it should do the same thing as my "Döviz Kuru" Button at action bar" - what does "Döviz Kuru" Button do? Try to explain better your problem, please! – viriato Aug 06 '14 at 08:29
  • use `android.R.id.home` – Pragnesh Ghoda シ Aug 06 '14 at 08:53
  • well what the button does is seen in the code i gave u ; it starts a new Intent and closes last1 ; – Wince Aug 06 '14 at 08:58

2 Answers2

1

To enable Home Icon of Actionbar...

Read This Document For More Info : Navigate Up to Parent Activity

You should Do following...

write these lines in OnCreate() method of your activity..

   ActionBar actn = getActionBar();
   actn.setHomeButtonEnabled(true); //set home button clickable...
   actn.setDisplayHomeAsUpEnabled(true); //add up indicator with home button..

add this to onOptionsItemSelected()..

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
    case android.R.id.home:
        //this calls Home Icon of Actionbar...
        //your code..
        return true;
    default:
        return super.onOptionsItemSelected(item);
   }
}

for calling same code for both cases You can write cases like below..

   switch (item.getItemId()) {

    case android.R.id.home:
    case R.id.Kur: 

        //this code runs for both cases..
        //your code..
        return true;
    default:
        return super.onOptionsItemSelected(item);
   }

if you want to restart your activity.... you should use this code..this will restart current activity..

Intent intent = getIntent();
finish();
startActivity(intent);
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
  • i tried this but it doesn't seem to be working is there any way maybe i need to turn the home icon clickable or something? – Wince Aug 06 '14 at 08:55
  • also break doesn't work on due to boolean it needs return. and any ideas abaut how i can make it open same activity as Kur(doviz kuru ) button ? – Wince Aug 06 '14 at 09:00
  • ok i figured something out for that if u can just tell me why while R.id wasn't working android.R.id did work i would appreciate it . – Wince Aug 06 '14 at 09:14
  • 1
    android.R.id.home is built-in id for Home button...and R.id.home is defined by you for some view or drawable.. – Pragnesh Ghoda シ Aug 06 '14 at 09:17
0

How do I change the android actionbar title and icon

If you want to change it in code call

setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);

And set the values to whatever you please.

Community
  • 1
  • 1
Forke
  • 358
  • 3
  • 11