Code Monkey home page Code Monkey logo

country's Introduction

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.

country's People

Contributors

naveenkumarkuppan avatar nkuppan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.