I am relatively new to Android platform so I don't know if there exists some specific way for doing this. Here goes my problem
I have a layout with a background image. On that background image I have 2 text views (one for header and other for sub header). Also I have four other image view for 4 very small images and 2 button. Now after fixed time interval (say 4 seconds ) I want all the background image, other 4 images and two text to change dynamically. Only the layout of buttons remains constant.
Rest all change after some time. Now this change should keep on happening.
Basically I need to show 4 screens. When user launch app , user see 1st screen then it changes to 2 screen then 3rd then 4th and then back to 1 and so on. I am currently using Animation.Drawable individually for background image and other four images. But i am unable to change text and the transition b/w images is not very smooth.
Is there a way by which i can solve this problem?
Here is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/login_page"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/background_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:contentDescription="TODO"
android:scaleType="fitXY"
/>
<ImageView
android:id="#+id/dot_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/signin"
android:layout_marginLeft="264dp"
android:contentDescription="TODO"
/>
<ImageView
android:id="#+id/dot_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/signin"
android:layout_marginLeft="284dp"
android:contentDescription="TODO"
android:src="#drawable/introdot_inactive" />
<ImageView
android:id="#+id/dot_image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/signin"
android:layout_marginLeft="304dp"
android:contentDescription="TODO"
android:src="#drawable/introdot_inactive" />
<ImageView
android:id="#+id/dot_image4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/signin"
android:layout_marginLeft="324dp"
android:contentDescription="TODO"
android:src="#drawable/introdot_inactive" />
<com.facebook.widget.LoginButton
android:id="#+id/facebook_signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="120dp"
android:layout_marginRight="43dp"
/>
<Button
android:id="#+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/facebook_signin"
android:layout_alignLeft="#+id/SubHeaderText"
android:layout_alignParentBottom="true"
android:layout_alignTop="#+id/facebook_signin"
android:layout_marginBottom="120dp"
android:background="#BBBBBB"
android:fitsSystemWindows="true"
/>
<TextView
android:id="#+id/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/signin"
android:layout_alignParentTop="true"
android:layout_marginTop="42dp"
android:text="Welcome"
android:textColor="#666666"
android:textSize="23pt"
android:typeface="serif" />
<TextView
android:id="#+id/SubHeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="44dp"
android:layout_marginTop="152dp"
android:text="Subheader text"
android:textColor="#666666"
android:textSize="15pt"
android:typeface="serif" />
</RelativeLayout>
You can change it setting the layout background resource in a UI thread or you can use framelayout. For frame layout you need to design an animation which is on the back and other components on the front.
Check this.
Related
I am testing my layout in eclipse's graphical layout. There are 5.4in FWVGA(480x854,mdpi) and 5.1in WVGA(480x800,mdpi) screens available to view in the graphical layout. If I am correct they both fall into large screen category, therefore I've put the layout xml into layout-large folder. And I've put my images into drawable-large-mdpi folder. But the image buttons are slightly off in 5.1in WVGA as you can see in the screenshots below. First is 5.1in, the second screenshot is 5.4'' and the correct one.
My second question which is similar to the above goes like this. I've done new layout for the Nexus 7 and put it into layout-sw600dp folder, and images to the drawable-tvdpi. Then I viewed this layout in the 7in tablet (1024x600,mdpi). And the image buttons are slightly off than it should look. The same way as in the first question.
I understand that the reason is probably because 5.1in and 5.4in isn't exactly the same even though they fall into the same category and I should have 2 different layouts for them. So how I should go about it? How can I have two different layouts? Or is there better solution, as I don't want to create a layout for each screen size that exist on the market.
Here is xml file:
<?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"
android:background="#drawable/wall4_cabinetclosed" android:orientation="vertical">
<RelativeLayout android:id="#+id/frames_layout"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="73dp" android:layout_marginTop="57dp">
<ImageView android:id="#+id/fire_frame"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
android:background="#drawable/small_fire_icon" />
<ImageView android:id="#+id/water_frame"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignTop="#+id/fire_frame" android:layout_marginLeft="75dp"
android:layout_toRightOf="#+id/fire_frame" android:background="#drawable/small_water_icon" />
<ImageView android:id="#+id/earth_frame"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/fire_frame" android:layout_marginTop="71dp"
android:layout_toLeftOf="#+id/water_frame" android:background="#drawable/small_earth_icon" />
<ImageView android:id="#+id/wind_frame"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="#+id/water_frame" android:layout_alignTop="#+id/earth_frame"
android:background ="#drawable/small_wind_icon" />
</RelativeLayout>
<ImageView android:id="#+id/zoomed_image"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="#+id/frames" android:layout_width="210dp"
android:layout_height="220dp" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginLeft="55dp"
android:layout_marginTop="30dp" android:onClick="zoomFrames"
android:background="#android:color/transparent"/>
<Button android:id="#+id/blue" android:layout_width="115dp"
android:layout_height="70dp" android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" android:layout_marginLeft="220dp"
android:onClick="zoomImage"
android:background="#android:color/transparent"/>
<Button android:id="#+id/white" android:layout_width="85dp"
android:layout_height="50dp" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" android:layout_marginRight="420dp"
android:layout_marginTop="420dp" android:onClick="zoomImage"
android:visibility="invisible"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/fireplace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="311dp"
android:layout_marginLeft="67dp"
android:background="#drawable/fire"
/>
<ImageButton
android:id="#+id/water_room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="310dp"
android:layout_marginLeft="90dp"
android:background="#drawable/view_water_room"
android:onClick="finishFireRoom"
android:visibility="gone"
/>
<ImageView android:id="#+id/frames_zoomed_image"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#drawable/frames_zoomed" android:onClick="hideZoomedFrames"
android:visibility="gone"/>
<RelativeLayout android:id="#+id/frameZoomedLayout"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="113dp" android:layout_marginLeft="145dp"
android:visibility="gone"
>
<ImageButton android:id="#+id/fire_icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/fire_icon" android:onClick="changeIcon" />
<ImageButton android:id="#+id/water_icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignBottom="#+id/fire_icon" android:layout_toRightOf="#+id/fire_icon"
android:background="#drawable/water_icon" android:layout_marginLeft="147dp" android:onClick="changeIcon"/>
<ImageButton android:id="#+id/earth_icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/fire_icon" android:layout_marginTop="140dp"
android:layout_toLeftOf="#+id/water_icon" android:background="#drawable/earth_icon" android:onClick="changeIcon"/>
<ImageButton android:id="#+id/wind_icon"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignLeft="#+id/water_icon" android:layout_alignTop="#+id/earth_icon"
android:background="#drawable/wind_icon" android:onClick="changeIcon"/>
</RelativeLayout>
</RelativeLayout>
in 3rd step set all ur device specific like screen, language etc all. and ur done
I have this code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/shape"
android:gravity="top" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#null"
android:onClick="btnCat1"
android:src="#drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="#string/hello_world" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#null"
android:onClick="btnCat1"
android:src="#drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="#string/hello_world" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#null"
android:onClick="btnCat1"
android:src="#drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="#string/hello_world" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#null"
android:onClick="btnCat1"
android:src="#drawable/ic_launcher" >
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerInParent="true"
android:clickable="false"
android:text="#string/hello_world" />
</RelativeLayout>
</LinearLayout>
There are 4 groups of an image and text, but if I open it on small resolution I can see only 3 boxes. How can I set them to show on next line?
edit: it should looks like this example
It looks like you may be trying to replicate the behavior of a GridView. You could accomplish switching from 2 to 4 columns based on the size of the view using a GridView.
Another alternative that may work for you, depending on your targeted versions, is to specify a different layout file to be used when less screen space is available (check table 2 "smallestWidth" row): Providing Alternative Resources. You would use this approach by specifying a folder such as "res/layout-sw600dp/" and putting an identically named layout file in that folder. In each of the layout files then explicitly layout your files with the number of columns desired.
If you are targeting devices that don't recognize the "sw" resource modifier, you could simulate this manually in your code by specifying a layout file to inflate by name, and simply pick a larger or smaller one based on the screen size (which is of course available to you when the app is running).
Let me know if you would like elaboration on a specific approach.
You've set a constant width (100dp) for each element. It won't fit on the screen which has width lower then 400dp. One way to fix this is to use a horizontal scroll view.
Replace top RelativeLayout with the following.
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
If you wish to have different layout on different screens, like on your attached drawing, simply use two different layout xmls as described here.
Is there a way to make text, buttons, progress bar stay in around the same area with any resolution. So if you on a nexus 10 and nexus 7 the text, buttons, progress bar will say around the same place. As my app just looks silly on nexus 10. Heres my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:textSize="20dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:textStyle="bold"
android:textColor="#FFFFFF"/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp" />
<ImageButton
android:id="#+id/pauseicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pauseicon"
android:layout_gravity="center"
android:layout_marginTop="20dp"
style="?android:attr/borderlessButtonStyle"/>
I would not recommend that you write your layout like you have. To make your app look similar on different devices, you should align all the views so that they are relational to each other.
There is a lot of information here
I would also advise that you use a relative layout rather than a linear layout, because it is easier to make the views relational to each other, which (usually) makes your app look better at different screen sizes.
Here is an example of a relative layout(from the dev docs)
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reminder" />
<Spinner
android:id="#+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times" />
<Spinner
android:id="#id/times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/times"
android:layout_alignParentRight="true"
android:text="#string/done" />
I used relativelayout and here is what I've got. I can't get the image to fill the section I want. I don't know how to fix it. I've heard something about using layout weight, is that some thing I should use?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background">
<ImageView
android:id="#+id/imagehome"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_margin="10dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="#+id/tvtitle1"
android:src="#drawable/one" />
<TextView
android:id="#+id/tvtitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Voted Best Dentist five years running by"
android:textSize="16dp"
android:textColor="#9D4F1B"
android:layout_above="#+id/tvtitle2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"/>
<TextView
android:id="#+id/tvtitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="The Oakland Tribune"
android:textSize="16dp"
android:textColor="#9D4F1B"
android:layout_above="#+id/tvhome"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"/>
<TextView
android:id="#+id/tvhome"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text=" You'll be cared for by our highly trained staff of dental professionals,energized to work as a team while providing the highest quality of care for your dental health. We offer complete dentistry for the entire family at a single location. "
android:textColor="#4C2016"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_above="#+id/special"
android:layout_marginBottom="25dp" />
<Button
android:id="#+id/special"
android:layout_width="120dp"
android:layout_height="40dp"
android:background="#drawable/cc"
android:text="Specials"
android:textColor="#F6E6C6"
android:textSize="17dp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_above="#+id/tvbutton"
android:layout_marginBottom="15dp" />
<TextView
android:id="#+id/tvbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App provided by Bay Area Dental Marketing"
android:layout_centerHorizontal="true"
android:textSize="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
on the xml file,
and on the device
It got better but it'snot there yet!
Not sure how much flexibility you have but the best solution would be to remove the text and image from the background and add them as views instead. As you have experienced unless each piece is added as a view it is nearly impossible to guarantee it will look correct on all devices.
When working with multiple screen sizes, I have found it best practice to either (a) use RelativeLayout, or (b) set a LinearLayout inside a ScrollView.
Use RelativeLayout so you won't have to specify the pixels.
Alternativley if you are doing that for one particular phone select corresponding screen size in the XML designer in Eclipse. Yet, using RelativeLayout guarantees it will work on all the phones
Even using the suggested answers above you may still run into problem on different size screens. The best solution I can think of is to create separate image resources for the background and foreground then add an image view with the foreground image as the source.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background" >
<!-- This method makes this layout redundant, dont need this anymore
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="23dip"
android:layout_marginRight="15dip"
android:layout_marginTop="255dip"
android:orientation="vertical" >-->
<!-- put the foreground image in an imageview -->
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/foregroundImage"/>
<TextView
android:layout_width="276dp"
android:layout_height="132dp"
android:textSize="15dp"
android:gravity="center_vertical"
android:text="some text"
android:textColor="#4C2016"/>
<Button
android:id="#+id/special"
android:layout_width="131dp"
android:layout_height="40dp"
android:background="#drawable/cc"
android:layout_marginTop="20dip"
android:layout_marginLeft="75dip"
android:textSize="17dp"
android:textStyle="bold"
android:text="Specials"
android:textColor="#F6E6C6" />
</LinearLayout>
Searched all day and came up with a poor solution i think.
i want three ImageButton placed on the right sida of the screen.
Not centered but just above center position..
With the code below i get the result i want but,
If the user have a bigger screen they will not have this position right?
I have tried the android:gravityall day and all i can do with it is
to center the three buttons very nicely.
What should i use to make the three buttons always stay at the positions that
they are on the image belove.
i have the button image in 3 different sizes in hdpi,mdpi,xhdpi folder.
<RelativeLayout
android:id="#+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:id="#+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="A"
android:src="#drawable/drawer_1"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
/>
<ImageButton
android:id="#+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="B"
android:src="#drawable/drawer_1"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
android:layout_below="#+id/btn_A"
/>
<ImageButton
android:id="#+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="C"
android:src="#drawable/drawer_1"
android:background="#android:color/transparent"
android:layout_alignParentRight="true"
android:layout_below="#+id/btn_B"
/>
</RelativeLayout>
Picture of the three buttons placed on the right side, and my daughter of course.
One option would be to put all three buttons within a LinearLayout (for simplicity's sake) and then set the layout of this container programmatically.
You can see the size of the screen using getResources().getDisplayMetrics().heightPizels and then set the top margin accordingly.
You could add a LinearLayout inside the RelativeLayout, and then use the following properties on the LinearLayout:
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
EDIT: Ok, I've done some testing and found a way of doing what you want without the use of styling it programatically, here's the xml:
<RelativeLayout
android:id="#+id/rightRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="80dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true">
<ImageButton
android:id="#+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:src="#drawable/icon"
android:background="#android:color/transparent"
/>
<ImageButton
android:id="#+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:src="#drawable/icon"
android:background="#android:color/transparent"
/>
<ImageButton
android:id="#+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:src="#drawable/icon"
android:background="#android:color/transparent"
/>
</LinearLayout>
</RelativeLayout>