Alexander Zaldostanov
Alexander Zaldostanov

Reputation: 3011

Android Class extends View and Placement on MainActivity

I am having trouble in Placing a View into main activity, what I did is,

I have a file Lines.java and in this file, we have all the functionality of drawing a circle, this circle class extends view as shown below

public class Lines extends View {
Paint paint = new Paint();

public Views(Context context) {
    super(context);
    paint.setColor(Color.BLACK);
}
@Override
protected void onDraw(Canvas canvas)
{
      canvas.drawLine(0, 0, 20, 20, paint);
      System.out.println("called");
}}

now in MainActivity I am creating a object of this circle class and placing it into setcontentview(obj);

below is the code snippet for that

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Linesobj=new Lines(getApplicationContext());
    setContentView(obj);
}

}

when I do this it takes whole screen place, I don't want that, I want to place it into main activity I tried through xml using this link, I am enclosing a picture of MainActivity, I want to place that view right in the middle of activity Red rectangle shows where I want to place Lines class(i.e View)

Your help is appreciated ! !

Red rectangle shows where I want to place Lines class(View)

XML FILE

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.compoundview.MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

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


    </LinearLayout>

    <com.example.compoundview.Views
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Upvotes: 3

Views: 185

Answers (1)

AdamMc331
AdamMc331

Reputation: 16730

The reason it takes over the entire view is because that is what you are doing when you call setContentView. From the docs:

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

Therefore, whatever you have in your XML (be it a LinearLayout, RelativeLayout, whatever) will be replaced by your custom View. To fix this, you need to add the view programatically to the existing layout. For example, if you have a LinearLayout, you can add your circle like this:

LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.my_linear_layout);
mLinearLayout.addView(obj);

Here are the docs for addView. For a RelativeLayout, it will look a little different. I'd reference this question.

With your edit, I see that you have both a RelativeLayout and a LinearLayout. So, which route you use programatically will depend on where you want your object to be placed in the view hierarchy, which is completely up to you.

Upvotes: 2

Related Questions