Tesla
Tesla

Reputation: 822

Setting custom view into layout in android

I am just learning about android development, and I am having some issues with getting this to work.

I have an activity that uses a relatively layout. I need it to have 2 buttons along the bottom, and then right above the bottoms, I want my custom view to take up the rest of the space.

viewer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<sketchViewer.AnimationPanelView
    android:id="@+id/animationView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/homeFromViewerButton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/homeFromViewerButton"
    android:layout_width="640dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Replay" />

<Button
    android:id="@+id/replayButton"
    android:layout_width="640dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Home" />

</RelativeLayout>

The issue I am having is I that when I run my program, I need to pass a number of parameters into my custom view constructor so that my custom view decides what it should draw. So after creating an instance of my custom view (AnimationPanelView), I am not sure how I set this object into the space I provided for the view.

This is my activity class:

Viewer.java

public class Viewer extends Activity {

AnimationPanelView animationPanelView;

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

    animationPanelView = new AnimationPanelView(this, true /*, more parameters here */);
    animationPanelView.setBackgroundColor(Color.GREEN);
    RelativeLayout v = (RelativeLayout) findViewById(R.id.viewerLayout);
    v.addView(animationPanelView);      
}

Right now, with my v.addView command, the view takes up the entire page, covering up the buttons at the bottom. Can anyone shed some light on this? I feel like I am close, but I've been playing around with it for a while, and I just seem stuck.

Upvotes: 0

Views: 101

Answers (2)

M.J
M.J

Reputation: 586

You are adding another custom view to your layout instead you should use animationPanelView = (AnimationPanelView) findViewById(R.id.animationView); animationPanelView.setBackgroundColor(Color.GREEN);

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93559

Check out the implementing a custom view section here. You need to override onLayout and onMeasure so you can tell your container how big you are.

Upvotes: 1

Related Questions