Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

FirstActivity

Kay-Uwe Janssen edited this page Oct 9, 2016 · 21 revisions

Now that your project is configured to use AndroidAnnotations, let's have fun with it!

  • Create a new activity (or use an already existing one)
  • Use @EActivity, @ViewById and @Click on the activity, following this example :
import android.app.Activity;
import android.widget.EditText;
import android.widget.TextView;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.main)
public class MyActivity extends Activity {

    @ViewById(R.id.myInput)
    EditText myInput;
       
    @ViewById(R.id.myTextView)
    TextView textView;
        
    @Click
    void myButton() {
         String name = myInput.getText().toString();
    	 textView.setText("Hello "+name);
    }
}

The main.xml layout works as usual :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<EditText  
	    android:id="@+id/myInput"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    />
	<Button  
	    android:id="@+id/myButton"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="Click me!"
	    />        
	<TextView  
		android:id="@+id/myTextView"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    />    
</LinearLayout>
  • Save the file (Eclipse will compile it, and generate a subclass of MyActivity, named MyActivity_). In IntelliJ / Android Studio, click Make to generate.

  • Register MyActivity_ instead of MyActivity in your manifest :

<activity android:name=".MyActivity_" />

The AndroidManifest.xml

You should always register your activity with an _ suffix in the Android Manifest.

This is because AndroidAnnotations generates a subclass for each annotated activity. It has the same package and name, plus an _ suffix.

AndroidAnnotations will tell you if you forget to register your activities in the AndroidManifest.xml.

Finding the AndroidManifest.xml

AndroidAnnotations finds the AndroidManifest.xml file by recursively going up from the generated source folder.

Since AndroidAnnotations 2.7

If this doesn't fit your project structure, you can specify the absolute path to the AndroidManifest.xml by giving an androidManifestFile option to the processor.

  • With javac, just pass the following option: -AandroidManifestFile=/path/to/AndroidManifest.xml
  • With Eclipse, go to Properties > Java Compiler > Annotation Processing and add an entry to Processor options.
  • For other build systems and IDEs, consult the customization page.

To learn more about what you can do with AndroidAnnotations, read the Cookbook and the list of available annotations

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally