How can I open a URL in Android’s web browser from my application?

Try this: Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(“http://www.google.com”)); startActivity(browserIntent); That works fine for me. As for the missing “http://” I’d just do something like this: if (!url.startsWith(“http://”) && !url.startsWith(“https://”)) url = “http://” + url; I would also probably pre-populate your EditText that the user is typing a URL in with “http://”.

How to get screen dimensions as pixels in Android

If you want the display dimensions in pixels you can use getSize: Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; If you’re not in an Activity you can get the default Display via WINDOW_SERVICE: WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); If you … Read more

How to lazy load images in ListView in Android

Here’s what I created to hold the images that my app is currently displaying. Please note that the “Log” object in use here is my custom wrapper around the final Log class inside Android. package com.wilson.android.library; /* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file … Read more

What is ‘Context’ on Android?

Putting it simply: As the name suggests, it’s the context of the current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application). You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this … Read more

How do I center text horizontally and vertically in a TextView?

I’m assuming you’re using XML layout. <TextView android:layout_width=”match_parent” android:layout_height=”match_parent” android:gravity=”center” android:text=”@string/**yourtextstring**” /> You can also use gravity center_vertical or center_horizontal according to your need. As @stealthcopter commented, in java: .setGravity(Gravity.CENTER);. And for Kotlin users, .gravity = Gravity.CENTER

How can I save an activity state using the save instance state?

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this: @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. savedInstanceState.putBoolean(“MyBoolean”, true); … Read more

Is there a unique Android device ID?

Settings.Secure#ANDROID_ID returns the Android ID as an unique for each user 64-bit hex string. import android.provider.Settings.Secure; private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); Also read Best practices for unique identifiers: https://developer.android.com/training/articles/user-data-ids

How to stop EditText from gaining focus when an activity starts in Android?

Adding the tags android:focusableInTouchMode=”true” and android:focusable=”true” to the parent layout (e.g. LinearLayout or ConstraintLayout) like in the following example, will fix the problem. <!– Dummy item to prevent AutoCompleteTextView from receiving focus –> <LinearLayout android:focusable=”true” android:focusableInTouchMode=”true” android:layout_width=”0px” android:layout_height=”0px”/> <!– :nextFocusUp and :nextFocusLeft have been set to the id of this component to prevent the dummy … Read more