user198725878
user198725878

Reputation: 6386

Positioning the View

I have sub classed a View to create a custom control. Below is my code in which I am trying to set the x, y, width and height.

But I am not able to set the x and y to position the view on the screen.

    public class MyAdvertisement extends View{

        public MyAdvertisement(){
            super(null);
            System.out.println("This first");
            this.setBackgroundColor(android.graphics.Color.RED);
            this.setMinimumWidth(500);
            this.setMinimumHeight(100);
        }

        public MyAdvertisement(Context context){
            super(context);
            this.setBackgroundColor(android.graphics.Color.YELLOW);
            this.setMinimumWidth(500);
            this.setMinimumHeight(100);
        }

        public MyAdvertisement(Context context, AttributeSet attrs){
            super(context, attrs);
            this.setBackgroundColor(android.graphics.Color.YELLOW);
            this.setMinimumWidth(500);
            this.setMinimumHeight(50);
            LayoutParams layoutParams=new LayoutParams(500,50);
            layoutParams.setMargins(0, 400,0,0);
            this.setLayoutParams(new LinearLayout.LayoutParams(layoutParams));

        }

        public MyAdvertisement(Context context, AttributeSet attrs, int defStyle){
            super(context, attrs, defStyle);
            this.setBackgroundColor(android.graphics.Color.GREEN);
            this.setMinimumWidth(500);
            this.setMinimumHeight(100);

        }

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <view
        class="com.rciicustomclass.MyAdvertisement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myb"
        android:tag="tag"
         />
</LinearLayout>

I want to position the view at the bottom of the screen.

Upvotes: 0

Views: 71

Answers (2)

sdabet
sdabet

Reputation: 18670

You should use a RelativeLayout with android:layout_alignParentBottom="true" on your custom view. And you can directly put your class name as the XML element's name:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <com.rciicustomclass.MyAdvertisement
        android:id="@+id/myb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:tag="tag" />

</RelativeLayout>

Upvotes: 2

Thommy
Thommy

Reputation: 5407

I reccomend using a RelativeLayout:

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

    <"com.rciicustomclass.MyAdvertisement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

Plus there are plenty of other tags to position your View like centering, alignParentLeft, centerVertical, centerHorizontal, etc.
Also it is possible to place them relativeley to each other.

Upvotes: 2

Related Questions