Friday, 19 February 2016

Creating a UI layout in xml and java code

In xml



<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
            android:orientation=”vertical”
            android:layout_width=”fill_parent”
            android:layout_height=”fill_parent”>
<TextView
           android:id=”@+id/myTextView”
           android:layout_width=”fill_parent”
           android:layout_height=”wrap_content”
           android:text=”@string/hello”  />
</LinearLayout>



Same can be done in code, although it is not generally considered as a good practice..

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

           LinearLayout.LayoutParams lp;
           lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                                          LinearLayout.LayoutParams.FILL_PARENT);

           LinearLayout.LayoutParams textViewLP;
           textViewLP = new
                        LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                                       LinearLayout.LayoutParams.WRAP_CONTENT);
 
          LinearLayout ll = new LinearLayout(this);
          ll.setOrientation(LinearLayout.VERTICAL);

          TextView myTextView = new TextView(this);
          myTextView.setText(getString(R.string.hello));

          ll.addView(myTextView, textViewLP);
          this.addContentView(ll, lp); 

}

No comments:

Post a Comment