Skip to content

Instantly share code, notes, and snippets.

@shershen08
Last active November 10, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shershen08/af808a7187c6172561a94bc72e9c9454 to your computer and use it in GitHub Desktop.
Save shershen08/af808a7187c6172561a94bc72e9c9454 to your computer and use it in GitHub Desktop.
Android app development for frontend developers

UI

Showing Toast
  Button linkBtn = (Button) findViewById(R.id.button_toast);
        linkBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(GolfcourseDetailActivity.this, "Here's your toast!",
                        Toast.LENGTH_LONG).show();
            }
        });

tutorial link

Showing alert
builder = new AlertDialog.Builder(this);
//Setting message manually and performing action on button click
builder.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //seems to close the app!!
        finish();
    }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //  Action for 'NO' Button
        dialog.cancel();
    }
});

 AlertDialog alert = builder.create();
//Setting the title manually
//alert.setTitle(title);
//alert.setMessage(message);
alert.show();
Set background of the element in a View

attribute: android:background="@drawable/ID_OF_IMAGE"

Use string value from resources in Java R.string.app_names
Activity set title:
  • in XML - android:label="@strings/my_activity_label"
  • in JAVA - setTitle("Hello StackOverflow");

1.3) How to add image to view

Using drawables; first declare in res/drawables, then use in layout:

```
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ID_OF_THE_DRAWABLE"/>
        ```

1.4) Show back button in the bar

```
getActionBar().setDisplayHomeAsUpEnabled(true);
```

1.5) Show HTML page in the Activity

Add html/css to assets, then use it in the activity:

	WebView myWebView = (WebView) findViewById(R.id.webview);
	myWebView.loadUrl("file:///android_asset/components_webpage.html");

"However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list."

1.6) Content lists

1.7) Open another activity by click on the button

In Java class:

    public void openListActivity (View v) {
        Intent intent = new Intent(this, MY_ACTIVITY_CLASS_NAME.class);
        startActivity(intent);
    }

then in the layout XML:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Back to list"
	android:id="@+id/button3"
	android:onClick="openListActivity"
	android:layout_alignParentBottom="true"
	android:layout_centerHorizontal="true" />

Build/DEV process

  • see: build.gradle
  • Gradle and build process
  • Put messages to log:
Log.d("MY_ACTIVITY_NAME", "opening state...");

How to open link in a browser

In Java class

private void goToUrl (String url) {
     Uri uriUrl = Uri.parse(url);
     Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
     startActivity(launchBrowser);
 }
 private void goToSu () {
     String addres = "http://stackoverflow.com/";
     goToUrl(addres);
 }
 ```

then in layout template:

```

5) Network

  • How to call json API in JAVA - Android app Volley or Retrofit

How to store data

SharedPreferences pref = getApplicationContext().getSharedPreferences(SAVE_PREF_ID, MODE_PRIVATE);
SharedPreferences.Editor sp = pref.edit();
sp.putString(SAVE_KEY_NAME, title);
//..
sp.apply();

7) General

<intent-filter>
	<action android:name="android.intent.action.MAIN" />
	<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

8) Sample apps

9) Tutorials and videos

10) Tips and best practices

11) Libraries

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment