# XMaterial Compose Country Code Picker
Jetpack Compose Material Country Code Picker
The latest version is v2.13
This project was forked from togisoft, His Github repo and efforts are what laid the ground work for me to improve on it and create a more customizable version of his library,
If you are looking for a highly customizable and Material Country Phone Code Picker for Jetpack Compose you can use the package.
Languages:
New features will be added as time goes by and updates will be made. This project is open source without any profit motive.
For language support, you can send me a message. Chat me up on Linkedln
@Composable
fun MaterialCountryCodePicker(
text: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
showCountryCode: Boolean = true,
showCountryFlag: Boolean = true,
defaultCountry: CountryData,
pickedCountry: (CountryData) -> Unit,
error: Boolean = false,
showErrorText: Boolean = true,
flagPadding: PaddingValues = PaddingValues(horizontal = 10.dp),
countryItemBgShape: RoundedCornerShape = RoundedCornerShape(0.dp),
phonenumbertextstyle: TextStyle = MaterialTheme.typography.bodyMedium,
phonehintnumbertextstyle: TextStyle = MaterialTheme.typography.bodyMedium,
searchFieldPlaceHolderTextStyle: TextStyle = MaterialTheme.typography.bodyMedium,
searchFieldTextStyle: TextStyle = MaterialTheme.typography.bodyMedium,
searchFieldShapeCornerRadiusInPercentage: Int = 30,
textFieldShapeCornerRadiusInPercentage: Int = 30,
errorTextStyle: TextStyle = MaterialTheme.typography.bodyMedium,
appbartitleStyle: TextStyle = MaterialTheme.typography.titleLarge,
countryItemVerticalPadding: Dp = 8.dp,
countryItemHorizontalPadding: Dp = 8.dp,
countrytextstyle: TextStyle = MaterialTheme.typography.bodyMedium,
dialogcountrycodetextstyle: TextStyle = MaterialTheme.typography.bodyMedium,
showCountryCodeInDIalog: Boolean = true,
countrycodetextstyle: TextStyle = MaterialTheme.typography.bodyMedium,
showDropDownAfterFlag: Boolean = false,
isEnabled: Boolean = true,
flagShape: CornerBasedShape = RoundedCornerShape(0.dp),
isReadOnly: Boolean = false,
@DrawableRes errorIcon: Int? = null,
@DrawableRes dropDownIcon: Int? = null,
showClearIcon: Boolean = false,
@DrawableRes clearIcon: Int? = null,
colors: CCPColors,
onDone: () -> Unit = {},
placeholder: @Composable () -> Unit? = null,
)
@Composable
fun SelectCountryWithCountryCode() {
val context = LocalContext.current
var phoneCode by remember { mutableStateOf(getDefaultPhoneCode(context)) }
val phoneNumber = rememberSaveable { mutableStateOf("") }
var defaultLang by rememberSaveable { mutableStateOf(getDefaultLangCode(context)) }
var isValidPhone by remember { mutableStateOf(true) }
Column(
modifier = Modifier.padding(16.dp)
) {
MaterialCountryCodePicker(
pickedCountry = {
phoneCode = it.countryPhoneCode
defaultLang = it.countryCode
},
defaultCountry = getLibCountries().single { it.countryCode == defaultLang },
error = !isValidPhone,
text = phoneNumber.value,
onValueChange = { phoneNumber.value = it },
searchFieldPlaceHolderTextStyle = MaterialTheme.typography.bodyMedium,
searchFieldTextStyle = MaterialTheme.typography.bodyMedium,
phonenumbertextstyle = MaterialTheme.typography.bodyMedium,
countrytextstyle = MaterialTheme.typography.bodyMedium,
countrycodetextstyle = MaterialTheme.typography.bodyMedium,
showErrorText = true,
showCountryCodeInDIalog = true,
showDropDownAfterFlag = true,
textFieldShapeCornerRadiusInPercentage = 40,
searchFieldShapeCornerRadiusInPercentage = 40,
appbartitleStyle = MaterialTheme.typography.titleLarge,
countryItemBgShape = RoundedCornerShape(5.dp),
showCountryFlag = true,
showCountryCode = true,
isEnabled = true,
colors = ccpDefaultColors(
primaryColor = MaterialTheme.colorScheme.primary,
errorColor = MaterialTheme.colorScheme.error,
backgroundColor = MaterialTheme.colorScheme.background,
surfaceColor = MaterialTheme.colorScheme.surface,
outlineColor = MaterialTheme.colorScheme.outline,
disabledOutlineColor = MaterialTheme.colorScheme.outline.copy(0.1f),
unfocusedOutlineColor = MaterialTheme.colorScheme.onBackground.copy(0.3f),
textColor = MaterialTheme.colorScheme.onBackground.copy(0.7f),
cursorColor = MaterialTheme.colorScheme.primary,
topAppBarColor = MaterialTheme.colorScheme.surface,
countryItemBgColor = MaterialTheme.colorScheme.surface,
searchFieldBgColor = MaterialTheme.colorScheme.surface,
dialogNavIconColor = MaterialTheme.colorScheme.onBackground.copy(0.7f),
dropDownIconTint = MaterialTheme.colorScheme.onBackground.copy(0.7f)
)
)
val fullPhoneNumber = "$phoneCode${phoneNumber.value}"
val checkPhoneNumber = checkPhoneNumber(
phone = phoneNumber.value,
fullPhoneNumber = fullPhoneNumber,
countryCode = defaultLang
)
OutlinedButton(
onClick = {
isValidPhone = checkPhoneNumber
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
.height(
50.dp
)
) {
Text(text = "Phone Verify")
}
}
}
false
and [showCountryFlag] is true
just the flag will be showntrue
the textfield shows red outlines
and an error icon MaterialCountryCodePicker(
pickedCountry = {
phoneCode = it.countryPhoneCode
defaultLang = it.countryCode
},
defaultCountry = getLibCountries().single { it.countryCode == defaultLang },
error = !isValidPhone,
text = phoneNumber.value,
onValueChange = { phoneNumber.value = it },
dialogItemBuilder = { country, onclick ->
Row(Modifier.clickable {
onclick()
}) {
Image(
painterResource(
id = getFlags(
country.countryCode
)
), contentDescription = null
)
Text(text = country.cNames)
}
}
)
You can now change just the picker Locale to your local by passing the language code
LaunchedEffect(key1 = true) {
setLocale(context, "en")
}
In the build.gradle add maven central repository
repositories {
maven { url 'https://jitpack.io' }
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.TuleSimon:xMaterialccp:v2.13'
}
Feel free to contact me for any feature request or file an issue if a bug is encountered