I just have a little question here. How can I make this arrow to back to previous activity? I mean, I know how to code it, but i can't find this arrow. Is it something, that is in e.g. android studio or is it something that i have to make by myself?
Asked
Active
Viewed 893 times
-4

Mate
- 192
- 2
- 18
-
1go to new and create new drawable from it. – Nouman Ch Mar 18 '18 at 17:13
-
show your code so that we can find out what is the wrong – Shalauddin Ahamad Shuza Mar 18 '18 at 17:13
-
1Possible duplicate of [How to add an image to the "drawable" folder in Android Studio?](https://stackoverflow.com/questions/29047902/how-to-add-an-image-to-the-drawable-folder-in-android-studio) – Nouman Ch Mar 18 '18 at 17:15
-
What are you trying to do actually? do you want to change the default drawable? – Bertram Gilfoyle Mar 18 '18 at 17:19
-
I want to add it, cause if i launch my main activity and go to second activity i want to have this arrow like on pic that can bring me back to main activity. Now I have only button but this arrow looks better. – Mate Mar 18 '18 at 17:24
3 Answers
1
There are many ways to achieve that, here is what I suggest:
Layout:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator" />
Activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// back button pressed
finish();
}
});

Rainmaker
- 10,294
- 9
- 54
- 89
0
Use below code to back your previous Activity.
Code is Here :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
}

Abhishek kumar
- 4,347
- 8
- 29
- 44
0
To show top back arrow you have to enable the display home button getSupportActionBar().setDisplayHomeAsUpEnabled(true)
and you will get the arrow click event at onOptionsItemSelected
method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId() == android.R.id.home){
// do your task on arrow click
return true;
}
return super.onOptionsItemSelected(item)
}

Shalauddin Ahamad Shuza
- 3,501
- 21
- 26