Skip to content

Commit

Permalink
Add left/right sliding animations to the navbar
Browse files Browse the repository at this point in the history
  • Loading branch information
meenbeese committed Nov 20, 2024
1 parent 9ef6a69 commit 536a543
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/src/main/java/org/torproject/android/OrbotActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController

Expand Down Expand Up @@ -85,6 +86,42 @@ class OrbotActivity : BaseActivity() {

bottomNavigationView.menu.findItem(R.id.connectFragment).isChecked = true

val navOptionsLeftToRight = NavOptions.Builder()
.setEnterAnim(R.anim.slide_in_right)
.setExitAnim(R.anim.slide_out_left)
.setPopEnterAnim(R.anim.slide_in_right)
.setPopExitAnim(R.anim.slide_out_left)
.build()

val navOptionsRightToLeft = NavOptions.Builder()
.setEnterAnim(R.anim.slide_in_left)
.setExitAnim(R.anim.slide_out_right)
.setPopEnterAnim(R.anim.slide_in_left)
.setPopExitAnim(R.anim.slide_out_right)
.build()

var lastSelectedItemId = R.id.connectFragment

bottomNavigationView.setOnItemSelectedListener { item ->
if (item.itemId == lastSelectedItemId) {
return@setOnItemSelectedListener true
}

val navOptions = if (item.itemId > lastSelectedItemId) {
navOptionsLeftToRight
} else {
navOptionsRightToLeft
}
lastSelectedItemId = item.itemId

when (item.itemId) {
R.id.connectFragment -> navController.navigate(R.id.connectFragment, null, navOptions)
R.id.kindnessFragment -> navController.navigate(R.id.kindnessFragment, null, navOptions)
R.id.moreFragment -> navController.navigate(R.id.moreFragment, null, navOptions)
}
true
}

with(LocalBroadcastManager.getInstance(this)) {
registerReceiver(
orbotServiceBroadcastReceiver, IntentFilter(OrbotConstants.LOCAL_ACTION_STATUS)
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/anim/slide_in_left.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:duration="300"/>
</set>
7 changes: 7 additions & 0 deletions app/src/main/res/anim/slide_in_right.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="0%"
android:duration="300"/>
</set>
7 changes: 7 additions & 0 deletions app/src/main/res/anim/slide_out_left.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="300"/>
</set>
7 changes: 7 additions & 0 deletions app/src/main/res/anim/slide_out_right.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="300"/>
</set>
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_orbot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down

0 comments on commit 536a543

Please sign in to comment.