Android App Without Layout

It’s very easy. The most important line of code is:

1
setContentView(myLInearLayout);

You can download this code: Download code

First step, remove layout.xml. Then in your main class you can programmatically add a LinearLayout, RelativeLayout… a example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.thedeveloperworldisyours.withoutlayout;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
public class TheDeveloperWorldIsYours extends Activity {
 
    private TextView valueTV;
    private Button valueB;
    private TextView valueRelativeLayoutTV;
    private Button valueRelativeLayoutB;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        //create LInearLayout
        LinearLayout myLInearLayout = new LinearLayout(getApplicationContext());
 
        //add LayoutParams
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        myLInearLayout.setOrientation(LinearLayout.VERTICAL);
 
        //set Content View
        setContentView(myLInearLayout);
 
        //add textView
        valueTV = new TextView(this);
        valueTV.setText("LinearLayout");
        valueTV.setId(5);
        valueTV.setLayoutParams(params);
 
        // add Button
        valueB = new Button(this);
        valueB.setText("RelativeLayout");
        valueB.setId(5);
 
        //add the textView and the Button to LinearLayout
        myLInearLayout.addView(valueTV);
        myLInearLayout.addView(valueB);
 
        valueB.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
 
                // add Button
                valueRelativeLayoutB = new Button(getApplicationContext());
                valueRelativeLayoutB.setText("RelativeLayout");
                valueRelativeLayoutB.setId(5);
 
                //add RelativeLayout
                RelativeLayout mainLayout = new RelativeLayout(getApplicationContext());
                RelativeLayout.LayoutParams lyp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                mainLayout.setLayoutParams(lyp);
 
                //android:layout_below
                lyp.addRule(RelativeLayout.BELOW, valueRelativeLayoutB.getId());
                v.setLayoutParams(lyp);
 
                //The most important line
                setContentView(mainLayout);
 
              //add textView
                valueRelativeLayoutTV = new TextView(getApplicationContext());
                valueRelativeLayoutTV.setText("RelativeLayout");
                valueRelativeLayoutTV.setId(5);
                valueRelativeLayoutTV.setLayoutParams(lyp);
 
                mainLayout.addView(valueRelativeLayoutTV);
                mainLayout.addView(valueRelativeLayoutB);
 
                valueRelativeLayoutB.setOnClickListener(new OnClickListener() {
 
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //Exit app
                        System.exit(0);
                    }
                });
 
            }
        });
 
    }
 
}

You can download this code: Download code

Leave a Reply

Your email address will not be published. Required fields are marked *