Skip to content

Instantly share code, notes, and snippets.

@ceefour
Last active August 31, 2021 07:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceefour/bc4c19e6bb8e4edbbacce5599302683f to your computer and use it in GitHub Desktop.
Save ceefour/bc4c19e6bb8e4edbbacce5599302683f to your computer and use it in GitHub Desktop.
Weather for Android (template) + Kinigram notes
// onCreate
// Cara menangani apabila sebuah item di ListView diklik
listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final Article item = (Article) adapterView.getAdapter().getItem(i);
Log.i(TAG, "Item " + i + " clicked: " + item);
// new Intent(...)
// startActivity(...);
}
});
package com.hendyirawan.weather;
import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final int PERM_COARSE_LOCATION = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void tryGetLocation(View view) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERM_COARSE_LOCATION);
// The callback method gets the result of the request.
} else {
realGetLocation();
}
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
Log.i(TAG, String.format("onRequestPermissionsResult permsRequestCode=%s permissions=%s grantResults=%s",
permsRequestCode, Arrays.asList(permissions), Arrays.asList(grantResults)));
switch (permsRequestCode) {
case PERM_COARSE_LOCATION:
boolean locationAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (locationAccepted) {
Log.i(TAG, "ACCESS_COARSE_LOCATION permission granted");
realGetLocation();
} else {
Log.w(TAG, "ACCESS_COARSE_LOCATION permission declined");
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Insufficient Permission");
alertDialog.setMessage("Please allow location access");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
break;
}
}
public void realGetLocation() {
// TODO: lakukan pengambilan data location: mFusedLocationClient.getLastLocation()...
}
}
public class UserProfileActivity extends AppCompatActivity {
private static final String TAG = "UserProfileActivity";
//...
private static final int REQUEST_IMAGE_CAPTURE = 1;
private ImageView profileImageView;
private File photoFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
//...
profileImageView = (ImageView) findViewById(R.id.profile_image);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Log.i(TAG, "onActivityResult REQUEST_IMAGE_CAPTURE photoFile=" + photoFile);
profileImageView.setImageURI(Uri.fromFile(photoFile));
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment