Skip to content

Commit

Permalink
Fixed bug ClassCastException
Browse files Browse the repository at this point in the history
  • Loading branch information
pchmn committed Apr 16, 2017
1 parent 9d327a0 commit d8c0d74
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 10 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Implementation of Material Design [Chips](https://material.io/guidelines/compone
<img src="https://github.com/pchmn/MaterialChipsInput/blob/master/docs/demo2.gif" alt="Demo" height="600px"/>

## 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)

Expand All @@ -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'
}
```
<br><br>
Expand Down
Binary file added docs/material-chips-input-sample-v1.0.1.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
9 changes: 7 additions & 2 deletions library/src/main/java/com/pchmn/materialchips/ChipsInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@
android:layout_height="wrap_content"
android:text="Custom chips examples"/>

<Button
android:visibility="gone"
android:id="@+id/dialog_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dialog Fragment"/>

</LinearLayout>
17 changes: 17 additions & 0 deletions sample/src/main/res/layout/framgnet_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<com.pchmn.materialchips.ChipsInput
android:id="@+id/chips_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hint="Enter a name"
app:maxRows="3" />

</RelativeLayout>

0 comments on commit d8c0d74

Please sign in to comment.