SQLite FAQ: How to create an autoincrement field in SQLite
SQLite FAQ - How do I create an autoincrement field in SQLite?
You define a SQLite autoincrement field (also known as a serial, identity, or primary key field) with this syntax:
SQLite FAQ - How do I create an autoincrement field in SQLite?
You define a SQLite autoincrement field (also known as a serial, identity, or primary key field) with this syntax:
SQLite FAQ: How do I default a SQLite datetime field to the current date and time? (i.e., how do I default it to now?)
Just use the SQLite current_timestamp function, like this:
last_updated datetime default current_timestamp
In a more complete create table example I just used on an Android project, this looks like this:
SQLite FAQ: Can you show me how the SQLite ALTER TABLE
syntax works?
At the time of this writing you can use the SQLite ALTER TABLE
syntax for two purposes:
For other changes you'll have to follow some workaround procedures (discussed below).
SQLite FAQ - How do I create a UNIQUE constraint on multiple fields in a SQLite database table?
Just specify the unique constraint in your create table declaration, as shown here:
SQLite schema FAQ: How do I list the schema for SQLite database table?
To view the schema for a SQLite database table, just use the SQLite schema
command, like this:
SQLite database FAQ: Can you show me an example of the SQLite CREATE TABLE
syntax?
Sure, here's a small collection of SQLite CREATE TABLE
examples I recently created for my other SQLite tutorials:
If you’re working with an Android application, this source code seems to work to load an image from a file:
Bitmap bitmap = BitmapFactory.decodeFile(pathToPicture);
The Bitmap
and BitmapFactory
classes are located in the android.graphics package:
import android.graphics.Bitmap; import android.graphics.BitmapFactory;
Assuming that your pathToPicture
is correct, you can then add this bitmap image to an ImageView
like this:
Before I completely delete this code from my current Android application, I want to make a copy of it here. It was intended to show a series of quotes (text phrases) in a “Grid” (GridView
), but (a) I never got it working as desired, and (b) I decided I didn’t want it in my application anyway.
Here’s the source code for the Java controller/fragment class:
XO Play is a “thinking man’s” football game that I created for Android devices. It’s for those of us who enjoy thinking about football strategy, of how to take advantage of our strengths and our opponents’ weaknesses, and how to call plays to win games. If you’ve ever watched a football game and thought, “I can call plays better than this guy,” XO Play is for you.
Version 1.4 introduces four game levels you can choose from:
The UI still needs a lot of work, but I like the idea of having an Inconceivable mode in the next version of my XO Play Android football game.
I’m working on a very small Android “notifications” app where I a) display a notification using a background service, b) the user taps the notification, which c) takes them to a view that shows the full text of the notification. To get this to work, I need to send the full text along with the notification. However, this didn’t work easily. The text that was shown by my full view would be updated once, then never again.
After a lot of googling and trial and error, I finally got this approach in my sendNotification
method working:
I need a new tablet, and I just might bite the bullet on the Amazon Fire 10 HD today (Black Friday, 2018). For my Android development I don’t like that it’s based on Android 5, but $100 (33% off its regular price) for a tablet with decent performance is hard to pass up for my current needs.
I was just working on centering some components in an Android LinearLayout
and came upon this helpful advice:
android:gravity takes care of its children,
android:layout_gravity takes care of itself
So, in my case, where I wanted to center two buttons horizontally in a LinearLayout
, I used android:gravity="center"
.
Android Button FAQ: How do I add a listener to a Button in Android?
To add a click (tap) listener to a button in Android, just get a reference to the button (typically using findViewById), then call the setOnClickListener method of the button, like this:
Android FAQ: How do I programmatically set the font size (and/or font style) for an Android TextView
?
Solution: Setting the Android TextView
font size programmatically is a simple two-step process. First, define everything about the font that you want to use in a resources file. For example, I put this code in a file named res/values/styles.xml:
I just started working with the Android Room database persistence library, and since you’re not supposed to run things like database queries on the main thread (the UI thread), I was looking at other ways to run them.
In general, you probably won’t want to run database queries using a Thread
, but just to see how Room works, I wrote this Java Thread code, and confirmed that it works as expected:
// works (pre-java8)
Thread t = new Thread() {
public void run() {
mAppDatabase.userDao().insert(u);
}
};
t.start();
Since that code runs in a separate thread, it runs fine even if I enable Android’s StrictMode.
Then I thought, “Hey, if I use the Android Studio 3 Canary Preview, I can use Android’s Java 8 features,” and when I did that I was able to use this Java 8 Thread syntax for the same purpose:
// works (java8)
new Thread(() -> {
mAppDatabase.userDao().insert(u);
}).start();
That also works, and it’s much easier to read than the old Java Thread syntax.
As a quick summary, if you want to use a Java 8 Thread with Android, I hope that last example is helpful. I don’t often interact with a database without calling back to update the UI, but when I’m writing a little test app, or testing new features like the Android Room persistence library, this is a simple approach to keep database I/O code off the main thread.
As another quick Android example, this Java source code shows how to add a “click listener” (on click/tap event) to an Android Button
:
As a quick example of how to use a Thread
with a basic Handler
in an Android application, the following code creates a view where the text in the TextView
is updated to show the current date and time when the Button
is tapped.
First, here’s the Java source code for a file class named ThreadHandlerActivity
:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.Handler;
import android.os.Message;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.alvinalexander.todo.R;
public class ThreadHandlerActivity extends Activity {
private static final String MSG_KEY = "yo";
/**
* perform the action in `handleMessage` when the thread calls
* `mHandler.sendMessage(msg)`
*/
@SuppressLint("HandlerLeak")
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
String string = bundle.getString(MSG_KEY);
final TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(string);
}
};
private final Runnable mMessageSender = new Runnable() {
public void run() {
Message msg = mHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString(MSG_KEY, getCurrentTime());
msg.setData(bundle);
mHandler.sendMessage(msg);
}
};
private String getCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss MM/dd/yyyy", Locale.US);
return dateFormat.format(new Date());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_thread_example);
final Button button = (Button) findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
handleButtonClick(v);
}
});
}
public void handleButtonClick(View view) {
new Thread(mMessageSender).start();
}
@Override
protected void onPause() {
super.onPause();
//TODO not sure if this is needed for this use case
mHandler.removeCallbacks(mMessageSender);
}
}
Next, here’s the layout XML for the view, from a file named activitythreadexample.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="32dp"
android:text="TextView" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Here’s what this little app looks like in an Android emulator:
Please note that this source code is heavily based on the now-old book, Android 4.4 App Development. I was just looking into threads and handlers in Android last night, and this is a relatively simple example of how to send a message from a thread/runnable to a handler.
Note that I had to add the @SuppressLint
annotation to this code. I don’t know much about handlers yet, so I need to look into why that is needed. I do know that Android Studio is telling me that the handler should be declared as a static field. (Adding the annotation isn’t 100% necessary, but it’s a nice reminder that there’s a potential problem here.)
Android error FAQ: Help, I'm developing an Android app, and I'm getting a "Unfortunately your Android app has stopped error message"; how do I fix it?
If you're getting an Android error message that looks like this:
"Unfortunately your Android app has stopped error message"
It's probably because you forgot to put a new Activity in the AndroidManifest.xml file (or at least that's been the #1 cause of this error message in my experience).