Last active
August 29, 2015 14:13
-
-
Save danialgoodwin/8e43146623403f3eb34e to your computer and use it in GitHub Desktop.
DemoAndroidAnnotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<FrameLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" | |
tools:ignore="MergeRootFrame" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="net.simplyadvanced.testandroidannotations" > | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<!-- Make sure to use the underscore postfix version of MainActivity, | |
which should be found by the IDE after building. --> | |
<activity | |
android:name=".MainActivity_" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'android-apt' | |
def AAVersion = '3.2' | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' | |
} | |
} | |
apt { | |
arguments { | |
androidManifestFile variant.outputs[0].processResources.manifestFile | |
resourcePackageName android.defaultConfig.applicationId | |
} | |
} | |
android { | |
compileSdkVersion 21 | |
buildToolsVersion "21.1.2" | |
defaultConfig { | |
applicationId "net.simplyadvanced.testandroidannotations" | |
minSdkVersion 15 | |
targetSdkVersion 21 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:21.0.3' | |
apt "org.androidannotations:androidannotations:$AAVersion" | |
compile "org.androidannotations:androidannotations-api:$AAVersion" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:orientation="vertical" | |
tools:context=".MainFragment" > | |
<Button | |
android:id="@+id/jokeButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Random Joke" /> | |
<TextView | |
android:id="@+id/jokeTextView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Static helper methods related to accessing Internet. */ | |
public class HttpUtils { | |
/** No need to instantiate this class. */ | |
private HttpUtils() {} | |
/** Downloads and returns the information from a given URI in the same thread | |
* this is called in. */ | |
public static String get(String uri) throws IOException, URISyntaxException { | |
return get(new URI(uri)); | |
} | |
/** Downloads and returns the information from a given URI in the same | |
* thread this is called in. The returned String may contain XML, JSON, | |
* or another format. You should check for network connectivity before | |
* calling this. | |
* Note: This is just a simple implementation, it should be made more | |
* robust, like by using a nice library. */ | |
public static String get(URI uri) throws IOException { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpGet httpGet = new HttpGet(); | |
httpGet.setURI(uri); | |
HttpResponse httpResponse = httpClient.execute(httpGet); | |
InputStream inputStream; | |
BufferedReader reader = null; | |
String value = null; | |
try { | |
inputStream = httpResponse.getEntity().getContent(); | |
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); | |
StringBuilder sb = new StringBuilder(inputStream.available()); | |
while ((value = reader.readLine()) != null) { | |
sb.append(value); | |
} | |
value = sb.toString(); | |
} finally { | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (IOException e) { | |
// Close quietly. | |
} | |
} | |
} | |
return value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// By passing in the layout XML here, we don't need to manually call | |
// `setContentView()` in `onCreate()`. | |
@EActivity(R.layout.activity_main) | |
public class MainActivity extends ActionBarActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Don't add a fragment if onCreate() is called on a config change, | |
// one already exists. | |
if (savedInstanceState == null) { | |
getSupportFragmentManager().beginTransaction() | |
// Note the underscore postfix. AndroidAnnotations works | |
// by auto-generating a subclass with code that you | |
// would normally have to write. | |
.add(R.id.container, new MainFragment_()) | |
.commit(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// By passing in layout XML here, we don't need to manually call `onCreateView()`. | |
@EFragment(R.layout.fragment_main) | |
public class MainFragment extends Fragment { | |
// This is the minimum needed to bind a variable to widget in XML. | |
// AndroidAnnotations will look for an id `R.id.jokeTextView`. | |
// Or, you could manually specify the id by passing it in as an | |
// argument to the annotation. | |
@ViewById TextView jokeTextView; | |
// Required no-args constructor. | |
public MainFragment() {} | |
// This annotation will auto-generate code to find a View with id of | |
// `R.id.jokeButton` and set the onClickListener to run this method. | |
@Click | |
void jokeButtonClicked() { | |
loadNewJoke(); | |
} | |
// This annotation makes sure this method is ran in a background thread. | |
@Background | |
void loadNewJoke() { | |
String joke = "N/A"; | |
String jokeJsonString = null; | |
try { | |
jokeJsonString = HttpUtils.get("http://api.icndb.com/jokes/random"); | |
JSONObject jokeJson = new JSONObject(jokeJsonString); | |
if (jokeJson.getString("type").equals("success")) { | |
joke = jokeJson.getJSONObject("value").getString("joke"); | |
} | |
} catch (IOException | JSONException | URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
setJokeText(joke); | |
} | |
// This annotation makes sure this method is ran on the main/UI thread. | |
@UiThread | |
void setJokeText(String joke) { | |
jokeTextView.setText(Html.fromHtml(joke)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment