Skip to content

Instantly share code, notes, and snippets.

@Tarelochkin
Tarelochkin / PriceFormat.java
Created August 18, 2017 19:50
Print out string in price format (24.5 -> 24.50, 24 -> 24.00)
double itemPrice = cursor.getDouble(priceIndex);
String itemPriceFormatted = String.format(Locale.ROOT, "%.2f", itemPrice);
@Tarelochkin
Tarelochkin / Implementation.java
Created August 18, 2017 19:48
Restrict number of decimal digits in EditText
mPriceEditText = (EditText) findViewById(R.id.price_field);
mPriceEditText.setFilters(new InputFilter[]{new DecimalDigitsInputFilter(5, 2)});
@Tarelochkin
Tarelochkin / SaveToDatabase.java
Last active August 5, 2017 18:07
An example saving to a database of new entries and changes to exisiting ones.
/**
* Content URI for the existing pet (null if it's a new pet)
*/
private Uri mCurrentPetUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
mCurrentPetUri = getIntent().getData();
}
@Tarelochkin
Tarelochkin / ConfirmationDialog.java
Last active August 5, 2017 17:38
Confirmation dialog example
private void showDeleteConfirmationDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.delete_all_dialog_msg);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// close the window on pressing Cancel button
if (dialog != null)
dialog.dismiss();
}
@Tarelochkin
Tarelochkin / SetupSpinner.java
Last active July 29, 2017 11:23
Setting up a spinner using arrays.xml
/**
* Setup the dropdown spinner that allows the user to select the gender of the pet.
*/
private void setupSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
ArrayAdapter genderSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.array_gender_options, android.R.layout.simple_spinner_item);
// Specify dropdown layout style - simple list view with 1 item per line
@Tarelochkin
Tarelochkin / OverflowMenu.java
Last active August 5, 2017 17:42
Overflow menu creation example
public class CatalogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/**
* The custom SearchView provides following features:
* 1) Located below the App Name with the effect of 'multi-level' toolbar.
* 2) Expanded to the full width of the screen with even margin from the both edges.
* 3) The search icon is located inside the text field (not really) as opposed to the default 'outing'
* when implementing setIconifiedByDefault(false);
* 4) Uses semi-transparent background, icons and he hint text.
* 5) Default underline of the text field is removed.
*/
@Tarelochkin
Tarelochkin / ToolbarIcon.java
Created July 28, 2017 19:17
Insert an icon in the toolbar and add padding to it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.xml_app_logo);
}
@Tarelochkin
Tarelochkin / searchable.xml
Created July 22, 2017 19:20
Avoid full screen SearchView in landscape mode
android:imeOptions="actionSearch|flagNoExtractUi|flagNoFullscreen
@Override
public void onStart() {
super.onStart();
Log.d(LOG_TAG, "onStart() called");
}
@Override
public void onPause() {
super.onPause();
Log.d(LOG_TAG, "onPause() called");