How can I put Back Arrow or Back Button on the top left side of Action Bar of Android App. Below is the code and screenshot.
I have gone through many tutorials and everytime I got some kind of error. So Now I posted my code here and I hope someone will surely help me. Thanks :)
@Override
protected void onCreate(Bundle savedInstanceState) {
setTitle("Search Station");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
editIntent = new Intent(this, EditStationActivity.class);
}
public void onResume(){
super.onResume();
updateList("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.add_station){
Intent intent = new Intent(this, AddStationActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public void findStation(View view) {
EditText keywrd = (EditText) findViewById(R.id.keyword);
String keyword = keywrd.getText().toString();
updateList(keyword);
}
public void updateList(String condition) {
DbHandler dbh = new DbHandler(this);
SQLiteDatabase db = dbh.getReadableDatabase();
String[] projection = {
DbHandler.StationsHandler.COLUMN_NAME_ENTRY_ID,
DbHandler.StationsHandler.COLUMN_NAME_TITLE
};
Cursor c;
if (!condition.equals("")) {
String cond = DbHandler.StationsHandler.COLUMN_NAME_TITLE+" LIKE ?";
String[] params = {
"%"+condition+"%"
};
c = db.query(DbHandler.StationsHandler.Table_Name, projection, cond, params, null, null, null);
} else
c = db.query(DbHandler.StationsHandler.Table_Name, projection, null, null, null, null, null);
if(c.getCount() == 0) {
items = new String[1];
items[0] = "No Station found";
ids = new String[1];
ids[0] = "0";
} else {
items = new String[c.getCount()];
ids = new String[c.getCount()];
c.moveToFirst();
int i = 0;
do {
ids[i] = c.getString(0);
items[i++] = c.getString(1);
}
while (c.moveToNext());
}
db.close();
dbh.close();
ListView stationList = (ListView) findViewById(R.id.station_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
stationList.setAdapter(adapter);
stationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(ids[position].equals("0"))
return;
editIntent.putExtra(EDITID, ids[position]);
startActivity(editIntent);
}
});
}
Extend your activity as ActionBarActivity and use the following in your onCreate() method
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Add this to your activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With