Skip to content

Commit

Permalink
Refactor Language Selection
Browse files Browse the repository at this point in the history
- Remove Navigation Drawer
- Add language selection as an Action item
  • Loading branch information
ndegwamartin committed Jul 7, 2021
1 parent 17fec0b commit 101075d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.fhir.datacapture.gallery.databinding.ActivityMainBinding
import com.google.android.fhir.datacapture.gallery.utils.LanguageSwitcherUtils
import com.google.android.material.navigation.NavigationView
import java.util.Locale

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var drawerToggle: ActionBarDrawerToggle
private lateinit var languageList: List<LanguageSwitcherUtils.Language>

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -40,17 +37,21 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
setContentView(binding.root)

languageList = LanguageSwitcherUtils.getLanguageList(this)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.top_bar_menu, menu)

setUpNavigationView()
menu.findItem(R.id.action_submit).isVisible = false
menu.findItem(R.id.action_language).isVisible = true
return true
}

private fun refreshToSelectedLanguage(
language: LanguageSwitcherUtils.Language,
context: Activity
) {

getNavigationView().menu.findItem(R.id.switch_language).title = language.displayName

val sharedPref =
context?.getSharedPreferences(
BuildConfig.APPLICATION_ID.plus(this.javaClass.canonicalName),
Expand Down Expand Up @@ -82,45 +83,15 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
applyOverrideConfiguration(newConfiguration)
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.switch_language ->
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_language -> {
LanguageSwitcherUtils.renderSelectLanguageDialog(this, languageList) { _, i ->
refreshToSelectedLanguage(languageList[i], this)
}
else -> false
}

return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {

if (drawerToggle.onOptionsItemSelected(item)) {
return true
true
}
else -> super.onOptionsItemSelected(item)
}
return super.onOptionsItemSelected(item)
}

private fun setUpNavigationView() {

drawerToggle =
ActionBarDrawerToggle(
this,
binding.drawerLayout,
R.string.nav_app_bar_open_drawer_description,
R.string.nav_app_bar_navigate_up_description
)
binding.drawerLayout.addDrawerListener(drawerToggle)
drawerToggle.syncState()

supportActionBar?.setDisplayHomeAsUpEnabled(true)

val navigationView = getNavigationView()
navigationView.setNavigationItemSelectedListener(this)

navigationView.menu.findItem(R.id.switch_language).title = Locale.getDefault().displayName
}

private fun getNavigationView() = findViewById<View>(R.id.nav_view) as NavigationView
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class QuestionnaireContainerFragment : Fragment() {
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.top_bar_menu, menu)
menu.findItem(R.id.action_submit).isVisible = true
menu.findItem(R.id.action_language).isVisible = false
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand Down
24 changes: 5 additions & 19 deletions datacapturegallery/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,20 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<androidx.drawerlayout.widget.DrawerLayout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
>

<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_nav_graph"
app:defaultNavHost="true"
/>

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_drawer_menu"
/>

</androidx.drawerlayout.widget.DrawerLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
9 changes: 0 additions & 9 deletions datacapturegallery/src/main/res/layout/nav_header.xml

This file was deleted.

9 changes: 0 additions & 9 deletions datacapturegallery/src/main/res/menu/nav_drawer_menu.xml

This file was deleted.

7 changes: 7 additions & 0 deletions datacapturegallery/src/main/res/menu/top_bar_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<item
android:id="@+id/action_language"
android:orderInCategory="300"
android:title="@string/select_language"
android:icon="@drawable/outline_language_24"
app:showAsAction="always"
/>

<item
android:id="@+id/action_submit"
Expand Down

0 comments on commit 101075d

Please sign in to comment.