Skip to content

Instantly share code, notes, and snippets.

@lovellfelix
Last active December 16, 2015 04:19
Show Gist options
  • Save lovellfelix/5376420 to your computer and use it in GitHub Desktop.
Save lovellfelix/5376420 to your computer and use it in GitHub Desktop.
Android Code Snippets
//Active Internet checker
public static boolean isNetworkConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
return (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected());
}
//check for network activity
if(isNetworkConnected(this)){
//Internet is working
}else {
//Internet is not working
}
//File picker
select = (Button) findViewById(R.id.btn_select);
select.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// start file chooser
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(
Intent.createChooser(intent, "Select a file to upload"),
FILE_SELECT_CODE);
}
});
//UI
//Custom fonts
Typeface tfText = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Regular.ttf");
TextView err = (TextView) findViewById(R.id.login_error);
err.setTypeface(tfText);
//Action bar
ActionBar actionBar = getSupportActionBar(); //show Action bar
actionBar.setDisplayHomeAsUpEnabled(true); //Enable Action bar back button
getSupportActionBar().setIcon(R.drawable.ic_sidemenu_icon); //Customize Action bar icon
getSupportActionBar().setDisplayShowHomeEnabled(false); //remove home icon
getSupportActionBar().setTitle("Main Title"); //Custom title
getSupportActionBar().setSubtitle("Sub Title"); //Custom sub title
//Custom item in actionbar
//Inflate the custom view
View customNav = LayoutInflater.from(this).inflate(R.layout.nav_custom_view, null); //create nav_custom_view in layout
//Attach to the action bar
getSupportActionBar().setCustomView(customNav);
getSupportActionBar().setDisplayShowCustomEnabled(true);
//Onclick events
//Return to previous activity [back functionality on button click]
Button backButton = (Button)this.findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//OnClick method for multiple buttons
@Override
public void onCreate(Bundle savedInstanceState) {
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
button3.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
//DO something
break;
case R.id.button2:
//DO something
break;
case R.id.button3:
//DO something
break;
}
}
};
//AMAZON EC2
//upload type
// Add Content-Type value for uploaded images
// Ensure that the image will be treated as such.
ResponseHeaderOverrides override = new ResponseHeaderOverrides();
override.setContentType("image/jpeg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment