Ak47
Ak47

Reputation: 55

Android: I am trying to add new Views into RelativeLayout

I want to add a new ImageView or another view programatically into a realative layout, Do i need to use setcontentView() after adding new views to the layout, can i have an empty realtivelayout when i am calling setcontentView() the first time and add views to it programatically, Thanks in advance, please give me the full code.

MainActivity.Java;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView im = (ImageView)findViewById(R.id.imageView1);
    im.setImageResource(R.drawable.a5);

    ImageView im2 = new ImageView(this);
    im2.setId(1);
    im2.setMinimumWidth(50);
    im2.setMaxWidth(50);
    im2.setMinimumHeight(50);
    im2.setMaxHeight(50);
    im2.setImageResource(R.drawable.a4);
    Toast.makeText(this, "Success1", Toast.LENGTH_LONG).show();

    RelativeLayout rt= (RelativeLayout)findViewById(R.layout.activity_main);
    rt.addView(im2);

    /*  RelativeLayout mainLayout = (RelativeLayout)findViewById(R.layout.activity_main);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button b1 = new Button(this);
    b1.setText("Clcik me");
    b1.setLayoutParams(params);
    b1.setId(1);
    mainLayout.addView(b1); */

    /*ImageView displayImage =new  ImageView(this);
    displayImage.setImageResource(R.drawable.a1);

    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.layout.activity_main);
    RelativeLayout.LayoutParams lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    displayImage.setLayoutParams(lparams);
    displayImage.setId(1);
    mainLayout.addView(displayImage);

    setContentView(mainLayout);*/

}

I have tried things that are in the comments but none worked out my layout:

<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=".MainActivity"
>
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
    />
</RelativeLayout> 

Upvotes: 0

Views: 135

Answers (2)

Kushal Sharma
Kushal Sharma

Reputation: 5973

I think what you want to do is inflate a dynamically created image view inside a relative layout. Bellow is a sample code to do that.. i hope it helps..

In activity_main :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    <!-- give this layout an id -->
    android:id="@+id/rt"
    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=".MainActivity"

</RelativeLayout> 

In MainActivity :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set layout on activity
    setContentView(R.layout.activity_main);

    // Find and initialise the relative layout we made 
    RelativeLayout rt = (RelativeLayout)findViewById(R.id.rt);

    // Make layout params for image view we want to add
    LinearLayout.LayoutParams layoutParamsImageView = 
                         new LinearLayout.LayoutParams(50, 50);

    // Create an image view
    ImageView im2 = new ImageView(this);

    // Set layout params on image view
    iv2.setLayoutParams(layoutParamsImageView);

    // Optional attributes 
    iv2.setScaleType(ImageView.ScaleType.CENTER_CROP);
    iv2.setAdjustViewBounds(true);

    // Set image to the view 
    im2.setImageResource(R.drawable.a4);

    // Add view to relative layout we made    
    rt.addView(im2);

}

Upvotes: 1

Narendra Kothamire
Narendra Kothamire

Reputation: 973

Pass your relativelayout in setContentView();

Upvotes: 0

Related Questions