I want to put button above image view.
How can i do this?
(Please, don't offer to set Background, cause i need ImageView)
Set as background!
Just kidding... what you need is put your views inside a RelativeLayout. Something like will work:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="blah blah"/>
</RelativeLayout>
Notice the use of params like layout_alignParentLeft which are used to position the view where you want.
I agree with #Cristian's answer. but also,
If you need button's action listener you can add onClick method to your imageView without using button.
And also ImageButton. I hope this answer shows some path to solve your problem.
Worked for me with both ImageView and Button inside RelativeLayout:
<Button android:layout_centerInParent="true">
...
Related
I want to do the imageView layout with the button to edit photo inside.
I think to put image as imageView background but i don´t know how to do the semicircle transparency and set the icon.
Any ideas?
UPDATE
Now i have this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="150dp"
android:layout_height="150dp"
android:background="#color/blue_bar">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#color/black"/>
<ImageButton
android:layout_alignStart="#+id/image"
android:layout_alignEnd="#+id/image"
android:src="#drawable/ic_edit_photo"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#B3ffffff"/>
</RelativeLayout>
Can i do the imageButtons background as a semicircular shape?
For that you have to make a CompoundView.
1) I would make a RelativeLayout with the imageview width and height set to match_parent, then a button with alignBottom set to true.
2) Then keep using your button normally in your Activity.
3) You can go fancier and use a CompoundView extending ViewGroup and putting the functionality of the button.
Here is a tutorial for it:
http://www.vogella.com/tutorials/AndroidCustomViews/article.html
I hope it helps.
So I have a list of images that come from the web, I don't know which color are they and I want to place a text over the ImageView.
My idea is to place the ImageView, an image overlay with transparency gradient over that ImageView and the text above it. I want to mimic this behaviour:
Is there anyway to do this via XML?
When you write the XML for your list items which get inflated in the getView(...) of whatever ListAdapter you've written you can surely do this.
Something like this for the list item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="320dp"
android:layout_height="240dp"
android:background="#ACACAC"/>
<RelativeLayout
android:layout_width="320dp"
android:layout_height="240dp"
android:background="#drawable/gradient">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Here is your text"
android:textColor="#000000"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
Then you create that drawable/gradient. For that you can recycle the answer from here.
Thanks to adityajones I managed to get there :)
So although this is my right answer, I'll mark his as the correct one!
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/image" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_image" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="6dp"
android:layout_marginBottom="18dp"
android:shadowColor="#000"
android:shadowRadius="7.0"
android:text="This is some random text"
android:textColor="#color/white"
android:textSize="22sp" />
</RelativeLayout>
I'd use a FrameLayout or RelativeLayout. The first View you add to either should be the background ImageView, then obviously you'll need some TextViews and Other ImageViews [or Buttons, or ImageButtons, etc]
Seems like a reasonable layout: a background image, and then one additional view in each corner.
For the gradient, you'll probably want a separate Layout/View at the bottom with a gradient drawable as the background, although I can imagine you might be able to get away with setting the background of one of your TextViews as the gradient.
You do not have to use a gradient drawable file or set it in your xml..
you can do this pragmatically using GradientDrawable Class as explained in this related Question (Create a radial gradient programmatically) then set it as a background for a layout that covers your ImageView, this gives you ability to use different colors and orientations
I'm trying to place a TextView at the bottom of every activity by using the include... My code is below... I don't know why, but it remains at top... Although it works fine for TextView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:orientation="vertical" />
<include
android:layout_alignParentBottom="true"
android:id="#+id/footer"
layout="#layout/footer" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:text="Hello"
android:textColor="#000000"
android:textSize="15dp" />
¿Maybe you are using a LinearLayout instead of a RelativeLayout? The latter doesn't have android:orientation property. We don't see the parent XML node, so just a guess
Also, you'll need to place the include at the bottom and the TextView above it
<TextView
...
android:layout_above="#id/footer" />
Right now you are overlapping both views
Another suggestion is to use FragmentActivity and a Fragment to handle the Footer, but that's out of the scope of this question.
Use RelativeLayout in your same xml code as the parent layout.
Well i got the solution.... I had to add the include inside another child relative layout...This is what i did..
enter code here
RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<include
android:id="#+id/footer"
android:layout_alignParentBottom="true"
layout="#layout/footer" />
</RelativeLayout>
but...for a textview it works directly without using another child layout...So why do you need to do this for an include.....????:|
I checked your code. It works fine as posted in 1st post.
Please note that when you use layout tag then Eclipse' Graphical Layout won't draw the child xml-layout you've included (in your case footer.xml).
At the same time, you'll get a footer like you want if you run same code on actual device or emulator.
NOTE: you might want to remove android:layout_alignParentBottom="true" from textView1 as currently textView1 and footer layouts overlaps.
I need to add an icon and some text to a button, in code (not xml), in my Android app.
The icon (a stock icon, "expander_open_holo_light.9.png") should be on the left and the text on the right.
I can't find any clue...
Take a look at setCompoundDrawableWithIntrinsicBounds(). You can find the info here:
http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)
Basically, that is the way to set the android:drawableLeft property of your button programatically.
If you aren't doing much with the button (like resizing, instantiating a few of them) i would just create a custom xml view with an imageview and textview side by side contained in a container.
If you are changing the picture and text often, create a custom ImageView or Button class.
E.G
textbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/base"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="100dp"
android:gravity="center"
android:background="#ff623466">
<ImageView android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="#drawable/icon"
android:gravity="center"
/>
<TextView android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Hello!"
android:gravity="center"
/>
</LinearLayout>
Then post this line in main.xml or any other layout you want the button:
<include layout="#layout/textbutton" />
Take a TextView and set ImageView as the background.After setting image, append text to it. Text will be moved to right. Hope it helps.
I am having a problem getting the ListView to display properly. It currently looks like this with the following xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/favs_main">
<Button
android:text="Return to Home"
android:id="#+id/return_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:textSize="15sp"/>
<ListView
android:id="#+id/favsListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_above="#id/return_button"/>
</RelativeLayout>
If you notice the list is down on the screen. I want it to be just below the favorites text instead of just above the return to home button. The catch however is that I always want the button to show and the list view to just occupy the space between the favorites text and the button. The text is from the background image so I can't just align below that. So even with 100 items I would still like to show the button.
Thanks for the help
If the word "favorites" is part of a background image as suggested in the RelativeLayout's background attribute, then you won't be able to align an element below it without using hacky margins or something to that effect. If you want to align an element below the word, separate that into a different ImageView and set the layout_below of the ListView to the id of that ImageView. To get an element to align properly in between two other elements, use a combination of layout_above and layout_below.
Couldn't you just align the ListView to the Parents' Top and set a margin for the ListView so that it is below the Text of the Background?
Also you could change the background to provide the Text in an ImageView and align the ListView to be below the ImageView.
Instead of trying to make a persistent View always show up under the ListView and align it (which you can do, see other suggestions), you might want to take a look at using a footerView:
http://developer.android.com/intl/de/reference/android/widget/ListView.html#addFooterView
"Add a fixed view to appear at the bottom of the list."
Note that it can be another layout too if you eventually need to do more than just one Button.
this my listview which have multiple entries and textview and button fixed in the botton. i haven't inserted background. try this hope it will help.
http://www.techuv.com/layout-with-butoon-and-textview-fixed-in-bottom/
You could use a simple LinearLayout and use the weight attribute on the ListView :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/favs_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/favsListView"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"/>
<Button
android:text="Return to Home"
android:id="#+id/return_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:textSize="15sp"/>
</LinearLayout>