for an application I need to place some objects at the exact position that I want to have them and therefore I need to use AbsoluteLayout this time.
I want to add buttons dynamically exactly like in the following XML - but during runtime.
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/backbutton"
android:text="TEST123"
android:layout_x="120px"
android:layout_y="120px"
android:layout_width="100px"
android:layout_height="100px" />
</AbsoluteLayout>
How can I archive this? I tried it with the following code to add a button, but I haven't found a function so far to set layout_x and layout_y. How can I do this? Thanks.
AbsoluteLayout al = new AbsoluteLayout(this);
Button t = new Button(this);
t.setHeight(300);
t.setWidth(300);
t.setText("TEST123");
// x y ???
setContentView(al);
When you add the view to the layout, you can provide LayoutParams, which is where you specify layout information for the view.
However, AbsoluteLayouts are deprecated, with good reason. How are you going to handle different screen sizes and resolutions? You should be able to create the layout you are looking for with a combination of the non-deprecated layout objects. See developer docs as a starting point.
If you describe your goals in more detail, someone should be able to help you select the proper layout.
You should try with Canvas methods.
onDraw(Canvas canvas)
Using canvas - will not create actual buttons, but you can work with images.
Also have a look to these link : Android drawing button to canvas with custom view?
Related
On an iOS App I saw such a Button:
The same I would like to do in Android, how could I achieve this?
What I tried is the following code:
<Button
android:id="#+id/widget41"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Email1 testmail#gmail.com"
android:textStyle="bold" />
Looks something like that:
Well just a normal (ugly looking) Button. I have no idea, how I could style it like in iOS. Any suggestions or links would be appreciated!
The best solution would be to create your own custom view that behaves like the iOS counterpart (though, as other users have mentioned, Android does have it's own design guidelines, and the view that you are seeing is an iOS implementation that is designed for that platform).
If you look at the iOS image above (a copy of yours with some parts highlighted), I have split it up into sections.
You could use an Android ViewGroup like a LinearLayout to create the overall image, and give the LinearLayout a border or background (which can be a bitmap image of a rounded rectangle for example (See Android Nine Patch for an example of how to make this fit multiple screens).
Firstly, for the mail icon you would need a LeftAligned ImageView
with appropriate dimensions.
Next up we have a Bold TextView containing the text "Email1".
This is followed by another TextView which is blue and uses the
elipsize property (as defined in an Android XML layout) to create
"..." at the end once the text has reached the max width it can
consume. (Use android:ellipsize="end" in the XML)
Finally we have an indicator image, which again can be an ImageView
sized appropriately.
You could also achieve this with a RelativeLayout, which would allow you to RightAlign the indicator image, LeftAlign the mail icon, and allow the text to fill the space in between that it can get hold of.
Example of Nine Patch use for the background here
That is UITableView in iOS(just like ListView in android). It depends on the list item design you do it. There is no such Button Control in Android.
You should design your own button to looks like iOS one.
Android has it own design guidelines:
http://developer.android.com/design/patterns/pure-android.html
Well, as others have clearly mentioned there is no default Button in Android like this, and for your info neither is in iOS. Its all about the design. Anything is possible, in the end it all comes to how far are you willing to go to achieve it.
Below is a simple code, that will be close to your design.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#drawable/text_background"
android:drawableLeft="#drawable/envelope"
android:drawablePadding="10dp"
android:drawableRight="#drawable/right_arrow"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="#string/email" />
There can be other ways also, like that whole view could be a ViewGroup, either a LinearLayout or a RelativeLayout and there could be multiple TextViews and ImageViews inside that.
Here is a tutorial for creating stylized android buttons. You can round the corners and change the background colors to look like the buttons in ios.
Here is a similar question.
Hope this helps.
Hi I'm having a daft problem with my android application.
Currently it looks like this:
Is there a way of making the button go to the bottom in the middle? I've tried messing around but same effect. Also tried changing the fill_parent/wrap_content, relative/linear layouts and gravities.
This is the screenshot of the .xml file
many thanks.
There are a couple things you can do to get this, with the relative layout you're using this would work. Add these lines to your button section
android:layout_below="#+id/android:empty"
android:layout_alignParentBottom="true"
android:layout_alignParentCenter="true"
Add these two attributes to your Button
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
and these one to your textview:
android:layout_above="#id/insertion"
android:layout_height="fill_parent"
Read the API reference here:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
May be you want to use a linear layout instead of the relative one...
With the LinearLayout you can place your item without thinking on their relative position.
You can place a vertical linear layout and inside it another layout for the list of reminders.
<LinearLayout android:orientation="vertical">
<ListView android:width="fill_parent"android:weight="2" />
<Button android:width="wrap_content" android:weight="1" />
</LinearLayout>
With weight option, you can choose to make the Button to be painted before the ListView.
EDIT: Reading other answers, I'm considering if you really need a RelativeLayout to place a button under a listview. I think you should learn how to handle simple view before to start using something more complex. If LinearLayout solve your problem, why don't use it? This is a personal observation...
First of all, can I just say, I find laying out android UI's to be a frustrating experience? I used to think the XML layouts were simple and clean and awesome but every time I try to make anything with it I spend hours trying to do the simplest things!
In this particular instance I'm trying to make a simple horizontal bar that contains an image button of fixed size on the right and to the left of it I want an ImageView that takes up the rest of the available width. I see similar constructs all the time in the UI: the search box that appears at the top of the screen when searching, the text area and send button for composing text/googletalk messages, etc.
I've tried both a horizontal linear layout and a relative layout, and I can't get the button to look right in either one. My latest attempt has the following layout code:
It looks like this:
Using the hiearchyviewer indicates that both the imageview and the button have the same height (45px). And it shows the view dimensions and positions to be exactly what I'm looking for. Same height (differing widths of course since the ImageView is much wider). And they butt right up next to each other, centered in the Relative Layout. However the button as drawn on screen is obviously not taking up the full ImageButton view. I'm thinking it's something weird about the android system 9patch drawable used for the ImageButton background. But what do I know? I can't get it to look right no matter what I try.
How did you set up your RelativeLayout? Try to set it up like this:
<?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="wrap_content">
<ImageButton android:layout_width="wrap_content" android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgButton"></ImageButton>
<ImageView android:id="#+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:src="#drawable/red_button" android:scaleType="fitXY" android:layout_alignBottom="#+id/imgButton" android:layout_alignTop="#+id/imgButton" android:layout_toLeftOf="#+id/imgButton"></ImageView>
</RelativeLayout>
Hope this helps.
If dimensions are exactly how you are looking for , then in ImageButton and ImageView , use android:scaleType="fitXY" and check.
For simple case , I might use linearlayout with horizontal orientation with two buttons in it with proper weights.
I'm having a problem calling a picture to view. In my app after they click the button i want to take them to a new page where it will display a picture and under it some text. For this example say i want a pic if dog to be at the top of the page then under it say i want something like the breed and the cost of breed. Can anyone help me?
Use this XML for your page what will display the image:
<ImageView
android:id="#+id/image"
android:src="src"
/>
<TextView
android:text="image text"
android:layout_below ="#id/image"
/>
Of course, add more options to the views
Some good references:
http://developer.android.com/reference/android/widget/ImageView.html
http://developer.android.com/reference/android/widget/TextView.html
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
Use ImageView.
Also for other questions like this, I highly recommend reading the User Interface section of the online Android Developer's Guide.
In a layout XML, put a LinearLayout, oriented vertically, containing first an ImageView widget then a TextView widget. You can set the text using TextView#setText(String) and the image using ImageView#setDrawable(...).
This layout will be used by an activity that reads the Extra information (for example the text to show and the image URL or the image path on the SDCard) when it starts.
You should read more about Views on the Android developer's guide as advised by Shawn Lauzon.
write the above code in your .xml
<ImageView android:id="#+id/img"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center">
</ImageView>
and add this line in your .java class
img = (ImageView) findViewById(R.id.img);
I have a widget layout xml which sets the src to the delivered android widget 4x1 frame image.Here is the widget layout code.
<?xml version="1.0" encoding="utf-8" ?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tuwidget" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/tuwidget_img_btn"
android:src="#drawable/widgetinitial"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</AbsoluteLayout>
#drawable/widgetinitial holds the widgetinitial.png image example 4x1 at developer.android.com (AppWidget design guidelines).(4x1_Widget_Frame_Portrait.psd) What I am trying to do is display an image inside the delivered frame instead what happens is the frame image goes away and only the image I am trying to display shows up. How can I display the image inside the bounding box or the background?
Any help is much appreciated.
Another question - I think I saw in a couple of forums AbsoluteLayout is a deprecated feature for Android 2.1 and above. Is that correct? and does using AbsoluteLayout throws any force close or other exceptions?
AbsoluteLayout is deprecated. It doesn't throw any exceptions, but its generally a bad idea to use it because it is really hard to design a layout that will work on all screen sizes. There is generally a better way to do it using a different layout.
In your case, I don't follow exactly, but it sounds like you want to layer two widgets on top of each other? An image with a frame? To do so, I'd use a FrameLayout. This is designed for having multiple layers of images.
Common layout objects is also a good guide to the basic layout types.
Take a look at the end of the page 2 of this pdf