Salve ragazzi, è possibile aggiungere un immagine ad un bottone? Se si, che metodo devo invocare?
Salve ragazzi, è possibile aggiungere un immagine ad un bottone? Se si, che metodo devo invocare?
non so se ho capito quello che ti serve, ma android ha l'ImageButton, in cui imposti android:src="..." da xml o setImageResource(...) da codice
Felice possessore di un LG O1 con GingerLOL 1.5.2 e di unMediacom Smartpad 810c con Pistoneone 4.2.1 / 5.0Samsung Galaxy Tab 10.1 P7510 (originale per ora)Mediacom Smartpad 715i (ROM smartpao 2.1), Mediacom Smartpad 842i (ROM smartpao 2.0)
Mini-Forum-FAQ per Mediacom Smartpad 810c
<Button
android:id="@+id/bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button + Image"
android:icon="@drawable/image" />
image è l'immagine che vuoi caricare e che devi mettere nella cartella drawable
Così dovrebbe funzionare
Button bt = (Button)findViewById(R.id.button);
bt.setBackgroundResource(R.drawable.nome_immagine) ;
altro problema..la dimensione dell'imagine è troppo grande, come faccio per adattarla al bottone?
Drawable drw = getResources().getDrawable(R.drawable.nome_img);
Bitmap bmp = ((BitmapDrawable) drw).getBitmap();
Drawable drw2 = new BitmapDrawable(Bitmap.createScaledBitmap(bmp, size_x, size_y, true));
bt.setBackgroundResource(drw2) ;
ultima domanda, poi non disturbo più :P esiste un metodo per calcolare la misura dei bottoni?
Così dovrebbe funzionare
Button bt = (Button)findViewById(R.id.button);
bt.getWidth();
bt.getHeight();
nessun disturbo![]()
appena aggiungo la parte di codice relativa all'assegnazione dell'immagine l'applicazione mi va in crashcodice:Button stop=new Button(this); stop.setText("STOP"); stop.setGravity(Gravity.BOTTOM); stop.setId(-1); stop.setOnClickListener(listener); Drawable drw = getResources().getDrawable(R.drawable.cane); Bitmap bmp = ((BitmapDrawable) drw).getBitmap(); Drawable drw2 = new BitmapDrawable(Bitmap.createScaledBitmap(bmp, stop.getWidth(), stop.getHeight(), true)); stop.setBackgroundDrawable(drw2) ; layout.addView(stop); setContentView(layout);![]()