Skip to content

Instantly share code, notes, and snippets.

@vxhviet
vxhviet / OnClickListenter.md
Last active October 26, 2021 19:06
setOnclickListener vs OnClickListener vs View.OnClickListener

Source: StackOverflow

Personal Note: Prefer method 2 as Big Nerd Ranch's Android Programming also use this method.

Imagine that we have 3 buttons for example

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@vxhviet
vxhviet / newActivityFromSearchView.md
Last active March 19, 2021 10:21
Start new activity from SearchView

Source: StackOverflow

This answer is a little late but I feel it'll be useful for future viewers. The dilemma seems to come from the ambiguity of the Android SearchView tutorial. The scenario they cover assumes you will be displaying the results in the same Activity the SearchView resides. In such a scenario, the Activity tag in the AndroidManifest.xml file would look something like this:

<activity
    android:name=".MainActivity"
    android:label="@string/main_activity_label"
    android:launchMode="singleTop">
        <intent-filter>
@vxhviet
vxhviet / questionmarkSQL.md
Last active March 5, 2016 12:02
What does a question mark represent in SQL queries?

Source: StackOverflow

Question: While going through some SQL books I found that examples tend to use question marks (?) in their queries. What does it represent?

Answer: What you are seeing is a parameterized query. They are frequently used when executing dynamic SQL from a program.

For example, instead of writing this (note: pseudocode):

ODBCCommand cmd = new ODBCCommand("SELECT thingA FROM tableA WHERE thingB = 7")
@vxhviet
vxhviet / RealVideoPath.md
Last active April 23, 2023 11:22
How to select a video from the gallery and get it's real path?

Source: StackOverflow & StackOverflow

Question: How can I get the real path of the selected video when the results are returned?

Answer: Here is the full code for get the video path after selecting from gallery.

Intent intent = new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);
@vxhviet
vxhviet / 0dp.md
Last active August 29, 2021 02:39
Why is 0dp considered a performance enhancement? & Avoid wrap_content on ListView
@vxhviet
vxhviet / runtimePermission.md
Last active March 21, 2016 07:19
Android M new runtime Permission
@vxhviet
vxhviet / orientation.md
Last active March 25, 2016 06:17
Detect orientation of a recorded video in Android

Source: StackOverflow

Question: How to detect orientation info of video?

Answer: For Api level 17+, we can extract the orientation of video: MediaMetadataRetriever

MediaMetadataRetriever m = new MediaMetadataRetriever();

m.setDataSource(path);
@vxhviet
vxhviet / filename.md
Last active March 30, 2016 03:57
Current timestamp as filename in Java, Android

Source: StackOverflow

Question: How do I name the new files created with the current timestamp?

Answer:

String fileName = new SimpleDateFormat("yyyyMMddhhmm'.txt'").format(new Date());
@vxhviet
vxhviet / saveToExternalStorage.md
Last active March 3, 2024 20:19
Android saving Bitmap to external storage.

Source: StackOverflow

Question: How do I save Bitmap to extrenal storage in Android?

Answer:

Use this function to save your bitmap in SD card:

public void saveTempBitmap(Bitmap bitmap) {
        if (isExternalStorageWritable()) {
@vxhviet
vxhviet / pauseCode.md
Last active March 30, 2016 04:46
How do you have the code pause for a couple of seconds in android?

Source: StackOverflow

Question: How do you have the code pause for a couple of seconds in android?

Answer: Learning to think in terms of events is indeed the key here. You can do it. :)

The first rule is: never stall the UI thread. The UI thread is responsible for keeping your app feeling responsive. Any work you do there should not block; do what you need to do and return as quickly as possible. Definitely avoid doing I/O on the UI thread. (There are some places where you can't really help it due to lifecycle requirements, for example saving app state in onPause.) If you ever call Thread.sleep on the UI thread you are doing it wrong.

Android enforces this with the "Application not responding" (or "ANR") error that the user sees. Whenever you see this in an Android app it means the developer did something that caused the UI thread to stall for too long. If the device is really