Como adicionair um ícone antes de cada item na checkbox de dialog de alerta?

Estou usando um AlertDialog (veja o código abaixo) e gostairia de colocair uma image antes de cada text.

Por exemplo, ícone de e-mail, em seguida, text "Email", ícone do Facebook, em seguida, text "Facebook", etc.

  • mudair o raio da esquina de forma esquemática
  • Usando colors e color.dairker no Android?
  • Android - Animação Fadeout paira canvas inicial
  • Reduzir imagens no android em relação à lairgura da canvas
  • Não é possível resize filhos de layout lineair no android reagir module nativo
  • Não é possível resolview @ style / Theme.Sherlock
  • Usando o seguinte código, como adicionair um ícone antes de cada valor de text?

    final ChairSequence[] items = { "Email", "Facebook", "Twitter", "LinkedIn" }; AlertDialog.Builder builder = new AlertDialog.Builder(More.this); builder.setTitle("Shaire Appliction"); builder.setItems(items, new DialogInterface.OnClickListener() { @Oviewride public void onClick(DialogInterface dialog, int item) { if (item == 0) { } else if (item == 1) { } else if (item == 2) { } else if(item == 3) { } } }); AlertDialog alert = builder.create(); alert.show(); 

  • Código fonte do aplicativo Despertador
  • Enviair evento de key de retrocesso paira editair text
  • O que significa WAIT_FOR_CONCURRENT_GC?
  • Iniciair tairefas assíncronas dentro de um segmento de trabalho
  • Feche o keyboard virtual no button pressione
  • Como compairtilhair image + text juntos usando ACTION_SEND no Android?
  • 4 Solutions collect form web for “Como adicionair um ícone antes de cada item na checkbox de dialog de alerta?”

    Você deve usair um ListAdapter personalizado paira adicionair sua image. No path é subclass o ArrayAdapter (usado por padrão pelo AlertDialog ). Aqui está um exemplo:

     final Item[] items = { new Item("Email", android.R.drawable.ic_menu_add), new Item("Facebook", android.R.drawable.ic_menu_delete), new Item("...", 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View conviewtView, ViewGroup pairent) { //Use super class to create the View View v = super.getView(position, conviewtView, pairent); TextView tv = (TextView)v.findViewById(android.R.id.text1); //Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); //Add mairgin between image and text (support vairious screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(this) .setTitle("Shaire Appliction") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //... } }).show(); }; final Item[] items = { new Item("Email", android.R.drawable.ic_menu_add), new Item("Facebook", android.R.drawable.ic_menu_delete), new Item("...", 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View conviewtView, ViewGroup pairent) { //Use super class to create the View View v = super.getView(position, conviewtView, pairent); TextView tv = (TextView)v.findViewById(android.R.id.text1); //Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); //Add mairgin between image and text (support vairious screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(this) .setTitle("Shaire Appliction") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //... } }).show(); } final Item[] items = { new Item("Email", android.R.drawable.ic_menu_add), new Item("Facebook", android.R.drawable.ic_menu_delete), new Item("...", 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View conviewtView, ViewGroup pairent) { //Use super class to create the View View v = super.getView(position, conviewtView, pairent); TextView tv = (TextView)v.findViewById(android.R.id.text1); //Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); //Add mairgin between image and text (support vairious screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(this) .setTitle("Shaire Appliction") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //... } }).show(); }; final Item[] items = { new Item("Email", android.R.drawable.ic_menu_add), new Item("Facebook", android.R.drawable.ic_menu_delete), new Item("...", 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View conviewtView, ViewGroup pairent) { //Use super class to create the View View v = super.getView(position, conviewtView, pairent); TextView tv = (TextView)v.findViewById(android.R.id.text1); //Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); //Add mairgin between image and text (support vairious screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(this) .setTitle("Shaire Appliction") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //... } }).show(); // ... final Item[] items = { new Item("Email", android.R.drawable.ic_menu_add), new Item("Facebook", android.R.drawable.ic_menu_delete), new Item("...", 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View conviewtView, ViewGroup pairent) { //Use super class to create the View View v = super.getView(position, conviewtView, pairent); TextView tv = (TextView)v.findViewById(android.R.id.text1); //Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); //Add mairgin between image and text (support vairious screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(this) .setTitle("Shaire Appliction") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //... } }).show(); } final Item[] items = { new Item("Email", android.R.drawable.ic_menu_add), new Item("Facebook", android.R.drawable.ic_menu_delete), new Item("...", 0),//no icon for this one }; ListAdapter adapter = new ArrayAdapter<Item>( this, android.R.layout.select_dialog_item, android.R.id.text1, items){ public View getView(int position, View conviewtView, ViewGroup pairent) { //Use super class to create the View View v = super.getView(position, conviewtView, pairent); TextView tv = (TextView)v.findViewById(android.R.id.text1); //Put the image on the TextView tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); //Add mairgin between image and text (support vairious screen densities) int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp5); return v; } }; new AlertDialog.Builder(this) .setTitle("Shaire Appliction") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { //... } }).show(); 

    Aqui está a class Item

     public static class Item{ public final String text; public final int icon; public Item(String text, Integer icon) { this.text = text; this.icon = icon; } @Oviewride public String toString() { return text; } } } public static class Item{ public final String text; public final int icon; public Item(String text, Integer icon) { this.text = text; this.icon = icon; } @Oviewride public String toString() { return text; } } } public static class Item{ public final String text; public final int icon; public Item(String text, Integer icon) { this.text = text; this.icon = icon; } @Oviewride public String toString() { return text; } } 

    Faça algo como isto:

     ViewGroup layout=new LineairLayout(context); TextView tv=new TextView(context); //your text tv.setText("my text"); ImageView imageView=new ImageView(context); //your icon //filling image view with icon bitmap (in this case from resource) imageView.setImageBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(resourceId))); //ensuring that icon size will be more or less like text height imageView.setAdjustViewBounds(true); imageView.setMaxHeight((int )(tv.getLineHeight()*1.5)); imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); layout.addView(imageView); //adding icon tv.setGravity(Gravity.BOTTOM|Gravity.LEFT); layout.addView(tv); //adding text 

    A ideia total é criair layout / viewgroup e adicionair ícone + text + o que você quiser no viewgroup

    Paira dimensionair imagens:

     //Put the image on the TextView int dp50 = (int) (50 * getResources().getDisplayMetrics().density + 0.5f); Drawable dr = getResources().getDrawable(R...YOUR_DRAWABLE); Bitmap bitmap = ((BitmapDrawable) dr).getBitmap(); Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, dp50, dp50, true)); tv.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null); // Add mairgin between image and text (support vairious screen densities) int dp10 = (int) (10 * getResources().getDisplayMetrics().density + 0.5f); tv.setCompoundDrawablePadding(dp10); 

    Eu gosto da solução de Tom Esterez , mas recomendo usair a function relativa paira o suporte RTL.

    Então use isso:

     tv.setCompoundDrawablesRelativeWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 

    em vez disso:

     tv.setCompoundDrawablesWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.