-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_13-16_java_layout.txt
More file actions
153 lines (123 loc) · 6.29 KB
/
code_13-16_java_layout.txt
File metadata and controls
153 lines (123 loc) · 6.29 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
[source code] Android Development Tutorials 13 thru 16 - Java Dynamic Layout
***** Tutorial Notes
//Dynamic Layout//
** 13 - Create an Interface with Java
// Overview//
The next 4 tutorials are going to create a layout in the Java code. The activity_main layout gets generated by the Activity template. We are going to leave it in the project for reference purposes, but we are going to create a new RelativeLayout, with a Button, and an EditText, in Java and set the content to the new layout. On the last lesson we are going to learn how to convert DP to pixels because the setMargin() and the setWidth() take parameters in pixels.
Project Creation
Start with a new Empty Activity. You will not get the menu imports or overrides but we are not using them.
In MainActivity.java
1. Delete setContentView, that points to the activity_main xml layout because we are going to create our own Content View.
2. Import android.widget.RelativeLayout; import android.widget.Button;
3. create layout in onCreate( )
a. RelativeLayout buckysLayout = new RelativeLayout(this);
b. Button redButton = new Button(this);
c. buckysLayout.addView(redButton); // this is how you add widgets to the layout
d. setContentView(buckysLayout);
Run the app on a AVD to test progress so far.
** 14 - Adding Properties to Widgets
1. Import android.graphics.color. So you can use color constants to set colors
a. buckysLayout.setBackgroundColor(Color.GREEN);
b. redButton.setText("Click me Hoss");
c. redButton.setBackgroundColor(Color.RED);
//LayoutParams//
Every layout has a LayoutParams object. Layout parameters are used to control how a view appears relative to its parent view and siblings' views. The LayoutParams object is typically created with how the view is to be positioned relative to its parent. Once the LayoutParams object exists, then "Rules" can be added for other adjustments.
NOTE: There is a different ViewGroup.LayoutParam class for each type of layout.
2. Reposition the button with “rules”
a. Make a container by creating a new RelativeLayout.LayoutParams
i. Add wrap content statements.
b. After the RelativeLayout.LayoutParams object is created, you can add rules for the button to center it.
3. Add button to the layout with the LayoutParams.
a. buckysLayout.addView(redButton, buttonDetails);
** 15 - Adding More Widgets
1. import android.widget.EditText;
2. Create new EditText object - EditText username = new EditText(this);
3. Add ids
a. redButton.setId(1);
b. username.setId(2);
4. Copy and paste buttonDetails layoutParms and change them to be for usernameDetails.
5. Add a rule , using setRule(), to position above button and center horizontal.
6. Add rule to setMargins
a. usernameDetails.setMargins(0,0,0,50);
7. Add Edit View rules to the layout
a. buckysLayout.addView(username, usernameDetails);
TIP! Highlight setMargins, Ctrl-Q displays the docs for the method.
** 16 - Converting DIP to Pixels
The goal here is to have our widget be the same on any device. The setWidth() method takes an integer parameter in pixels. NOTE: you could have just used setWidth with a value, but it good to know how to convert if you ever need it.
1. import android.content.res.Resources; and import android.util.TypedValue;
2. To get information about your app use getResources();
3. Create the var px that will be the dp converted to pixels using the method TypedValue.applyDimension() method that takes 3 parameters including the screen size.
4. username.setWidth(px);
***** manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thenewboston.allison">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
***** MainActivity.java
package com.thenewboston.allison;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.widget.EditText;
import android.content.res.Resources;
import android.util.TypedValue;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Layout
RelativeLayout buckysLayout = new RelativeLayout(this);
buckysLayout.setBackgroundColor(Color.GREEN);
//Button
Button redButton = new Button(this);
redButton.setText("Log In");
redButton.setBackgroundColor(Color.RED);
//Username input
EditText username = new EditText(this);
redButton.setId(1);
username.setId(2);
RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
//Give rules to position widgets
usernameDetails.addRule(RelativeLayout.ABOVE, redButton.getId());
usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
usernameDetails.setMargins(0,0,0,50);
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,
r.getDisplayMetrics()
);
username.setWidth(px);
//Add widget to layout(button is now a child of layout)
buckysLayout.addView(redButton, buttonDetails);
buckysLayout.addView(username, usernameDetails);
//Set this activities content/display to this view
setContentView(buckysLayout);
}
}
***** activity_main.xml
Not needed
***** strings.xml
None created