Skip to content

Commit

Permalink
Merge pull request #77 from angrezichatterbox/Back-Button-for-Action-bar
Browse files Browse the repository at this point in the history
feat:Implemented back button in the action bar
  • Loading branch information
andrewtavis authored Sep 4, 2024
2 parents a41cbb9 + a677fb3 commit 9693b6e
Show file tree
Hide file tree
Showing 24 changed files with 262 additions and 88 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ dependencies {
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.recyclerview:recyclerview:1.3.2'
implementation 'androidx.cardview:cardview:1.0.0'
implementation "androidx.viewpager2:viewpager2:1.0.0"
implementation "androidx.viewpager2:viewpager2:1.1.0"


api 'joda-time:joda-time:2.10.13'
api 'com.github.tibbi:RecyclerView-FastScroller:e7d3e150c4'
Expand Down
24 changes: 2 additions & 22 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
android:localeConfig="@xml/locales_config"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme.Base">
<activity android:name=".activities.MainActivity"
android:exported="true">
<intent-filter>
Expand All @@ -31,7 +31,7 @@
<activity
android:name=".activities.SplashActivity"
android:exported="false"
android:theme="@style/SplashTheme" />
/>

<activity-alias
android:name=".activities.SplashActivity.Orange"
Expand Down Expand Up @@ -170,26 +170,6 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".fragments.AboutFragment"
android:label="@string/app.about.title"
android:parentActivityName=".fragments.MainFragment" />
<activity
android:name=".fragments.WikimediaScribeFragment"
android:label="@string/app.about.wikimedia"
android:parentActivityName=".fragments.MainFragment" />
<activity
android:name=".fragments.PrivacyPolicyFragment"
android:label="@string/app.about.privacyPolicy"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".fragments.ThirdPartyFragment"
android:label="@string/app.about.thirdParty"
android:parentActivityName=".activities.MainActivity" />
<activity
android:name=".activities.LanguageSettings"
android:label="Language Settings"
android:parentActivityName=".fragments.MainFragment" />
</application>

</manifest>
94 changes: 85 additions & 9 deletions app/src/main/java/be/scri/activities/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package be.scri.activities


import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.bottomnavigation.BottomNavigationView
import be.scri.R
import be.scri.adapters.ViewPagerAdapter
import be.scri.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView


class MainActivity : SimpleActivity() {

Expand All @@ -17,9 +22,24 @@ class MainActivity : SimpleActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.displayOptions = androidx.appcompat.app.ActionBar.DISPLAY_SHOW_CUSTOM;
supportActionBar?.setDisplayShowCustomEnabled(true);
supportActionBar?.setCustomView(R.layout.custom_action_bar_layout);
supportActionBar?.elevation = 0F
val layoutParams = supportActionBar?.customView?.layoutParams
layoutParams?.height = 1000
supportActionBar?.customView?.layoutParams = layoutParams
setActionBarTitle(R.string.app_launcher_name)
val mButton = supportActionBar?.customView?.findViewById<Button>(R.id.button)
val mImage = getDrawable(R.drawable.chevron)
mButton?.setCompoundDrawablesWithIntrinsicBounds(mImage, null, null, null)
mButton?.compoundDrawablePadding = 2
mButton?.visibility = View.GONE

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)


viewPager = findViewById(R.id.view_pager)
bottomNavigationView = findViewById(R.id.bottom_navigation)

Expand All @@ -33,18 +53,24 @@ class MainActivity : SimpleActivity() {
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
bottomNavigationView.menu.getItem(position).isChecked = true
when(position) {
when (position) {
0 -> {
supportActionBar?.title = getString(R.string.app_launcher_name)
binding.fragmentContainer.visibility = View.GONE
setActionBarTitle(R.string.app_launcher_name)
}

1 -> {
supportActionBar?.title = getString(R.string.app_settings_title)
binding.fragmentContainer.visibility = View.GONE
setActionBarTitle(R.string.app_settings_title)
}

2 -> {
supportActionBar?.title = getString(R.string.app_about_title)
binding.fragmentContainer.visibility = View.GONE
setActionBarTitle(R.string.app_about_title)
}

else -> {
binding.fragmentContainer.visibility = View.VISIBLE
}
}
}
Expand All @@ -55,30 +81,80 @@ class MainActivity : SimpleActivity() {
when (menuItem.itemId) {
R.id.installation -> {
viewPager.setCurrentItem(0, true)
supportActionBar?.title = getString(R.string.app_launcher_name)
binding.fragmentContainer.visibility = View.GONE
setActionBarTitle(R.string.app_launcher_name)
true
}

R.id.info -> {
viewPager.setCurrentItem(2, true)
binding.fragmentContainer.visibility = View.GONE
supportActionBar?.title = getString(R.string.app_about_title)
setActionBarTitle(R.string.app_about_title)
true
}

R.id.settings -> {
viewPager.setCurrentItem(1, true)
binding.fragmentContainer.visibility = View.GONE
supportActionBar?.title = getString(R.string.app_settings_title)
setActionBarTitle(R.string.app_settings_title)
true
}

else -> {
false
false
}
}
}
}

fun showFragmentContainer() {
binding.fragmentContainer.visibility = View.VISIBLE
}

fun setActionBarTitle(title: Int) {
supportActionBar?.customView?.findViewById<TextView>(R.id.name)?.text = getString(title)
}

fun setActionBarButtonVisible() {
supportActionBar?.customView?.findViewById<Button>(R.id.button)?.visibility = View.VISIBLE
}

fun setActionBarButtonInvisible() {
supportActionBar?.customView?.findViewById<Button>(R.id.button)?.visibility = View.GONE
}

fun setActionBarButtonFunction(page: Int, title: Int) {
val button = supportActionBar?.customView?.findViewById<Button>(R.id.button)
button?.setOnClickListener {
val viewpager = findViewById<ViewPager2>(R.id.view_pager)
val frameLayout = findViewById<ViewGroup>(R.id.fragment_container)
if (viewpager.currentItem == page) {

frameLayout.visibility = View.GONE
viewpager.setCurrentItem(page, true)

}
frameLayout.visibility = View.GONE
unsetActionBarLayoutMargin()
setActionBarTitle(title)
button.visibility = View.GONE

}
}
fun setActionBarLayoutMargin() {
val textView = supportActionBar?.customView?.findViewById<TextView>(R.id.name)
val params = textView?.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = -50
params.bottomMargin = 30
textView.layoutParams = params
}
fun unsetActionBarLayoutMargin() {
val textView = supportActionBar?.customView?.findViewById<TextView>(R.id.name)
val params = textView?.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = 50
params.bottomMargin = 0
textView.layoutParams = params
}


}
2 changes: 1 addition & 1 deletion app/src/main/java/be/scri/extensions/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fun Activity.appLaunched(appId: String) {
checkAppIconColor()
} else if (!baseConfig.wasOrangeIconChecked) {
baseConfig.wasOrangeIconChecked = true
val primaryColor = resources.getColor(R.color.color_primary)
val primaryColor = resources.getColor(R.color.you_background_color)
if (baseConfig.appIconColor != primaryColor) {
getAppIconColors().forEachIndexed { index, color ->
toggleAppIconColor(appId, index, color, false)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/be/scri/fragments/AboutFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ class AboutFragment : Fragment() {
getParentFragmentManager().popBackStack()
}
callback.isEnabled = true
(requireActivity() as MainActivity).setActionBarTitle(R.string.app_about_title)
(requireActivity() as MainActivity).unsetActionBarLayoutMargin()
(requireActivity() as MainActivity).setActionBarButtonInvisible()
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(requireActivity() as MainActivity).supportActionBar?.title = getString(R.string.app_settings_title)
setupRecyclerViews()
}

Expand Down
27 changes: 25 additions & 2 deletions app/src/main/java/be/scri/fragments/LanguageSettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.fragment.app.Fragment
Expand All @@ -24,10 +25,15 @@ class LanguageSettingsFragment : Fragment() {
super.onCreate(savedInstanceState)
val viewpager = requireActivity().findViewById<ViewPager2>(R.id.view_pager)
val frameLayout = requireActivity().findViewById<ViewGroup>(R.id.fragment_container)
(requireActivity() as MainActivity).setActionBarButtonFunction(3,R.string.app_settings_title)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
viewpager.setCurrentItem(3, true);
(requireActivity() as MainActivity).supportActionBar?.setDisplayHomeAsUpEnabled(false);
(requireActivity() as MainActivity).unsetActionBarLayoutMargin()
}
(requireActivity() as MainActivity).setActionBarLayoutMargin()
(requireActivity() as MainActivity).supportActionBar?.customView?.findViewById<Button>(R.id.button)?.text = getString(R.string.app_settings_title)

callback.isEnabled = true

}
Expand All @@ -40,9 +46,11 @@ class LanguageSettingsFragment : Fragment() {
override fun handleOnBackPressed() {
val viewpager = requireActivity().findViewById<ViewPager2>(R.id.view_pager)
val frameLayout = requireActivity().findViewById<ViewGroup>(R.id.fragment_container)
(requireActivity() as MainActivity).unsetActionBarLayoutMargin()
if (viewpager.currentItem == 3) {
viewpager.setCurrentItem(3, true)
frameLayout.visibility = View.GONE
(requireActivity() as MainActivity).unsetActionBarLayoutMargin()
} else {
if (parentFragmentManager.backStackEntryCount > 0) {
parentFragmentManager.popBackStack()
Expand All @@ -62,10 +70,13 @@ class LanguageSettingsFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val language = arguments?.getString("LANGUAGE_EXTRA") ?: return
(requireActivity() as MainActivity).supportActionBar?.title = language
val titleInt = getLanguageStringFromi18n(language)
(requireActivity() as MainActivity).setActionBarTitle(titleInt)
(requireActivity() as MainActivity).showFragmentContainer()

(requireActivity() as MainActivity).setActionBarButtonVisible()
(requireActivity() as MainActivity).setActionBarButtonFunction(3,R.string.app_settings_title)
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) {
(requireActivity() as MainActivity).setActionBarButtonInvisible()
parentFragmentManager.beginTransaction()
.replace(R.id.fragment_container, SettingsFragment())
.addToBackStack(null)
Expand Down Expand Up @@ -110,4 +121,16 @@ class LanguageSettingsFragment : Fragment() {
_binding = null
}

fun getLanguageStringFromi18n(language: String): Int {
when (language) {
"German" -> return R.string._global_german
"French" -> return R.string._global_french
"Spanish" -> return R.string._global_spanish
"Italian" -> return R.string._global_italian
"Russian" -> return R.string._global_russian
"Portuguese" -> return R.string._global_portuguese
"Swedish" -> return R.string._global_swedish
else -> return R.string._global_english
}
}
}
2 changes: 2 additions & 0 deletions app/src/main/java/be/scri/fragments/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
import be.scri.R
import be.scri.activities.MainActivity
import be.scri.databinding.FragmentMainBinding
import be.scri.dialogs.ConfirmationAdvancedDialog

Expand Down Expand Up @@ -47,6 +48,7 @@ class MainFragment : Fragment() {
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
getParentFragmentManager().popBackStack()
}
(requireActivity() as MainActivity).setActionBarButtonInvisible()
callback.isEnabled = true
return binding.root
}
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/be/scri/fragments/PrivacyPolicyFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import be.scri.R
Expand All @@ -21,11 +24,25 @@ class PrivacyPolicyFragment : Fragment(){

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val viewpager = requireActivity().findViewById<ViewPager2>(R.id.view_pager)
val frameLayout = requireActivity().findViewById<ViewGroup>(R.id.fragment_container)
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
viewpager.setCurrentItem(2, true);
(requireActivity() as MainActivity).unsetActionBarLayoutMargin()
}
(requireActivity() as MainActivity).setActionBarButtonVisible()
(requireActivity() as MainActivity).setActionBarTitle(R.string.app_about_privacyPolicy)
(requireActivity() as MainActivity).setActionBarLayoutMargin()
val textView = (requireActivity() as MainActivity).supportActionBar?.customView?.findViewById<TextView>(R.id.name)
(requireActivity() as MainActivity).supportActionBar?.customView?.findViewById<Button>(R.id.button)?.text = getString(R.string.app_about_title)

textView?.let{
val params = it.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = -50
it.layoutParams = params
}

callback.isEnabled = true


Expand All @@ -35,12 +52,17 @@ class PrivacyPolicyFragment : Fragment(){
savedInstanceState: Bundle?
): View? {
binding = FragmentPrivacyPolicyBinding.inflate(inflater, container, false)
(requireActivity() as MainActivity).supportActionBar?.title = getString(R.string.app_about_privacyPolicy)
(requireActivity() as MainActivity).showFragmentContainer()
(requireActivity() as MainActivity).setActionBarTitle(R.string.app_about_privacyPolicy)
(requireActivity() as MainActivity).setActionBarButtonFunction(2,R.string.app_about_title)
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val viewpager = requireActivity().findViewById<ViewPager2>(R.id.view_pager)
val frameLayout = requireActivity().findViewById<ViewGroup>(R.id.fragment_container)
(requireActivity() as MainActivity).setActionBarTitle(R.string.app_about_title)
val textView = (requireActivity() as MainActivity).supportActionBar?.customView?.findViewById<TextView>(R.id.name)
(requireActivity() as MainActivity).setActionBarButtonInvisible()
(requireActivity() as MainActivity).unsetActionBarLayoutMargin()
if (viewpager.currentItem == 2) {
viewpager.setCurrentItem(2, true)
frameLayout.visibility = View.GONE
Expand All @@ -53,7 +75,6 @@ class PrivacyPolicyFragment : Fragment(){
}
}

(requireActivity() as MainActivity).supportActionBar?.title = getString(R.string.app_about_title)
}
})
return binding.root
Expand Down
Loading

0 comments on commit 9693b6e

Please sign in to comment.