Most votes on android questions 7

Most votes on android questions 7. #61 Converting pixels to dp #62 How to send an object from one Android Activity to another using Intents? #63 How can I connect to Android with ADB over TCP? #64 What is the simplest and most robust way to get the user's current location on Android? #65 How to pass an object from one activity to another on Android #66 Dilemma: when to use Fragments vs Activities: #67 Unfortunately MyApp has stopped. How can I solve this? #68 Making TextView scrollable on Android #69 How to call a method after a delay in Android #70 android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

Read all the top votes questions and answers in a single page.

#61: Converting pixels to dp (Score: 890)

Created: 2011-01-05 Last updated: 2020-04-17

Tags: android, pixel, resolution, dpi

I have created my application with the height and width given in pixels for a Pantech device whose resolution is 480x800.

I need to convert height and width for a G1 device.
I thought converting it into dp will solve the problem and provide the same solution for both devices.

Is there any easy way to convert pixels to dp?
Any suggestions?

#61 Best answer 1 of Converting pixels to dp (Score: 1089)

Created: 2011-06-13 Last updated: 2021-02-05

Java code:

// Converts 14 dip into its equivalent px
float dip = 14f;
Resources r = getResources();
float px = TypedValue.applyDimension(

Kotlin code:

 val dip = 14f
 val r: Resources = resources
 val px = TypedValue.applyDimension(

#61 Best answer 2 of Converting pixels to dp(Score: 902)

Created: 2012-03-05 Last updated: 2018-12-24

 * This method converts dp unit to equivalent pixels, depending on device density. 
 * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent px equivalent to dp depending on device density
public static float convertDpToPixel(float dp, Context context){
    return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);

 * This method converts device specific pixels to density independent pixels.
 * @param px A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
public static float convertPixelsToDp(float px, Context context){
    return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);

See also original question in stackoverflow

#62: How to send an object from one Android Activity to another using Intents? (Score: 884)

Created: 2010-01-26 Last updated: 2020-04-17

Tags: android, android-intent, android-activity

How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?

#62 Best answer 1 of How to send an object from one Android Activity to another using Intents? (Score: 775)

Created: 2010-01-26 Last updated: 2019-04-03

If you’re just passing objects around then Parcelable was designed for this. It requires a little more effort to use than using Java’s native serialization, but it’s way faster (and I mean way, WAY faster).

From the docs, a simple example for how to implement is:

// simple class that just has one member property as an example
public class MyParcelable implements Parcelable {
    private int mData;

    /* everything below here is for implementing Parcelable */

    // 99.9% of the time you can just ignore this
    public int describeContents() {
        return 0;

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
        public MyParcelable createFromParcel(Parcel in) {
            return new MyParcelable(in);

        public MyParcelable[] newArray(int size) {
            return new MyParcelable[size];

    // example constructor that takes a Parcel and gives you an object populated with it's values
    private MyParcelable(Parcel in) {
        mData = in.readInt();

Observe that in the case you have more than one field to retrieve from a given Parcel, you must do this in the same order you put them in (that is, in a FIFO approach).

Once you have your objects implement Parcelable it’s just a matter of putting them into your Intents with putExtra():

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);

Then you can pull them back out with getParcelableExtra():

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

If your Object Class implements Parcelable and Serializable then make sure you do cast to one of the following:

i.putExtra("parcelable_extra", (Parcelable) myParcelableObject);
i.putExtra("serializable_extra", (Serializable) myParcelableObject);

#62 Best answer 2 of How to send an object from one Android Activity to another using Intents?(Score: 202)

Created: 2010-01-26 Last updated: 2018-11-05

You’ll need to serialize your object into some kind of string representation. One possible string representation is JSON, and one of the easiest ways to serialize to/from JSON in android, if you ask me, is through Google GSON.

In that case you just put the string return value from (new Gson()).toJson(myObject); and retrieve the string value and use fromJson to turn it back into your object.

If your object isn’t very complex, however, it might not be worth the overhead, and you could consider passing the separate values of the object instead.

See also original question in stackoverflow

#63: How can I connect to Android with ADB over TCP? (Score: 879)

Created: 2010-04-09 Last updated: 2014-03-16

Tags: android, networking, tcp, debugging, adb

I am attempting to debug an application on a Motorola Droid, but I am having some difficulty connecting to the device via USB. My development server is a Windows 7 64-bit VM running in Hyper-V, and so I cannot connect directly via USB in the guest or from the host.

I installed a couple of different USB-over-TCP solutions, but the connection appears to have issues since the ADB monitor reports “devicemonitor failed to start monitoring” repeatedly. Is there a way to connect directly from the client on the development machine to the daemon on the device using the network instead of the USB connection or possibly another viable options?

#63 Best answer 1 of How can I connect to Android with ADB over TCP? (Score: 1205)

Created: 2010-09-02 Last updated: 2020-06-20

Manual Process

From your device, if it is rooted

According to a post on xda-developers, you can enable ADB over Wi-Fi from the device with the commands:

setprop service.adb.tcp.port 5555
stop adbd
start adbd

And you can disable it and return ADB to listening on USB with

setprop service.adb.tcp.port -1
stop adbd
start adbd

From a computer, if you have USB access already (no root required)

It is even easier to switch to using Wi-Fi, if you already have USB. From a command line on the computer that has the device connected via USB, issue the commands

adb tcpip 5555
adb connect

Be sure to replace with the IP address that is actually assigned to your device. Once you are done, you can disconnect from the adb tcp session by running:

adb disconnect

You can find the IP address of a tablet in two ways:

Manual IP Discovery:

Go into Android’s WiFi settings, click the menu button in the action bar (the vertical ellipsis), hit Advanced and see the IP address at the bottom of the screen.

Use ADB to discover IP:

Execute the following command via adb:

adb shell ip -f inet addr show wlan0

To tell the ADB daemon return to listening over USB

adb usb

Apps to automate the process

There are also several apps on Google Play that automate this process. A quick search suggests adbWireless, WiFi ADB and ADB WiFi. All of these require root access, but adbWireless requires fewer permissions.

#63 Best answer 2 of How can I connect to Android with ADB over TCP?(Score: 132)

Created: 2010-09-18 Last updated: 2014-03-16

This is really simple if your phone is rooted.

Download a terminal emulator from Google Play (there are lots that are free). Make sure that your Android device is connected to your Wi-Fi and get the Wi-Fi IP address. Open the terminal program and type:

setprop service.adb.tcp.port 5555
stop adbd
start adbd

Now go to your computer (assuming that you are using Windows) and create a shortcut on the desktop for “cmd.exe” (without the quotations).

Right click on the cmd shortcut and choose "Run as Administrator"

Change to your android-sdk-windows\tools folder


adb connect ***wifi.ip.address***:5555

(example: adb connect

adb should now say that you are connected.

Note: if you are too fast to give the connect command it may fail. So try at least two times five seconds apart before you say this doesn’t work.

See also original question in stackoverflow

#64: What is the simplest and most robust way to get the user's current location on Android? (Score: 835)

Created: 2010-06-29 Last updated: 2018-01-12

Tags: android, geolocation, location, latitude-longitude

The LocationManager API on Android seems like it’s a bit of a pain to use for an application that only needs an occasional and rough approximation of the user’s location.

The app I’m working on isn’t really a location app per se, but it does need to get the user’s location in order to display a list of nearby businesses. It doesn’t need to worry about if the user is moving around or anything like that.

Here’s what I’d like to do:

  1. Show the user a list of nearby locations.
  2. Preload the user’s location so that by the time I need it in Activity X, it will be available.
  3. I don’t particularly care about accuracy or frequency of update. Just grabbing one location is sufficient as long as it’s not way off. Maybe if I want to be fancy I’ll update the location once every few mins or so, but it’s not a huge priority.
  4. Work for any device as long as it has either a GPS or a Network Location provider.

It seems like it shouldn’t be that hard, but it appears to me that I have to spin up two different location providers (GPS and NETWORK) and manage each’s lifecycle. Not only that, but I have to duplicate the same code in multiple activities to satisfy #2. I’ve tried using getBestProvider() in the past to cut the solution down to just using one location provider, but that seems to only give you the best “theoretical” provider rather than the provider that’s actually going to give you the best results.

Is there a simpler way to accomplish this?

#64 Best answer 1 of What is the simplest and most robust way to get the user's current location on Android? (Score: 958)

Created: 2010-06-30 Last updated: 2013-06-25

Here’s what I do:

  1. First of all I check what providers are enabled. Some may be disabled on the device, some may be disabled in application manifest.
  2. If any provider is available I start location listeners and timeout timer. It’s 20 seconds in my example, may not be enough for GPS so you can enlarge it.
  3. If I get update from location listener I use the provided value. I stop listeners and timer.
  4. If I don’t get any updates and timer elapses I have to use last known values.
  5. I grab last known values from available providers and choose the most recent of them.

Here’s how I use my class:

LocationResult locationResult = new LocationResult(){
    public void gotLocation(Location location){
        //Got the location!
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);

And here’s MyLocation class:

import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;
    public boolean getLocation(Context context, LocationResult result)
        //I use LocationResult callback class to pass location value from MyLocation to user code.
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}
        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    class GetLastLocation extends TimerTask {
        public void run() {
             Location net_loc=null, gps_loc=null;
             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);

Somebody may also want to modify my logic. For example if you get update from Network provider don’t stop listeners but continue waiting. GPS gives more accurate data so it’s worth waiting for it. If timer elapses and you’ve got update from Network but not from GPS then you can use value provided from Network.

One more approach is to use LocationClient But it requires Google Play Services apk to be installed on user device.

#64 Best answer 2 of What is the simplest and most robust way to get the user's current location on Android?(Score: 45)

Created: 2011-06-17 Last updated: 2013-08-27

After searching for best implementation how to get best precise user location I managed to combine all the best methods and come up with following class:

 * Retrieve accurate location from GPS or network services. 
 * Class usage example:
 * public void onCreate(Bundle savedInstanceState) {
 * 		...
 * 		my_location = new MyLocation();
 * 		my_location.init(main.this, locationResult);
 * }
 * public LocationResult locationResult = new LocationResult(){
 *	    @Override
 *	    public void gotLocation(final Location location){
 *	    	// do something
 *			location.getLongitude();
 *		   	location.getLatitude();
 *	    }
 *	};
class MyLocation{

     * If GPS is enabled. 
     * Use minimal connected satellites count.
    private static final int min_gps_sat_count = 5;

     * Iteration step time.
    private static final int iteration_timeout_step = 500;

    LocationResult locationResult;
    private Location bestLocation = null;
    private Handler handler = new Handler();
    private LocationManager myLocationManager; 
    public Context context;

    private boolean gps_enabled = false;

    private int counts 	  = 0;
    private int sat_count = 0;

    private Runnable showTime = new Runnable() {
         public void run() {
            boolean stop = false;
            System.println("counts=" + counts);
            //if timeout (1 min) exceeded, stop tying
            if(counts > 120){
                stop = true;
            //update last best location
            bestLocation = getLocation(context);
            //if location is not ready or don`t exists, try again
            if(bestLocation == null && gps_enabled){
                System.println("BestLocation not ready, continue to wait");
                handler.postDelayed(this, iteration_timeout_step);
                //if best location is known, calculate if we need to continue to look for better location
                //if gps is enabled and min satellites count has not been connected or min check count is smaller then 4 (2 sec)  
                if(stop == false && !needToStop()){
                    System.println("Connected " + sat_count + " sattelites. continue waiting..");
                    handler.postDelayed(this, iteration_timeout_step);
                    System.println("BestLocation finded return result to main. sat_count=" + sat_count);

                    // removing all updates and listeners
                    sat_count = 0;
                    // send best location to locationResult
     * Determine if continue to try to find best location
    private Boolean needToStop(){

                          return true;
          else if(counts <= 4){
                return false;
            if(sat_count < min_gps_sat_count){
                //if 20-25 sec and 3 satellites found then stop
                if(counts >= 40 && sat_count >= 3){
                    return true;
                return false;
        return true;

     * Best location abstract result class
    public static abstract class LocationResult{
         public abstract void gotLocation(Location location);

     * Initialize starting values and starting best location listeners
     * @param Context ctx
     * @param LocationResult result
    public void init(Context ctx, LocationResult result){
        context = ctx;
        locationResult = result;
        myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        gps_enabled = (Boolean) myLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        bestLocation = null;
        counts = 0;
        // turning on location updates
        myLocationManager.requestLocationUpdates("network", 0, 0, networkLocationListener);
        myLocationManager.requestLocationUpdates("gps", 0, 0, gpsLocationListener);
        // starting best location finder loop
        handler.postDelayed(showTime, iteration_timeout_step);

     * GpsStatus listener. OnChainged counts connected satellites count.
    public final GpsStatus.Listener gpsStatusListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
             if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS){
                try {
                    // Check number of satellites in list to determine fix state
                     GpsStatus status = myLocationManager.getGpsStatus(null);
                     Iterable<GpsSatellite>satellites = status.getSatellites();
                     sat_count = 0;
                     Iterator<GpsSatellite>satI = satellites.iterator();
                     while(satI.hasNext()) {
                         GpsSatellite satellite =;
                         System.println("Satellite: snr=" + satellite.getSnr() + ", elevation=" + satellite.getElevation());                         
                } catch (Exception e) {
                    sat_count = min_gps_sat_count + 1;
                 System.println("#### sat_count = " + sat_count);

     * Gps location listener.
    public final LocationListener gpsLocationListener = new LocationListener(){
         public void onLocationChanged(Location location){
         public void onProviderDisabled(String provider){}
         public void onProviderEnabled(String provider){}
         public void onStatusChanged(String provider, int status, Bundle extras){}

     * Network location listener.
    public final LocationListener networkLocationListener = new LocationListener(){
         public void onLocationChanged(Location location){
         public void onProviderDisabled(String provider){}
         public void onProviderEnabled(String provider){}
         public void onStatusChanged(String provider, int status, Bundle extras){}

     * Returns best location using LocationManager.getBestProvider()
     * @param context
     * @return Location|null
    public static Location getLocation(Context context){
        // fetch last known location and update it
        try {
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
             String strLocationProvider = lm.getBestProvider(criteria, true);
             System.println("strLocationProvider=" + strLocationProvider);
             Location location = lm.getLastKnownLocation(strLocationProvider);
             if(location != null){
                return location;
             return null;
        } catch (Exception e) {
            return null;

This class tries to connect to min_gps_sat_count satellites if GPS is enabled. Else returns LocationManager.getBestProvider() location. Check the code!

See also original question in stackoverflow

#65: How to pass an object from one activity to another on Android (Score: 829)

Created: 2010-04-29 Last updated: 2017-09-29

Tags: java, android, object, android-intent, android-activity

I am trying to work on sending an object of my customer class from one Activity and display it in another Activity.

The code for the customer class:

public class Customer {

    private String firstName, lastName, Address;
    int Age;

    public Customer(String fname, String lname, int age, String address) {

        firstName = fname;
        lastName = lname;
        Age = age;
        Address = address;

    public String printValues() {

        String data = null;

        data = "First Name :" + firstName + " Last Name :" + lastName
        + " Age : " + Age + " Address : " + Address;

        return data;

I want to send its object from one Activity to another and then display the data on the other Activity.

How can I achieve that?

#65 Best answer 1 of How to pass an object from one activity to another on Android (Score: 920)

Created: 2010-04-29 Last updated: 2018-05-26

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.


//To pass:
intent.putExtra("MyClass", obj);

// To retrieve object in second Activity

Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

class MainClass implements Serializable {
    public MainClass() {}

    public static class ChildClass implements Serializable {
        public ChildClass() {}

#65 Best answer 2 of How to pass an object from one activity to another on Android(Score: 325)

Created: 2011-10-19 Last updated: 2015-11-22

Implement your class with Serializable. Let’s suppose that this is your entity class:


@SuppressWarnings("serial") //With this annotation we are going to hide compiler warnings
public class Deneme implements Serializable {

    public Deneme(double id, String name) { = id; = name;

    public double getId() {
        return id;

    public void setId(double id) { = id;

    public String getName() {

    public void setName(String name) { = name;

    private double id;
    private String name;

We are sending the object called dene from X activity to Y activity. Somewhere in X activity;

Deneme dene = new Deneme(4,"Mustafa");
Intent i = new Intent(this, Y.class);
i.putExtra("sampleObject", dene);

In Y activity we are getting the object.

Intent i = getIntent();
Deneme dene = (Deneme)i.getSerializableExtra("sampleObject");

That’s it.

See also original question in stackoverflow

#66: Dilemma: when to use Fragments vs Activities: (Score: 826)

Created: 2013-11-30 Last updated: 2020-08-05

Tags: android, android-fragments, android-activity, architecture

I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.

Until not long ago, I developed an application as it said that they should be developed. I created an Activity to represent a screen of my application and used Fragments for ViewPager or Google Maps. I rarely created a ListFragment or other UI that can be reused several times.

Recently I stumbled on a project that contains only 2 Activities one is a SettingsActivity and other one is the MainActivity. The layout of the MainActivity is populated with many hidden full screen UI fragments and only one is shown. In the Activity logic there are many FragmentTransitions between the different screens of the application.

What I like about this approach is that because the application uses an ActionBar, it stays intact and does not move with the screen switching animation, which is what happens with Activity switching. This give a more fluent feel to those screen transitions.

So I guess what I’m asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question… Not really an opinion based one.

UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)

From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity. So basically you will have the same number of Activities in your application as the number of flows. That way the action bar stays intact on all the flow’s screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities as possible is not applicable for all situations because it creates a mess in what he calls the “God” activity.

#66 Best answer 1 of Dilemma: when to use Fragments vs Activities: (Score: 294)

Created: 2014-09-23 Last updated: 2017-02-26

Experts will tell you: “When I see the UI, I will know whether to use an Activity or a Fragment”. In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.

There is a good practice I found very helpful for me. It occurred to me while I was trying to explain something to my daughter.

Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.

But don’t forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.

Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.

Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did.

#66 Best answer 2 of Dilemma: when to use Fragments vs Activities:(Score: 144)

Created: 2013-11-30 Last updated: 2018-12-21

My philosophy is this:

Create an activity only if it’s absolutely absolutely required. With the back stack made available for committing bunch of fragment transactions, I try to create as few activities in my app as possible. Also, communicating between various fragments is much easier than sending data back and forth between activities.

Activity transitions are expensive, right? At least I believe so - since the old activity has to be destroyed/paused/stopped, pushed onto the stack, and then the new activity has to be created/started/resumed.

It’s just my philosophy since fragments were introduced.

See also original question in stackoverflow

#67: Unfortunately MyApp has stopped. How can I solve this? (Score: 825)

Created: 2014-04-28 Last updated: 2020-07-21

Tags: java, android, debugging, kotlin

I am developing an application, and everytime I run it, I get the message:

Unfortunately, MyApp has stopped.

What can I do to solve this?

About this question - obviously inspired by, there are lots of questions stating that their application has crashed, without any further detail. This question aims to instruct novice Android programmers on how to try and fix their problems themselves, or ask the right questions.

#67 Best answer 1 of Unfortunately MyApp has stopped. How can I solve this? (Score: 745)

Created: 2014-04-28 Last updated: 2018-03-12

This answer describes the process of retrieving the stack trace. Already have the stack trace? Read up on stack traces in “"

The Problem

Your application quit because an uncaught RuntimeException was thrown.
The most common of these is the NullPointerException.

How to solve it?#

Every time an Android application crashes (or any Java application for that matter), a Stack trace is written to the console (in this case, logcat). This stack trace contains vital information for solving your problem.

##Android Studio##

Finding the stack trace in Android Studio

In the bottom bar of the window, click on the Logcat button. Alternatively, you can press alt+6. Make sure your emulator or device is selected in the Devices panel. Next, try to find the stack trace, which is shown in red. There may be a lot of stuff logged into logcat, so you may need to scroll a bit. An easy way to find the stack trace is to clear the logcat (using the recycle bin on the right), and let the app crash again.

I have found the stack trace, now what?#

Yay! You’re halfway to solving your problem.
You only need to find out what exactly made your application crash, by analyzing the stack trace.

Read up on stack traces in “"

I still can’t solve my problem!

If you’ve found your Exception and the line where it occurred, and still cannot figure out how to fix it, don’t hesitate to ask a question on StackOverflow.

Try to be as concise as possible: post the stack trace, and the relevant code (e.g. a few lines up to the line which threw the Exception).

#67 Best answer 2 of Unfortunately MyApp has stopped. How can I solve this?(Score: 122)

Created: 2015-03-22 Last updated: 2019-05-05

You can use Google’s ADB tool to get Logcat file to analyze the issue.

adb logcat > logcat.txt

open logcat.txt file and search for your application name. There should be information on why it failed, the line number, Class name, etc.

See also original question in stackoverflow

#68: Making TextView scrollable on Android (Score: 823)

Created: 2009-11-17 Last updated: 2021-02-26

Tags: android, scroll, textview, android-scrollview, scrollable

I am displaying text in a TextView that appears to be too long to fit into one screen. I need to make my TextView scrollable. How can I do that?

Here is the code:

final TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
tv.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent e) {
        Random r = new Random();
        int i = r.nextInt(101);
        if (e.getAction() == e.ACTION_DOWN) {
        return true;

#68 Best answer 1 of Making TextView scrollable on Android (Score: 1837)

Created: 2010-07-15 Last updated: 2018-07-24

You don’t need to use a ScrollView actually.

Just set the

android:scrollbars = "vertical"

properties of your TextView in your layout’s xml file.

Then use:

yourTextView.setMovementMethod(new ScrollingMovementMethod());

in your code.

Bingo, it scrolls!

#68 Best answer 2 of Making TextView scrollable on Android(Score: 320)

Created: 2011-12-16 Last updated: 2017-04-28

This is how I did it purely in XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""




  1. android:fillViewport="true" combined with android:layout_weight="1.0" will make the textview take up all available space.

  2. When defining the Scrollview, DO NOT specify android:layout_height="fill_parent" otherwise the scrollview doesn’t work! (this has caused me to waste an hour just now! FFS).


To programmatically scroll to the bottom after appending text, use this:

mTextStatus = (TextView) findViewById(;
mScrollView = (ScrollView) findViewById(;

private void scrollToBottom()
{ Runnable()
        public void run()
            mScrollView.smoothScrollTo(0, mTextStatus.getBottom());

See also original question in stackoverflow

#69: How to call a method after a delay in Android (Score: 818)

Created: 2010-06-18 Last updated: 2014-09-04

Tags: java, android, handler, delay

I want to be able to call the following method after a specified delay. In objective c there was something like:

[self performSelector:@selector(DoSomething) withObject:nil afterDelay:5];

Is there an equivalent of this method in android with java? For example I need to be able to call a method after 5 seconds.

public void DoSomething()
     //do something here

#69 Best answer 1 of How to call a method after a delay in Android (Score: 1990)

Created: 2012-02-06 Last updated: 2020-09-08


	  //Do something after 100ms
    }, 100)


    final Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new Runnable() {
      public void run() {
	  	//Do something after 100ms
    }, 100);

#69 Best answer 2 of How to call a method after a delay in Android(Score: 338)

Created: 2012-06-23

I couldn’t use any of the other answers in my case. I used the native java Timer instead.

new Timer().schedule(new TimerTask() {			
    public void run() {
        // this code will be executed after 2 seconds		
}, 2000);

See also original question in stackoverflow

#70: android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() (Score: 818)

Created: 2016-07-05 Last updated: 2018-02-02

Tags: android, android-file, android-7.0-nougat

The app is crashing when I’m trying to open a file. It works below Android Nougat, but on Android Nougat it crashes. It only crashes when I try to open a file from the SD card, not from the system partition. Some permission problem?

Sample code:

File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
startActivity(intent); // Crashes on this line


android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()


When targeting Android Nougat, file:// URIs are not allowed anymore. We should use content:// URIs instead. However, my app needs to open files in root directories. Any ideas?

#70 Best answer 1 of android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() (Score: 1454)

Created: 2016-08-09 Last updated: 2020-11-28

If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps. We create our own class inheriting FileProvider in order to make sure our FileProvider doesn’t conflict with FileProviders declared in imported dependencies as described here.

Steps to replace file:// URI with content:// URI:

  • Add a FileProvider <provider> tag in AndroidManifest.xml under <application> tag. Specify a unique authority for the android:authorities attribute to avoid conflicts, imported dependencies might specify ${applicationId}.provider and other commonly used authorities.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
                android:resource="@xml/provider_paths" />
  • Then create a provider_paths.xml file in res/xml folder. A folder may be needed to be created if it doesn’t exist yet. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
    <external-path name="external_files" path="."/>
  • The final step is to change the line of code below in

     Uri photoURI = Uri.fromFile(createImageFile());


     Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());
  • Edit: If you’re using an intent to make the system open your file, you may need to add the following line of code:


Please refer to the full code and solution that have been explained here.

#70 Best answer 2 of android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()(Score: 328)

Created: 2016-11-18 Last updated: 2018-01-06

Besides the solution using the FileProvider, there is another way to work around this. Simply put

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();

in Application.onCreate(). In this way the VM ignores the file URI exposure.



enables the file exposure check, which is also the default behavior if we don’t setup a VmPolicy.

I encountered a problem that if I use a content:// URI to send something, some apps just can’t understand it. And downgrading the target SDK version is not allowed. In this case my solution is useful.


As mentioned in the comment, StrictMode is diagnostic tool, and is not supposed to be used for this problem. When I posted this answer a year ago, many apps can only receive File uris. They just crash when I tried to send a FileProvider uri to them. This is fixed in most apps now, so we should go with the FileProvider solution.

See also original question in stackoverflow

  1. This page use API to get the relevant data from stackoverflow community.
  2. Content license on this page is CC BY-SA 3.0.
  3. score = up votes - down votes.