I'm very new to Android.
I created a ImageButton. The main.xml includes code segment as following.
<ImageButton
android:id="#+id/button1"
android:layout_width="50px"
android:layout_height="50px"
android:src="#drawable/cat"
android:layout_x="50px"
android:layout_y="52px"
>
Where do i have to put the "cat.png" i have. Do i need to rename it to something else?
Put it in the res/drawable/ folder in your Android project. Keep the name cat.png
you can simply put in any one folder it will work no need to change xml for that.
Related
I have dragged an ImageView to layout and I selected the picture. But when I execute the application, It doesn't appear. What can I do to solve this ?
ImageView XML code;
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
app:srcCompat="#mipmap/idlepoor_moneys"
android:id="#+id/imageView4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
Add this property in Your image View
android:src="#mipmap/idlepoor_moneys"
Remove the following line from your code
app:srcCompat="#mipmap/idlepoor_moneys"
As allready said change
app:srcCompat="#mipmap/idlepoor_moneys"
to
android:src="#mipmap/idlepoor_moneys"
Also I recommend to put your images in the #drawable folder. The mipmap is where app icons are stored when making an app.
app:src="#drawable/idlepoor_moneys"
You can easily find the drawable from your project map. If you can't find it you can always right click the folder to get the path to it.
I want to add specific XML attribute for the view in android layout file.
I am getting lint message to add contentDescription to ImageView for more 400 ImageView and I don't want to add it manually by going to each ImageView XML tag. so is there any way to automatically add missing attribute with its value in android studio.
Right click the layout folder, click Replace in Path. Then in string to match put in
<ImageView
then in string to replace field, put
<ImageView android:contentDescription="yourText"
Then, press Find. Finally press All Files.
Moxdroid:
And finally Reformat and Rearrange the code.
Me:
You don't need to reformat. Your ImageView output will look like this
<ImageView android:contentDescription="yourText"
android:id="#+id/img_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:background="#drawable/back_selector" />
Unless OCD kicks in, this is totally fine i guess.
I would like to make a button, a simple button, having the default Search icon that is used in Android.
However, I do not want to make my own xml file and put the images in the Drawable folder, because i know they already exist in the Android sdk.
So why not making use of them?
I tried to make something like this:
android:background="#android:drawable/...." but there in this directory it seems that all the files are png file not xml file able to interact with the user (on button pressed, etc..)
I hope an expert can help solving this problem.
You don't need an xml file for the button to work. The png files in the drawables are just for the image. You can create a button programmatically or in the xml but you still have to create it somewhere because the Button instance is what is used for the onClick() and not necessarily the xml. Either way you must have an xml file for your Layout to use in setContentView() so you can put a Button in that layout file or create it in your Java code but either way, you have to create a Button
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
in this Button you can set the background or use an ImageButton instead and set `android:src="#drawable/..."
Then in your code you still have to get the button instance
Button btn1 = (Button) findViewById(R.id.button)
after you have called your layout like
setContentView(R.layout.your_layout_file);
If you use an ImageButton just replace Button with ImageButton which is what it sounds like you want. Hope this helps it make a little more sense for you
Suppose I want to draw simple icon on the button surface. Let it be the cross or some letter.
What is the best way to do this? Can I use wingdings or webdings font on button?
As Android uses Unicode (utf-8) you can use any sign defined there, as long your font supports that. Just use copy and paste. The better solutions i think is using ImageButton using images from your drawables folder. This folder can be found in project/res/drawables or via Ecplipse Package-Explorer and you should use this to copy and paste your files in place (png, gif and jpg are supported)
In XML layout file a image is assigned the following way to an ImageButton
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
See also:
http://developer.android.com/reference/android/widget/ImageButton.html
http://developer.android.com/guide/topics/resources/providing-resources.html
http://developer.android.com/training/multiscreen/screendensities.html
I'm searching a way to put my own image as an ImageButton. There is the src thing but it accepts only icons i thing. Any help?
This is pretty fundamental stuff which would be covered if you ran through tutorials.
There's more to it, but the general common procedure would be to include any images you want to use in your res/drawable folder for the project. Then, you'd usually reference the image in XML something like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myimage" />