This class provides a basic demonstration of how to write an Android activity.
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
* This class provides a basic demonstration of how to write an Android
* activity. Inside of its window, it places a single view: an EditText that
* displays and edits some internal text.
*/
public class Test extends Activity {
static final private int BACK_ID = Menu.FIRST;
static final private int CLEAR_ID = Menu.FIRST + 1;
private EditText mEditor;
public Test() {
}
/** Called with the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate our UI from its XML layout description.
setContentView(R.layout.skeleton_activity);
// Find the text editor view inside the layout, because we
// want to do various programmatic things with it.
mEditor = (EditText) findViewById(R.id.editor);
// Hook up button presses to the appropriate event handler.
((Button) findViewById(R.id.back)).setOnClickListener(mBackListener);
((Button) findViewById(R.id.clear)).setOnClickListener(mClearListener);
mEditor.setText(getText(R.string.main_label));
}
/**
* Called when the activity is about to start interacting with the user.
*/
@Override
protected void onResume() {
super.onResume();
}
/**
* Called when your activity's options menu needs to be created.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// We are going to create two menus. Note that we assign them
// unique integer IDs, labels from our string resources, and
// given them shortcuts.
menu.add(0, BACK_ID, 0, R.string.back).setShortcut('0', 'b');
menu.add(0, CLEAR_ID, 0, R.string.clear).setShortcut('1', 'c');
return true;
}
/**
* Called right before your activity's option menu is displayed.
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
// Before showing the menu, we need to decide whether the clear
// item is enabled depending on whether there is text to clear.
menu.findItem(CLEAR_ID).setVisible(mEditor.getText().length() > 0);
return true;
}
/**
* Called when a menu item is selected.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case BACK_ID:
finish();
return true;
case CLEAR_ID:
mEditor.setText("");
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A call-back for when the user presses the back button.
*/
OnClickListener mBackListener = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
/**
* A call-back for when the user presses the clear button.
*/
OnClickListener mClearListener = new OnClickListener() {
public void onClick(View v) {
mEditor.setText("");
}
};
}
//
//res\layout\skeleton_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This file describes the layout of the main SkeletonApp activity
user interface.
-->
<!-- The top view is a layout manager that places its child views into
a row, here set to be vertical (so the first is at the top) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<!-- First view is a text editor. We want it to use all available
horizontal space, and stretch to fill whatever vertical space
is available to it. Note the use of the "id" attribute, which
allows us to find this object from the Java code. -->
<EditText android:id="@+id/editor"
android:layout_width="match_parent" android:layout_height="0dip"
android:autoText="true"
android:capitalize="sentences"
android:layout_weight="1"
android:freezesText="true" >
<requestFocus />
</EditText>
<!-- Next view is another linear layout manager, now horizontal. We
give it a custom background; see colors.xml for the definition
of drawable/semi_black-->
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:gravity="center_horizontal"
android:orientation="horizontal"
android:background="@drawable/semi_black">
<!-- On the left: the "back" button. See styles.xml for the
definition of style/ActionButton, which we use to hold
common attributes that are used for both this and the
clear button. See strings.xml for the definition of
string/back. -->
<Button android:id="@+id/back" style="@style/ActionButton"
android:text="@string/back" />
<!-- In the middle: a custom image, -->
<ImageView android:id="@+id/image"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="4dip" android_paddingRight="4dip"
android:src="@drawable/violet" />
<!-- On the right: another button, this time with its text color
changed to red. Again, see colors.xml for the definition. -->
<Button android:id="@+id/clear" style="@style/ActionButton"
android:text="@string/clear" android:textColor="@color/red" />
</LinearLayout>
</LinearLayout>
//
//res\values\colors.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This file contains resource definitions for solid colors. In
addition to plain color resources (retrieved via Resources.getColor()
and friends), we are also using it to define drawables that are
a solid color. -->
<resources>
<!-- Retrieved via Resources.getColor() and friends. -->
<color name="red">#f00</color>
<!-- Retrieved via Resources.getDrawable() and friends. -->
<drawable name="semi_black">#80000000</drawable>
</resources>
//res\values\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This file contains resource definitions for displayed strings, allowing
them to be changed based on the locale and options. -->
<resources>
<!-- Simple strings. -->
<string name="skeleton_app">Skeleton App</string>
<string name="back">Back</string>
<string name="clear">Clear</string>
<!-- This is a complex string containing style runs. -->
<string name="main_label">Hello <u>th<ignore>e</ignore>re</u>, <i>you</i> <b>Activity</b>!</string>
</resources>
//res\values\styles.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This file contains resource definitions for style. A style is
a collection of named values that can be applied as a group. Here,
we are using a style to define a common set of XML attributes that
will be used in multiple tags. -->
<resources>
<style name="ActionButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textAppearance">@style/TextAppearance.ActionButton</item>
</style>
<style name="TextAppearance" parent="android:TextAppearance">
</style>
<style name="TextAppearance.ActionButton">
<item name="android:textStyle">italic</item>
</style>
</resources>
Related examples in the same category