Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

i want to implement my app layout by java and xml together .


but when i run my project the first part that i wrote in xml will disappear.
why?


xml part

XML
<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=".MylayoutActivity" > 
       
   
   
<LinearLayout
    android:id="@+id/welcom" 
    android:orientation="horizontal" 
    android:layout_width="match_parent"
        android:layout_height="20dp"
         android:removed="@color/br"
         android:gravity="center"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fontFamily="@string/action_settings"
        
        android:text="sm shahrokni : welcome to my firste project of android. "
        android:textColor="@color/white" >
</TextView>
    
</LinearLayout>



 <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:removed="@color/br"
        android:orientation="horizontal"
        android:layout_marginTop="25dp" >
        
<TextView android:layout_width="0dp" android:gravity="center" android:layout_height="50dp" android:layout_weight="25" android:text="Name" android:textColor="@color/white"  />
<TextView android:layout_width="0dp" android:gravity="center"  android:layout_height="50dp" android:layout_weight="25" android:text="Family" android:textColor="@color/white" />
<TextView android:layout_width="0dp" android:gravity="center"  android:layout_height="50dp" android:layout_weight="25" android:text="Age" android:textColor="@color/white" />
<TextView android:layout_width="0dp" android:gravity="center"  android:layout_height="50dp" android:layout_weight="25" android:text="Gender" android:textColor="@color/white"  />

 </LinearLayout>
</RelativeLayout>


part 2 : java


Java
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);




  LinearLayout l=new LinearLayout(this);
  int i=1;
   for( i=0 ;i<=3;i++)
   {
       Button btn = new Button(this);
    btn.setText("hi "+i);
    btn.setId(i);
//   btn.setBackgroundColor(Color.RED);
//  btn.setTextColor(Color.RED);
    l.addView(btn);
     LayoutParams Params=btn.getLayoutParams();
        Params.height=40;
        Params.width=120;
        btn.setLayoutParams(Params);

   }
  setContentView(l);
Posted
Comments
ridoy 24-Sep-13 14:38pm    
Because your java file's layout overlap the xml layout.
seyed mahmud shahrokni 24-Sep-13 14:51pm    
so
What should I do ?
ridoy 24-Sep-13 15:09pm    
clearly you need to choose one,either design through xml or from code behind.But i would prefer you the xml, because it is easy to maintain.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900