I need to dynamically add to my main activity (a number) a view that I have defined in an xml file in the layouts folder. I have defined the custom view as a RelativeLayout where I have different types of views.
The problem I have is that after inflating the custom view and gregar the main activity the elements of the custom view are disordered and all superimposed in the center of my mainactivity
this is the xml file of my custom view:
<?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="100dp"
>
<ImageView
android:layout_width="100dp"
android:layout_height="match_parent"
android:id="@+id/imageView"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginStart="100dp"
android:layout_height="100dp">
<TextView
android:text="TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="11dp"
android:layout_marginTop="27dp"
android:id="@+id/textView2"
/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="47dp"
android:id="@+id/button" />
<TextView
android:text="TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/textView2" />
<TextView
android:text="TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_alignBottom="@+id/button"
android:layout_alignStart="@+id/textView2" />
</RelativeLayout>
in this way I add the view to the main activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vistaPadre=(LinearLayout)findViewById(R.id.mainLinear);
LayoutInflater inflar= LayoutInflater.from(this);
int id = R.layout.vistapersonalizada;
vistaPadre = (LinearLayout) findViewById(R.id.mainLinear);
layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
vistaPadre.addView(layoutPlantilla);