Setting OnClickListener on the action bar home icon makes the icon lost in a click state

Setting the following: the home icon shows the state of pressing:

actionBar.setHomeButtonEnabled(true); 

But after installing OnClickListener, the home icon stops showing the click state:

 ImageView iconImage = (ImageView) activity.findViewById(android.R.id.home); iconImage.setOnClickListener(new android.view.View.OnClickListener() { @Override public void onClick(View v) { } }); 

Any idea on how to prevent a blackout?

+4
source share
2 answers

To handle clicking on the home icon, you do not need to install onClickListener , you need to do the following.

 public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == android.R.id.home) { //app icon in action bar clicked; go back //do something return true; } return super.onOptionsItemSelected(item); } 
+8
source
 public class MainActivity extends Activity { Fragment fr; FragmentManager fm; Button btn1,btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(true); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); fm = getFragmentManager(); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub fr = new frag22(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.fragment, fr); ft.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); // TODO Auto-generated method stub return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId() == android.R.id.home) { //app icon in action bar clicked; go back //do something Toast.makeText(getApplicationContext(), "set click", Toast.LENGTH_LONG).show(); return true; } return super.onOptionsItemSelected(item); } } 
0
source

All Articles