Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Search For Countries #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
buildscript {
ext.kotlin_version = '1.3.72'
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'

classpath 'com.android.tools.build:gradle:3.5.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
google()
jcenter()

}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 28
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 10
targetSdkVersion 22
minSdkVersion 21
targetSdkVersion 28
versionCode 106
versionName "1.0.6"
}
Expand All @@ -28,6 +42,16 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'

// Recyclerview
def recyclerview_version = "1.1.0-beta03"
implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
android.enableJetifier=true
android.useAndroidX=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':app'
16 changes: 11 additions & 5 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.heetch.countrypicker">
package="com.heetch.countrypicker">

<application
android:label="@string/app_name"
>
<application android:label="@string/app_name">
<activity android:name=".MainActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
</manifest>
12 changes: 7 additions & 5 deletions src/main/java/com/heetch/countrypicker/Country.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
public class Country {
private String isoCode;
private String dialingCode;
private String countryName;

public Country() {

}

public Country(String isoCode, String dialingCode) {
public Country(String name, String isoCode, String dialingCode) {
this.isoCode = isoCode;
this.dialingCode = dialingCode;
this.countryName = name;
}

public String getIsoCode() {
return isoCode;
}

public String getCountryName() {
return countryName;
}

public void setIsoCode(String isoCode) {
this.isoCode = isoCode;
}
Expand Down
134 changes: 86 additions & 48 deletions src/main/java/com/heetch/countrypicker/CountryListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,127 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Created by GODARD Tuatini on 07/05/15.
*/
public class CountryListAdapter extends BaseAdapter {
public class CountryListAdapter extends RecyclerView.Adapter<CountryListAdapter.ViewHolder> implements Filterable {

private final Context mContext;
private static final String TAG = CountryListAdapter.class.getSimpleName();
private LayoutInflater inflater;
private List<Country> countries;
private ArrayList<Country> countries;
private boolean showDialingCode;
public OnItemClickListener onItemClickListener;
private List<Country> countryListFiltered;

public CountryListAdapter(Context context, List<Country> countries, boolean showDialingCode) {
public CountryListAdapter(Context context, List<Country> countries, boolean showDialingCode,
OnItemClickListener onItemClickListener) {
mContext = context;
this.countries = countries;
this.countries = (ArrayList<Country>) countries;
countryListFiltered = countries;
this.showDialingCode = showDialingCode;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.onItemClickListener = onItemClickListener;
}

@Override
public int getCount() {
return countries.size();
}


@NonNull
@Override
public Object getItem(int position) {
return countries.get(position);
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_country, parent, false);
return new ViewHolder(view);
}

@Override
public long getItemId(int position) {
return 0;
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Country country = countryListFiltered.get(position);
holder.name.setText(country.getCountryName());
String drawableName = country.getIsoCode().toLowerCase(Locale.ENGLISH) + "_flag";
holder.icon.setImageResource(Utils.getMipmapResId(holder.icon.getContext(), drawableName));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
Item item;
Country country = countries.get(position);

if (convertView == null) {
item = new Item();
itemView = inflater.inflate(R.layout.item_country, parent, false);
item.setIcon((ImageView) itemView.findViewById(R.id.icon));
item.setName((TextView) itemView.findViewById(R.id.name));
itemView.setTag(item);
} else {
item = (Item) itemView.getTag();
}
public int getItemCount() {
return countryListFiltered.size();
}

item.getName().setText(new Locale(mContext.getResources().getConfiguration().locale.getLanguage(),
country.getIsoCode()).getDisplayCountry() + (showDialingCode ?
" (+" + country.getDialingCode() + ")" : ""));
public void update(List<Country> filterList) {
countries.clear();
countries.addAll(filterList);
notifyDataSetChanged();

// Load drawable dynamically from country code
String drawableName = country.getIsoCode().toLowerCase(Locale.ENGLISH) + "_flag";
item.getIcon().setImageResource(Utils.getMipmapResId(mContext, drawableName));
return itemView;
}

public static class Item {
private TextView name;
private ImageView icon;
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public ImageView getIcon() {
return icon;
}
TextView name;
ImageView icon;

public void setIcon(ImageView icon) {
this.icon = icon;
public ViewHolder(@NonNull View itemView) {
super(itemView);
icon = itemView.findViewById(R.id.icon);
name = itemView.findViewById(R.id.name);
itemView.setOnClickListener(this);
}

public TextView getName() {
return name;
@Override
public void onClick(View v) {
onItemClickListener.onItemClick(getAdapterPosition(), countryListFiltered.get(getAdapterPosition()));
}
}

public void setName(TextView name) {
this.name = name;
}
interface OnItemClickListener{
void onItemClick(int position, Country country);
}

@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
countryListFiltered = countries;
} else {
List<Country> filteredList = new ArrayList<>();
for (Country row : countries) {

// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getCountryName().toLowerCase()
.contains(charString.toLowerCase())) {
filteredList.add(row);
}
}

countryListFiltered = filteredList;
}

FilterResults filterResults = new FilterResults();
filterResults.values = countryListFiltered;
return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
countryListFiltered = (ArrayList<Country>) filterResults.values;

// refresh the list with filtered data
notifyDataSetChanged();
}
};
}
}
Loading