Ted pottel
Ted pottel

Reputation: 6993

Android, trying to get buttons to be on buttom of screen

I'm trying to make a simple app to show pictures. The images get scaled to fit the screen then there is a next and prev button below the image. I would like the next and prev buttons to be at the button, but they keep getting drawn at the button of the image. Thus they move up and down, depending how big the picture is, each time you display a new image.

I found the following solution here, but it is not working for me:

android:gravity="bottom"  
android:layout_alignParentBottom="true"

This is the xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backFeetGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/viewimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/background" />

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/butLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" Left "
            android:textColor="#ff0000ff" />

        <Button
            android:id="@+id/butFavrest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" Favrets "
            android:textColor="#ff0000ff" />

        <Button
            android:id="@+id/butEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" email "
            android:textColor="#ff0000ff" />

        <Button
            android:id="@+id/butRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" Right "
            android:textColor="#ff0000ff" />
    </LinearLayout>

</LinearLayout>

source code

public class cFeetView extends cBaseView implements OnClickListener {
    cFileNames mFileNames;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feet);

        // add listeners
        View mLeft = findViewById(R.id.butRight);
        mLeft.setOnClickListener(this);

        // add listeners
        View mRight = findViewById(R.id.butLeft);
        mRight.setOnClickListener(this);

        mFileNames=new cFileNames();
        mFileNames.Start();
        DrawFeet();
    }

    public void DrawFeet()
    {
        int screenHeight;
        ImageView picImage = (ImageView) findViewById(R.id.viewimage);// R.id.viewpic);
        try {
            String FileName = "canon20.png";
            FileName=mFileNames.Current();
            AssetManager assetManager = getAssets();
            InputStream inputStream;
            inputStream = assetManager.open(FileName);
            Bitmap icon = BitmapFactory.decodeStream(inputStream);

            int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
            screenHeight = getWindowManager().getDefaultDisplay().getHeight();

            int bw = icon.getWidth();
            int bh=icon.getHeight();
            float t = (float) screenWidth / (float) bw;

            int iConHeight=(int)((float)bh*t);
            picImage.setImageBitmap(icon);

            // scale it
            picImage.getLayoutParams().width = screenWidth;
            picImage.getLayoutParams().height =iConHeight;
            Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, screenWidth, iConHeight, false);

        } catch (IOException e) {
        }
    }
    // set the top and buttom margins

    public void onClick(View v)
    {
        Intent i;
        switch(v.getId())
        {
        case R.id.butLeft:
            mFileNames.Pre();   
            DrawFeet();
            break;
        case R.id.butRight:
            mFileNames.Next();
            DrawFeet();
            break;              
        }
    }
} // end class

Upvotes: 0

Views: 108

Answers (3)

Swathi
Swathi

Reputation: 142

Instead of LinearLayout, use RelativeLayout in your xml file.

LinearLayout is used only to arrange them in horizontal or vertical directions.

Upvotes: 2

Lucas Arrefelt
Lucas Arrefelt

Reputation: 3929

In order to use alignParentBottom you need to use RelativeLayout. And you dont need the gravity attribute.

My suggestion for your particular scenario is that you you have a RelativeLayout and put all your buttons inside a LinearLayout that is aligned at bottom and the ImageView above it.

Psedo:

<RelativeLayout>
   <ImageView />

   <LinearLayout
      android:layout_below="@ImageView_id"
      android:layout_alignParentBottom=true>
   </LinearLayout>
</RelativeLayout>

Edited with your xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backFeetGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/viewimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/background" />

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_below="@id/viewimage"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/butLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" Left "
            android:textColor="#ff0000ff" />

        <Button
            android:id="@+id/butFavrest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" Favrets "
            android:textColor="#ff0000ff" />

        <Button
            android:id="@+id/butEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" email "
            android:textColor="#ff0000ff" />

        <Button
            android:id="@+id/butRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12px"
            android:text=" Right "
            android:textColor="#ff0000ff" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 0

VendettaDroid
VendettaDroid

Reputation: 3111

You need to have everything in RelativeLayout. For example, look at this sample code which I made for one button which is aligned to bottom center of screen.

<?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" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

And here is the complete layout file for your layout

<?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" >

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Button" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/button2"
    android:text="Button" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/button3"
    android:text="Button" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="38dp"
    android:src="@android:drawable/gallery_thumb" />

This is how it looks, enter image description here

Upvotes: 0

Related Questions