You can do both in XML and via code.
The advantage of being via XML is that you don’t write as much in the code.
Imagebutton:
ImageButton imageButton = new ImageButton(this);
imageButton.setBackgroundColor(getResources().getColor(android.R.color.transparent));
File file = new File("caminho para a imagem");
Bitmap bmImg = BitmapFactory.decodeFile(file.getPath());
imageButton.setImageBitmap(bmImg);
//Se quiser algum tamanho especifico
ViewGroup.LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imageButton.setLayoutParams(layoutParams);
//aí você adiciona essa imagem onde quiser, num ViewGroup
layoutPai.addView(imageButton);
Via XLM:
imagebutton.xml file
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:id="@+id/imageButton" />
In the code:
ImageButton imageButton = (ImageButton)LayoutInflater.from(this).inflate(R.layout.imagebutton, null);
where is null
above, you can pass the father, who will be the ViewGroup
File file = new File("caminho para a imagem");
Bitmap bmImg = BitmapFactory.decodeFile(file.getPath());
imageButton.setImageBitmap(bmImg);
//aí você adiciona essa imagem onde quiser, num ViewGroup
layoutPai.addView(imageButton);
thanks I’ll try.
– Ilgner de Oliveira
Cicero my project gave an error like this : Default Activity not found, but I only have one Activity and it has no error.
– Ilgner de Oliveira
@Ilgneroliveira made the reference of Activity no
Manifest.xml
?– Antony Alkmim
yes. nor did I touch <Activity android:name=". Myactivity" android:label="@string/app_name" > <Intent-filter> <action android:name="android.intent.action.MAIN" /> <Category android:name="android.intent.Category.LAUNCHER" /> </Intent-filter> </Activity>
– Ilgner de Oliveira
This is because there is some error in your project. See if next to Run up there in the IDE, the Android doll where your project’s name is with red X error. This error you saw occurs when there is an error in the project or even in the manifest, as @Antony Alkimim mentioned above.
– Cícero Moura