I made an app that displays an AlertDialog once I click on an ImageButton in eclipse.
But when I was trying to input a picture onto the ImageButton, I only found a way to do it in the XML graphic layout of eclipse via right clicking on the Button and then on "edit src". Is there a way to put in the picture from written XML itself?
Yes. You can put a picture into the written xml of an Android layout file.
With your xml file open in eclipse, there are two tabs at the bottom of the layout:
Graphical Layout
.xml
Press the ".xml" tab and you can edit the raw xml.
In order to add an image to your ImageButton, do this:
android:background="#drawable/your_image"
There are also other tags that work too, such as android:src, and the differences are described here: What is the difference between src and background of ImageView.
There are also ways to add the image or change the image programmatically in your .java file. This is useful only if you want to change the image dynamically (although, see below for changes of state)
And, this is just the beginning of what you can do...
If you don't want the default frame around the button, you can use other items, such as an ImageView to make your button.
If you want to display various graphical images depending on state such as "pressed", "selected", etc, then you can specify a "selector" file instead of "your_image". There are many examples of how to make "selector" files on this site, and how to implement the other upgrades to your button.
Have fun!
Related
I want to change some properties for imageview , problem is that imageview ID name is used for many imageview in project , I want to change only for the one which i am looking through layout inspector .
I saw in this video that we can navigate to the file through declared attributes , but the problem is i am not able to find Declared Attributes section in Layout Inspector .
P.S My Device is API 29 Level .
find the activity name.xml than click spilt (see attached image) than find the image-view. now code & relative view is in-front of you and you can add whatever you want.
Secondly, you made a very big mistake it'll disturb you in up-coming requirements.
Remember Never use same id for more than one view item
How to create a button as shown in the picture:
and also Image name Button should permanently change color on click. Moreover I will be creating this button dynamically from the android file. So it will be good if somebody suggest me in that way instead of xml files way.
Have a look at the ImageButton class. You can easily create this at runtime so long as you have the picture file for your button in your resources. For it to permanently change colour simply create the different coloured version of the file, put that in your resources folder too and run some code on the onClick of the button to change the image of the ImageButton to the different coloured image. Should take no more than a few lines of code.
See: https://developer.android.com/reference/android/widget/ImageButton.html
Is it possible to have the Android Graphical Layout next to the Android XML layout so I can modify both simultaneously? It would be much better to change the XML and instantly see the effects, as opposed to clicking the tab to change it all the time.
Yes, although I just tried it and it has a bit of lag after you change something in the Graphical Layout editor.
Open your layout file, such as "activity_foo.xml".
Choose the menu item "Window > New Editor". This will give you another editor for the same layout. In this example, you'll see two tabs that both have "activity_foo.xml".
Mouse press on the second tab and then drag it to the right and downwards, until you see the outlines of two rectangles representing editor views side by side, then let go of the mouse button.
You now have the editors side by side. Place one into Graphical Layout mode and the other into XML mode by choosing the respective tabs.
Note that it seems like the Graphical Layout will consistently update the XML. However, changes in the XML don't seem to affect the Graphical Layout until you save and then click in the Graphical Editor again.
I managed to make the code instantly update the graphical layout and to have both of them shown at the same time.
First, I followed the steps given by louielouie on his answer to have both graphical layout and XML code shown at the same time but this wasn't enough.
Then, I clicked on the code of the XML file and after that I clicked on the graphical layout. At this point, I was able to make changes on the XML file and they were instantly shown in the graphical layout.
There is a little bit better possibility to do that then with new editor like louielouie wroted..
1) Open your layout from project explorer and show it in graphical layout.
2) Get project explorer, right click on your layout
3) Select OPEN WITH -> XML EDITOR
4) At bottom you can switch between Source and Design layout.
5) Catch the 'header' and put it on the side of your graphical layout.
In short word just right click your layout -> open with -> xml editor.
Hey guys I am having trouble with placing my buttons in my XML layout. I already have a background and was just trying to put 3 buttons near the middle and thats where I'm having trouble. I know about center gravity and all that but could you help me? Also, If I made my own buttons can I just import them using the image button thing, or is that not right? Thanks for the help!
PS: Also, there is a big white box around my button. How do I get that to go away?
I posted this answer to your duplicate question, so I'll also post it here. :)
You can rearrange elements of your layout by editing the XML directly or by dragging and dropping in the outline view or the graphical layout view (I find the latter harder to do reliably).
To use your custom views, click on the "Custom & Library Views" button in the graphical layout palette, or just type in the fully qualified class name as the view tag in the XML.
If you use Image button you can set your custom Image to src property or background property. If youi chose src you should add android:background="#null".
But I would recommend to use background property while if you use 9-patch png (who knows maybe tomorow you will use 9-patch) they would not stretch if you set it in src.
I want to change the style of a button in an android app. here is a picture that has both kinds of buttons, the one that I have now, and the one that I want to switch to:
the kinds of buttons that I am using now, look like any of the letters on that keyboard. the ones that I want to use like like the arrows. I would I go about changing this. I prefer to do it in xml but I can do it in java if that is the only way. Thanks
change the background of the Button to a png image with arrows!
in XML use android:background="#drawable/yourimage"
To get more sophisticated control check here,
Standard Android Button with a different color