Android Spinner

Spinner spinner = findViewById(R.id.currency_spinner);

// Create an ArrayAdapter using the String array and a spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.spinner_values, R.layout.spinner_item);

// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

// Apply the adapter to the spinner
spinner.setAdapter(adapter);


spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Log.d("Spinner Demo", "" + parent.getItemAtPosition(position));
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        Log.d("Spinner Demo", "Nothing selected");
    }
});

Switch between activities

public class MainActivity extends AppCompatActivity {
    private Button mAddButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAddButton = findViewById(R.id.addButton);
        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAddActivity();
            } 
        });
}

    public void openAddActivity(){
       Intent intent = new Intent(this, Main2Activity.class);
       startActivity(intent);
}

Android screen orientation

disable orientation change

<activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    tools:ignore="LockedOrientationActivity">

Handling orientation state
Saving instance state

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("ScoreKey", mScore);
    outState.putInt("IndexKey", mIndex);

}

Retrieving instance state

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState != null){
        mScore = savedInstanceState.getInt("ScoreKey");
        mIndex = savedInstanceState.getInt("IndexKey");

    } else {
        mScore = 0;
        mIndex = 0;
    }

Android App Lifecycle

Code

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("LifecycleTest", "onCreate")
    }

    override fun onStart() {
        super.onStart()
        Log.d("LifecycleTest", "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d("LifecycleTest", "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d("LifecycleTest", "onPause")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("LifecycleTest", "onSaveInstanceState")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        Log.d("LifecycleTest", "onSaveInstanceState")
    }

    override fun onStop() {
        super.onStop()
        Log.d("LifecycleTest", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("LifecycleTest", "onDestroy")
    }
}

https://github.com/jboegeholz/LifecycleTest

Android Enable USB-Debugging

  1. goto Settings > General > About phone > Software information and tap “Build number” seven times until it says “You are now a developer”
  2.  Goto Settings > General > Developer options and enable USB Debugging
  3. Drop down the notifications menu and click on “Tap for more USB options”. Most likely, the main title will say “Media Device (MTP)”. Choose the option “Send Images (PTP)”

Features and Permissions in Android

Features and Permissions

A permission is something an app is allowed to do 🙂

E.g. using the camera, reading location or accessing your contacts.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="true" />

Internet Access

If your app uses e.g. web-APIs you need internet access. This can be achieved by adding android.permission.INTERNET to your manifest. In this case you need the uses-permission tag.

<manifest package="de.creatronix.sample_app"
          xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

INTERNET

Allows applications to open network sockets.

ACCESS_NETWORK_STATE

Allows applications to access information about networks.

Non-TLS APIs

When You have resources which do not use TLS (https but just http) you have to add

<application
        ...
        android:usesCleartextTraffic="true">
</application>

 

Permissions in Services

Checking Permission

import android.content.pm.PackageManager;
int uid = getCallingUid(); 
int pid = getCallingPid(); 
Log.d(LOGTAG, "Message from PID " + pid + " UID " + uid);
int perm = mContext.checkPermission("de.creatronix.my_service.MY_PERM", pid, uid); 
if (perm == PackageManager.PERMISSION_GRANTED) 
    Log.d(LOGTAG, "Granted"); 
else 
    Log.d(LOGTAG, "Denied");

Enforcing Permissions

mContext.enforceCallingPermission("de.creatronix.my_service.MY_PERM", "echoString permission not granted"); 

More about permissions:
https://developer.android.com/reference/android/Manifest.permission.html