Creating UI using XML and Java

Below are the two ways to create UI (User Interface)
------------------------------------------------------------------
1) Using XML (use below 2 files)

i) XML code (/res/layout/main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="UI Created by using XML layout (res/layout/main.xml)" 
        android:textColor="#FF11AA33"
     />
</LinearLayout>

ii) Java (/src/MyXmlLayoutActivity.java)
import android.app.Activity;
import android.os.Bundle;

public class MyXmlLayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Output


2) Using Java (use below 1 file)
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyJavaLayoutActivity extends Activity {

LinearLayout linear;
TextView text;

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

linear = new LinearLayout(this);
linear.setOrientation(LinearLayout.VERTICAL);
text = new TextView(this);
text.setText("UI Created by Java Code without using /layout/main.xml");
text.setTextColor(Color.parseColor("#FF11AA33")); //#AARRGGBB format 
linear.addView(text);
setContentView(linear);
}
}

Output

No comments :

Post a Comment