Skip to content

To select country information such as country name, flag, dial code, currency and states. Android library as a list implementation with activity and dialogs.

Notifications You must be signed in to change notification settings

nkuppan/country

Repository files navigation

Country Selection Library

API Workflow

Country selection library. Can launch this selection picker as an activity, fragment, dialog or bottom sheet to show the list of country with flag. Please follow the below implementation to access the flag selection picker.

XML Sample Compose Sample

XML Activity XML Dialog XML Bottom Sheet
XML Activity XML Dialog XML Bottom Sheet
Compose New Page Compose Dialog Dark Theme
Compose New Page Compose Dialog Dark Theme

How to add to your project

Sample implementation gif for country selection and using search functionality

Add repository info in your root project gradle file

// project.gradle
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add this below in your app.gradle

// app.gradle

android {
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
}

dependencies {
    implementation("com.github.nkuppan.country:country:${latestVersion}")
    implementation("com.github.nkuppan.country:countrycompose:${latestVersion}")
}

Implementation

Simple steps to achieve. Call country search activity with result.

Starting country selection as activity based

XML Way

Calling as an activity:

//Registering result receiver as a global variable or registering before Lifecycle.Event.CREATED
import com.github.nkuppan.country.utils.getCountryImage
import com.github.nkuppan.country.utils.launchCountrySelectionActivity

private val countrySelectionReceiver = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == Activity.RESULT_OK) {

        val country: Country = result.getSelectedCountryData()

        if (country != null) {
            changeValues(country)
        }
    }
}

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

    setContentView(R.layout.main_activity)

    binding.searchActivity.setOnClickListener {
        countrySelectionReceiver.launchCountrySelectionActivity(this)
    }
}

Calling as an activity with result (Legacy way):

activity.launchCountrySelectionActivity()

You will receive your result once the user is selected the country

import com.github.nkuppan.country.utils.getSelectedCountryData
import com.github.nkuppan.country.utils.isCountrySelectionResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (isCountrySelectionResult(requestCode, resultCode)) {
        val country: Country? = data?.getSelectedCountryData()

        if (country != null) {
            changeValues(country)
        }
    }
}

Calling as a dialog:

import com.github.nkuppan.country.utils.openCountrySelectionDialog

supportFragmentManager.openCountrySelectionDialog {
    changeValues(it)
}

Calling as a bottom sheet:

import com.github.nkuppan.country.utils.openCountrySelectionBottomSheet

supportFragmentManager.openCountrySelectionBottomSheet {
    changeValues(it)
}

Compose Way

Calling as a compose dialog:

Call the dialog inside compose like below. It will open the country selection as dialog with search option. Handle the dialog dismiss on the callback.

import com.github.nkuppan.countrycompose.presentation.country.CountrySelectionDialog

CountrySelectionDialog(onDismissRequest = {/*TODO Close dialog*/}) { country ->
    message = "Selected Country ${country.name} & ${country.countryCode}"
}

Calling as a compose page:

Call the page inside compose like below. It will open the country selection as new page with search option. Handle the page backpress on the callback. You can use this inside the composable using navigation-compose as well.

import com.github.nkuppan.countrycompose.presentation.country.CountrySelectionPage

CountrySelectionPage(onDismissRequest = {/*TODO Close page*/}) { country ->
    message = "Selected Country ${country.name} & ${country.countryCode}"
}

License

Apache Version 2.0

Copyright (C) 2019 Naveen Kumar Kuppan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.