Skip to content

Commit c44241f

Browse files
authored
Merge pull request #61 from glwhitney/master
Contributions to Android
2 parents 6c13e76 + e2c28cf commit c44241f

File tree

20 files changed

+2484
-0
lines changed

20 files changed

+2484
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
[source code] Android Development Tutorials - 11 & 12 - User Interface
2+
3+
***** Main Activity.java
4+
package your.package.name
5+
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.os.Bundle;
8+
import android.view.Menu;
9+
import android.view.MenuItem;
10+
11+
public class MainActivity extends AppCompatActivity {
12+
13+
@Override
14+
protected void onCreate(Bundle savedInstanceState) {
15+
super.onCreate(savedInstanceState);
16+
setContentView(R.layout.activity_main);
17+
}
18+
19+
@Override
20+
public boolean onCreateOptionsMenu(Menu menu) {
21+
// Inflate the menu; this adds items to the action bar if it is present.
22+
getMenuInflater().inflate(R.menu.menu_main, menu);
23+
return true;
24+
}
25+
26+
@Override
27+
public boolean onOptionsItemSelected(MenuItem item) {
28+
// Handle action bar item clicks here. The action bar will
29+
// automatically handle clicks on the Home/Up button, so long
30+
// as you specify a parent activity in AndroidManifest.xml.
31+
int id = item.getItemId();
32+
33+
//noinspection SimplifiableIfStatement
34+
if (id == R.id.action_settings) {
35+
return true;
36+
}
37+
38+
return super.onOptionsItemSelected(item);
39+
}
40+
}
41+
42+
43+
***** activity_main.xml
44+
45+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
46+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
47+
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
48+
android:paddingRight="@dimen/activity_horizontal_margin"
49+
android:paddingTop="@dimen/activity_vertical_margin"
50+
android:paddingBottom="@dimen/activity_vertical_margin"
51+
tools:context="glwhitney.info.nbvideos11_12.MainActivity">
52+
53+
<TextView
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content"
56+
android:textAppearance="?android:attr/textAppearanceLarge"
57+
android:text="@string/SignInTitle"
58+
android:id="@+id/textView"
59+
android:layout_alignParentTop="true"
60+
android:layout_alignParentLeft="true"
61+
android:layout_alignParentStart="true"
62+
android:singleLine="true"/>
63+
64+
<EditText
65+
android:layout_width="wrap_content"
66+
android:layout_height="wrap_content"
67+
android:inputType="textEmailAddress"
68+
android:ems="10"
69+
android:id="@+id/editText"
70+
android:layout_below="@+id/textView"
71+
android:layout_centerHorizontal="true"
72+
android:layout_marginTop="36dp"
73+
android:width="320dp"/>
74+
75+
<EditText
76+
android:layout_width="wrap_content"
77+
android:layout_height="wrap_content"
78+
android:inputType="textPassword"
79+
android:ems="10"
80+
android:id="@+id/editText2"
81+
android:layout_marginTop="45dp"
82+
android:layout_below="@+id/editText"
83+
android:layout_alignLeft="@+id/editText"
84+
android:layout_alignStart="@+id/editText"
85+
android:width="320dp"/>
86+
87+
<Button
88+
android:layout_width="wrap_content"
89+
android:layout_height="wrap_content"
90+
android:text="@string/SignInButtonText"
91+
android:id="@+id/button"
92+
android:layout_centerVertical="true"
93+
android:layout_centerHorizontal="true" />
94+
</RelativeLayout>
95+
96+
***** strings.xml
97+
98+
<resources>
99+
<string name="app_name">NB Videos 11_12</string>
100+
<string name="title_activity_main">MainActivity</string>
101+
102+
<string name="hello_world">Hello world!</string>
103+
<string name="action_settings">Settings</string>
104+
<string name="SignInTitle">Sign In</string>
105+
<string name="SignInButtonText">Log In</string>
106+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* API23 WARNING
2+
* This activity was created with API21 when ActioinBarActivity was not depreciated.
3+
* IF you are using API23 or later then you will need to extend AppCompatActivity
4+
*/
5+
****** MainActivity
6+
7+
package your.package.name
8+
9+
import android.support.v7.app.ActionBarActivity;
10+
import android.os.Bundle;
11+
import android.view.Menu;
12+
import android.view.MenuItem;
13+
import android.widget.RelativeLayout;
14+
import android.widget.Button;
15+
import android.graphics.Color;
16+
import android.widget.EditText;
17+
import android.content.res.Resources;
18+
import android.util.TypedValue;
19+
20+
public class MainActivity extends ActionBarActivity {
21+
22+
@Override
23+
protected void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
26+
//Layout
27+
RelativeLayout buckysLayout = new RelativeLayout(this);
28+
buckysLayout.setBackgroundColor(Color.GREEN);
29+
30+
//Button
31+
Button redButton = new Button(this);
32+
redButton.setText("Log In");
33+
redButton.setBackgroundColor(Color.RED);
34+
35+
//Username input
36+
EditText username = new EditText(this);
37+
38+
redButton.setId(1);
39+
username.setId(2);
40+
41+
RelativeLayout.LayoutParams buttonDetails = new RelativeLayout.LayoutParams(
42+
RelativeLayout.LayoutParams.WRAP_CONTENT,
43+
RelativeLayout.LayoutParams.WRAP_CONTENT
44+
);
45+
RelativeLayout.LayoutParams usernameDetails = new RelativeLayout.LayoutParams(
46+
RelativeLayout.LayoutParams.WRAP_CONTENT,
47+
RelativeLayout.LayoutParams.WRAP_CONTENT
48+
);
49+
50+
//Give rules to position widgets
51+
usernameDetails.addRule(RelativeLayout.ABOVE, redButton.getId());
52+
usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
53+
usernameDetails.setMargins(0,0,0,50);
54+
55+
buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
56+
buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);
57+
58+
Resources r = getResources();
59+
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,200,
60+
r.getDisplayMetrics()
61+
);
62+
63+
username.setWidth(px);
64+
65+
//Add widget to layout(button is now a child of layout)
66+
buckysLayout.addView(redButton, buttonDetails);
67+
buckysLayout.addView(username, usernameDetails);
68+
69+
//Set this activities content/display to this view
70+
setContentView(buckysLayout);
71+
72+
}
73+
74+
75+
@Override
76+
public boolean onCreateOptionsMenu(Menu menu) {
77+
// Inflate the menu; this adds items to the action bar if it is present.
78+
getMenuInflater().inflate(R.menu.menu_main, menu);
79+
return true;
80+
}
81+
82+
@Override
83+
public boolean onOptionsItemSelected(MenuItem item) {
84+
// Handle action bar item clicks here. The action bar will
85+
// automatically handle clicks on the Home/Up button, so long
86+
// as you specify a parent activity in AndroidManifest.xml.
87+
int id = item.getItemId();
88+
89+
//noinspection SimplifiableIfStatement
90+
if (id == R.id.action_settings) {
91+
return true;
92+
}
93+
94+
return super.onOptionsItemSelected(item);
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[source code] Android Development Tutorial - 17 GridLayout
2+
3+
***** MainActivity.java
4+
5+
package your.package.name;
6+
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.os.Bundle;
9+
import android.view.Menu;
10+
import android.view.MenuItem;
11+
12+
public class MainActivity extends AppCompatActivity {
13+
14+
@Override
15+
protected void onCreate(Bundle savedInstanceState) {
16+
super.onCreate(savedInstanceState);
17+
setContentView(R.layout.activity_main);
18+
}
19+
20+
@Override
21+
public boolean onCreateOptionsMenu(Menu menu) {
22+
// Inflate the menu; this adds items to the action bar if it is present.
23+
getMenuInflater().inflate(R.menu.menu_main, menu);
24+
return true;
25+
}
26+
27+
@Override
28+
public boolean onOptionsItemSelected(MenuItem item) {
29+
// Handle action bar item clicks here. The action bar will
30+
// automatically handle clicks on the Home/Up button, so long
31+
// as you specify a parent activity in AndroidManifest.xml.
32+
int id = item.getItemId();
33+
34+
//noinspection SimplifiableIfStatement
35+
if (id == R.id.action_settings) {
36+
return true;
37+
}
38+
39+
return super.onOptionsItemSelected(item);
40+
}
41+
}
42+
43+
***** activity_main.xml
44+
45+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
46+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
47+
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
48+
android:paddingRight="@dimen/activity_horizontal_margin"
49+
android:paddingTop="@dimen/activity_vertical_margin"
50+
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
51+
52+
53+
<GridLayout
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content"
56+
android:layout_alignParentTop="true"
57+
android:layout_alignParentLeft="true"
58+
android:layout_alignParentStart="true">
59+
60+
<Button
61+
android:layout_width="wrap_content"
62+
android:layout_height="wrap_content"
63+
android:text="Bottom"
64+
android:id="@+id/button4"
65+
android:layout_row="1"
66+
android:layout_column="0"
67+
android:layout_columnSpan="2"
68+
android:layout_gravity="fill_horizontal" />
69+
70+
<Button
71+
android:layout_width="wrap_content"
72+
android:layout_height="wrap_content"
73+
android:text="Center"
74+
android:id="@+id/button"
75+
android:layout_row="0"
76+
android:layout_column="1" />
77+
78+
<Button
79+
android:layout_width="wrap_content"
80+
android:layout_height="wrap_content"
81+
android:text="Left"
82+
android:id="@+id/button3"
83+
android:layout_row="0"
84+
android:layout_column="0" />
85+
86+
<Button
87+
android:layout_width="wrap_content"
88+
android:layout_height="wrap_content"
89+
android:text="Right"
90+
android:id="@+id/button2"
91+
android:layout_row="0"
92+
android:layout_column="2"
93+
android:layout_rowSpan="2"
94+
android:layout_gravity="fill_vertical" />
95+
</GridLayout>
96+
</RelativeLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* API23 WARNING
2+
* This activity was created with API21 when ActioinBarActivity was not depreciated.
3+
* IF you are using API23 or later then you will need to extend AppCompatActivity
4+
*/
5+
****** MainActivity
6+
7+
package your.package.name
8+
9+
import android.support.v7.app.ActionBarActivity;
10+
import android.os.Bundle;
11+
import android.view.Menu;
12+
import android.view.MenuItem;
13+
import android.view.View;
14+
import android.widget.Button;
15+
import android.widget.TextView;
16+
17+
18+
public class MainActivity extends ActionBarActivity {
19+
20+
@Override
21+
protected void onCreate(Bundle savedInstanceState) {
22+
super.onCreate(savedInstanceState);
23+
setContentView(R.layout.activity_main);
24+
25+
Button buckysButton = (Button)findViewById(R.id.buckysButton);
26+
27+
buckysButton.setOnClickListener(
28+
new Button.OnClickListener(){
29+
public void onClick(View v){
30+
TextView buckysText = (TextView)findViewById(R.id.buckysText);
31+
buckysText.setText("Good job Hoss!");
32+
}
33+
}
34+
);
35+
}
36+
37+
38+
@Override
39+
public boolean onCreateOptionsMenu(Menu menu) {
40+
// Inflate the menu; this adds items to the action bar if it is present.
41+
getMenuInflater().inflate(R.menu.menu_main, menu);
42+
return true;
43+
}
44+
45+
@Override
46+
public boolean onOptionsItemSelected(MenuItem item) {
47+
// Handle action bar item clicks here. The action bar will
48+
// automatically handle clicks on the Home/Up button, so long
49+
// as you specify a parent activity in AndroidManifest.xml.
50+
int id = item.getItemId();
51+
52+
//noinspection SimplifiableIfStatement
53+
if (id == R.id.action_settings) {
54+
return true;
55+
}
56+
57+
return super.onOptionsItemSelected(item);
58+
}
59+
}

0 commit comments

Comments
 (0)