diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..780f9260 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# CHANGELOG + +# 1.0.1 + +Fixed bug with `android.view.ContextThemeWrapper cannot be cast to android.app.Activity` ([see](https://github.com/pchmn/MaterialChipsInput/issues/1) issue) + +## 1.0.0 + +First version of the library \ No newline at end of file diff --git a/README.md b/README.md index ac77197c..0edaf49c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Implementation of Material Design [Chips](https://material.io/guidelines/compone Demo ## Demo -[Download sample-v1.0.0.apk](https://github.com/pchmn/MaterialChipsInput/raw/master/docs/material-chips-input-sample-v1.0.0.apk) +[Download sample-v1.0.1.apk](https://github.com/pchmn/MaterialChipsInput/raw/master/docs/material-chips-input-sample-v1.0.1.apk) [Live demo on appetize.io](https://appetize.io/app/dmhu0jfyuypde3bw8wrjr7zn94?device=nexus5&scale=75&orientation=portrait&osVersion=7.0) @@ -28,7 +28,7 @@ allprojects { In your app level build.gradle : ```java dependencies { - compile 'com.github.pchmn:MaterialChipsInput:1.0.0' + compile 'com.github.pchmn:MaterialChipsInput:1.0.1' } ```

diff --git a/docs/material-chips-input-sample-v1.0.1.apk b/docs/material-chips-input-sample-v1.0.1.apk new file mode 100644 index 00000000..698ce72c Binary files /dev/null and b/docs/material-chips-input-sample-v1.0.1.apk differ diff --git a/library/build.gradle b/library/build.gradle index 0fb0cc05..ba4cbcb2 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 25 - versionCode 1 - versionName "1.0.0" + versionCode 2 + versionName "1.0.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/library/src/main/java/com/pchmn/materialchips/ChipsInput.java b/library/src/main/java/com/pchmn/materialchips/ChipsInput.java index ba729fd5..ba3dbd56 100644 --- a/library/src/main/java/com/pchmn/materialchips/ChipsInput.java +++ b/library/src/main/java/com/pchmn/materialchips/ChipsInput.java @@ -17,6 +17,7 @@ import com.pchmn.materialchips.adapter.ChipsAdapter; import com.pchmn.materialchips.model.Chip; import com.pchmn.materialchips.model.ChipInterface; +import com.pchmn.materialchips.util.ActivityUtil; import com.pchmn.materialchips.util.MyWindowCallback; import com.pchmn.materialchips.util.ViewUtil; import com.pchmn.materialchips.views.ChipsInputEditText; @@ -141,8 +142,12 @@ private void init(AttributeSet attrs) { // set window callback // will hide DetailedOpenView and hide keyboard on touch outside - android.view.Window.Callback mCallBack = ((Activity) mContext).getWindow().getCallback(); - ((Activity) mContext).getWindow().setCallback(new MyWindowCallback(mCallBack, ((Activity) mContext))); + Activity activity = ActivityUtil.scanForActivity(mContext); + if(activity == null) + throw new ClassCastException("android.view.Context cannot be cast to android.app.Activity"); + + android.view.Window.Callback mCallBack = (activity).getWindow().getCallback(); + activity.getWindow().setCallback(new MyWindowCallback(mCallBack, activity)); } public void addChip(ChipInterface chip) { diff --git a/library/src/main/java/com/pchmn/materialchips/util/ActivityUtil.java b/library/src/main/java/com/pchmn/materialchips/util/ActivityUtil.java new file mode 100644 index 00000000..c119fad0 --- /dev/null +++ b/library/src/main/java/com/pchmn/materialchips/util/ActivityUtil.java @@ -0,0 +1,20 @@ +package com.pchmn.materialchips.util; + + +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; + +public class ActivityUtil { + + public static Activity scanForActivity(Context context) { + if (context == null) + return null; + else if (context instanceof Activity) + return (Activity)context; + else if (context instanceof ContextWrapper) + return scanForActivity(((ContextWrapper)context).getBaseContext()); + + return null; + } +} diff --git a/sample/build.gradle b/sample/build.gradle index 1882ae03..2157d6d0 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.pchmn.sample.materialchipsinput" minSdkVersion 15 targetSdkVersion 25 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.0.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/sample/src/main/java/com/pchmn/sample/materialchipsinput/ContactListActivity.java b/sample/src/main/java/com/pchmn/sample/materialchipsinput/ContactListActivity.java index ebdb1b41..927e2afc 100644 --- a/sample/src/main/java/com/pchmn/sample/materialchipsinput/ContactListActivity.java +++ b/sample/src/main/java/com/pchmn/sample/materialchipsinput/ContactListActivity.java @@ -3,16 +3,15 @@ import android.Manifest; import android.database.Cursor; import android.net.Uri; +import android.os.Bundle; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.pchmn.materialchips.ChipsInput; -import com.pchmn.materialchips.model.Chip; import com.pchmn.materialchips.model.ChipInterface; import com.tbruyelle.rxpermissions2.RxPermissions; diff --git a/sample/src/main/java/com/pchmn/sample/materialchipsinput/MainActivity.java b/sample/src/main/java/com/pchmn/sample/materialchipsinput/MainActivity.java index 664a4cf4..95e88ffd 100644 --- a/sample/src/main/java/com/pchmn/sample/materialchipsinput/MainActivity.java +++ b/sample/src/main/java/com/pchmn/sample/materialchipsinput/MainActivity.java @@ -2,17 +2,21 @@ import android.content.Intent; import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindView; import butterknife.ButterKnife; +import butterknife.OnClick; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.toString(); @BindView(R.id.contacts_button) Button mContactListButton; @BindView(R.id.custom_chips_button) Button mCustomChipsButton; + private int mStackLevel = 0; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,4 +33,23 @@ protected void onCreate(Bundle savedInstanceState) { startActivity(new Intent(MainActivity.this, ChipExamplesActivity.class)); }); } + + @OnClick(R.id.dialog_fragment) + public void showDialog() { + mStackLevel++; + + // DialogFragment.show() will take care of adding the fragment + // in a transaction. We also want to remove any currently showing + // dialog, so make our own transaction and take care of that here. + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); + if (prev != null) { + ft.remove(prev); + } + ft.addToBackStack(null); + + // Create and show the dialog. + MyDialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); + newFragment.show(ft, "ok"); + } } diff --git a/sample/src/main/java/com/pchmn/sample/materialchipsinput/MyDialogFragment.java b/sample/src/main/java/com/pchmn/sample/materialchipsinput/MyDialogFragment.java new file mode 100644 index 00000000..bfe3bca6 --- /dev/null +++ b/sample/src/main/java/com/pchmn/sample/materialchipsinput/MyDialogFragment.java @@ -0,0 +1,28 @@ +package com.pchmn.sample.materialchipsinput; + +import android.os.Bundle; +import android.support.v7.app.AppCompatDialogFragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +public class MyDialogFragment extends AppCompatDialogFragment { + + public static MyDialogFragment newInstance(int num) { + MyDialogFragment f = new MyDialogFragment(); + + // Supply num input as an argument. + Bundle args = new Bundle(); + args.putInt("num", num); + f.setArguments(args); + + return f; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + return inflater.inflate(R.layout.framgnet_dialog, container, false); + } +} \ No newline at end of file diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index d05e5891..2c65be32 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -22,4 +22,11 @@ android:layout_height="wrap_content" android:text="Custom chips examples"/> +