ユーザーからテキストを取得MultiAutoCompleteTextView
してバブルのような形式で表示しようとしていますが、Androidでは幅と高さが0以上でなければなりません
final MultiAutoCompleteTextView tags = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
String[] TAGS = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, TAGS);
tags.setAdapter(adapter);
tags.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
tags.setThreshold(1);
String contactName = tags.getText().toString();
final SpannableStringBuilder sb = new SpannableStringBuilder();
TextView tv = createContactTextView(contactName);
BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
sb.append(contactName + ",");
sb.setSpan(new ImageSpan(bd), sb.length() - (contactName.length() + 1), sb.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Toast.makeText(getApplicationContext(),sb,Toast.LENGTH_SHORT).show();
}
public TextView createContactTextView(String text){
//creating textview dynamically
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextSize(20);
tv.setBackgroundResource(R.drawable.oval);
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_search_api_holo_light, 0);
return tv;
}
public static Object convertViewToDrawable(View view) {
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
view.setDrawingCacheEnabled(true);
Bitmap cacheBmp = view.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
view.destroyDrawingCache();
return new BitmapDrawable(viewBmp);
}
これを実行しようとすると、このエラーが発生し、幅と高さは 0 より大きくなければなりません。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidbegin.train, PID: 11263
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.train/com.androidbegin.loginregister.WorkNow}: java.lang.IllegalArgumentException: width and height must be > 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:836)
at android.graphics.Bitmap.createBitmap(Bitmap.java:815)
at android.graphics.Bitmap.createBitmap(Bitmap.java:782)
at com.androidbegin.loginregister.WorkNow.convertViewToDrawable(WorkNow.java:57)
at com.androidbegin.loginregister.WorkNow.onCreate(WorkNow.java:34)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)