Category Archives: Android Studio

How make row in Parse Unique using Parse CloudCode

In the Parse app database there is no option to make a key (object) unique. Usually you can chat using af find before adding a row but when you use .saveinbackground()  you cannot be sure.

Best solution is to add a beforeSave trigger on Parse Server using cloud code. I’m not a cloud code specialist so it took me quite a while to figure it out but using the code below you should be able to do this pretty quick.

This needs no modification of you app.

Add this (and adust to your needs) to you main.js and upload to Parse Server.

myObjectClass is the ‘table name’ in Parse.
myKey is the ‘field’ you want to make unique within your table (object)

Parse.Cloud.beforeSave("myObjectClass", function(request, response) {
    var myObject = request.object;
    
	if (myObject.isNew()){  // only check new records, else its a update
		var query = new Parse.Query("myObjectClass");
		query.equalTo("myKey",myObject.get("myKey"));
		query.count({
			success:function(number){ //record found, don't save
				//sResult = number;
				if (number > 0 ){
					response.error("Record already exists");
				} else {
					response.success();
				}
			},
			error:function(error){ // no record found -> save
				response.success();
			}
		})
    } else {
        response.success();
    }
});

How to change text size of TabLayout Tabs in Android Studio

To change the size of the font 2 steps need to be done:

  1. Add as Style for the Tablayout
  2. Apply the style to the TabLayout in the XML

Find or create res -> values -> styles.xml

1. Add the Style to the XML

<style name="MyCustomTabText" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">12sp</item>
</style>

2. Apply Style

Find the Layout containing the TabLayout and add the style. The added line is bold.

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    app:tabTextAppearance="@style/MyCustomTabText" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

 

How to Add Option / settings menu to android App

 

Add this to your mainactivity:

@Override
public boolean onCreateOptionsMenu(Menu menu){
    // Create menu
    // See onOptionItemSelected for actions when clicked

    Menu settingsMenu = menu;
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.optionmenu, menu);

    MenuItem settingsMenuLogin = settingsMenu.findItem(R.id.settingsMenuExport);

return true;
}

and

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();


    if (id == R.id.settingsMenuExport) {
        // add additional check
        Intent i = new Intent(getApplicationContext(), export.class);
        startActivity(i);
    }
    return super.onOptionsItemSelected(item);
}

and create an xml in res\menu like this:

optionmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/settingsMenuExport"
        android:orderInCategory="100"
        android:title="Export"
        app:showAsAction="never" />
</menu>