title: how to open text file when you click it in listview
body: <p>I can't open text files when I click on it my programm go to the start page. I see many codes 
  where you shulod know the exact location of text files but how can I open text files when I don't know its exact location.</p>
  
  <p>Main activity open xml which contain 3 buttons when I press Browse button app open all files in phone</p>
  
  <pre><code>package com.example.fileexplorerb;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.List;
  
  import android.net.Uri;
  import android.os.Bundle;
  import android.app.Activity;
  import android.content.Intent;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.TextView;
  
  public class MainActivity extends Activity implements OnClickListener{
  
      private static final int REQUEST_PATH = 1;
  
      Button browseButton;
      Button exitButton;
      Button nextButton;
  
      String curFileName;
  
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          browseButton = (Button)findViewById(R.id.button1);
          browseButton.setOnClickListener(this);
  
          exitButton = (Button)findViewById(R.id.button2);
          exitButton.setOnClickListener(this);
  
          nextButton= (Button)findViewById(R.id.button3);
          nextButton.setOnClickListener(this);
  
  
  
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data){
          // See which child activity is calling us back.
          if (requestCode == REQUEST_PATH){
              if (resultCode == RESULT_OK) { 
                  curFileName = data.getStringExtra("GetFileName"); 
                  //edittext.setText(curFileName);
              }
           }
      }
  
      @Override
      public void onClick(View v) {
  
          if (v.getId() == R.id.button1){
              Intent intent1 = new Intent(this, FileChooser.class);
              startActivityForResult(intent1,REQUEST_PATH);
          }
          if (v.getId() == R.id.button2){
              finish();
          }
  
          if (v.getId() == R.id.button3){
              //Intent intent2 = new Intent(this, FileChooser.class);
              //startActivityForResult(intent2,REQUEST_PATH);
  
              Intent intent = new Intent(Intent.ACTION_VIEW);
              intent.setType("application/txt");
  
              startActivity(intent);
          }
      }
  
  
  }
  </code></pre>
  
  <p>File ArrayAdapter shows file size, date when it was created and name</p>
  
  <pre><code>package com.example.fileexplorerb;
  
  import java.util.List; 
  
  import android.content.Context; 
  import android.graphics.drawable.Drawable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup; 
  import android.widget.ArrayAdapter;
  import android.widget.ImageView; 
  import android.widget.TextView;
  
  
  public class FileArrayAdapter extends ArrayAdapter&lt;Item&gt;{
  
      private Context c;
      private int id;
      private List&lt;Item&gt;items;
  
      public FileArrayAdapter(Context context, int textViewResourceId,
              List&lt;Item&gt; objects) {
          super(context, textViewResourceId, objects);
          c = context;
          id = textViewResourceId;
          items = objects;
      }
      public Item getItem(int i)
       {
           return items.get(i);
       }
       @Override
         public View getView(int position, View convertView, ViewGroup parent) {
                 View v = convertView;
                 if (v == null) {
                     LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     v = vi.inflate(id, null);
                 }
  
                 /* create a new view of my layout and inflate it in the row */
              //convertView = ( RelativeLayout ) inflater.inflate( resource, null );
  
                 final Item o = items.get(position);
                 if (o != null) {
                         TextView t1 = (TextView) v.findViewById(R.id.textView1);
                         TextView t2 = (TextView) v.findViewById(R.id.textView2);
                         TextView t3 = (TextView) v.findViewById(R.id.textView3);
                         /* Take the ImageView from layout and set the city's image */
                          ImageView imageCity = (ImageView) v.findViewById(R.id.imageView1);
                          String uri = "drawable/" + o.getImage();
                          int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
                          Drawable image = c.getResources().getDrawable(imageResource);
                          imageCity.setImageDrawable(image);
  
                         if(t1!=null)
                          t1.setText(o.getName());
                         if(t2!=null)
                              t2.setText(o.getData());
                         if(t3!=null)
                              t3.setText(o.getDate());
  
                 }
                 return v;
         }
  
  }
  </code></pre>
  
  <p>FileChooser set the images to files and </p>
  
  <pre><code>package com.example.fileexplorerb;
  
  import java.io.File;
  import java.sql.Date;
  import java.util.ArrayList; 
  import java.util.Collections;
  import java.util.List;
  import java.text.DateFormat; 
  import android.os.Bundle; 
  import android.app.ListActivity;
  import android.content.Intent; 
  import android.view.View;
  import android.widget.ListView; 
  
  public class FileChooser extends ListActivity {
  
      private File currentDir;
      private FileArrayAdapter adapter;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState); 
          currentDir = new File("/sdcard/");
          fill(currentDir); 
      }
      private void fill(File f)
      {
          File[]dirs = f.listFiles(); 
           this.setTitle("Current Dir: "+f.getName());
           List&lt;Item&gt;dir = new ArrayList&lt;Item&gt;();
           List&lt;Item&gt;fls = new ArrayList&lt;Item&gt;();
           try{
               for(File ff: dirs)
               { 
                  Date lastModDate = new Date(ff.lastModified()); 
                  DateFormat formater = DateFormat.getDateTimeInstance();
                  String date_modify = formater.format(lastModDate);
                  if(ff.isDirectory()){
  
  
                      File[] fbuf = ff.listFiles(); 
                      int buf = 0;
                      if(fbuf != null){ 
                          buf = fbuf.length;
                      } 
                      else buf = 0; 
                      String num_item = String.valueOf(buf);
                      if(buf == 0) num_item = num_item + " item";
                      else num_item = num_item + " items";
  
                      //String formated = lastModDate.toString();
                      dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon")); 
                  }
                  else
                  {
  
                      fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
                  }
               }
           }catch(Exception e){    
  
           }
           Collections.sort(dir);
           Collections.sort(fls);
           dir.addAll(fls);
           if(!f.getName().equalsIgnoreCase("sdcard"))
               dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
           adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir);
           this.setListAdapter(adapter); 
      }
      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
          // TODO Auto-generated method stub
          super.onListItemClick(l, v, position, id);
          Item o = adapter.getItem(position);
          if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
                  currentDir = new File(o.getPath());
                  fill(currentDir);
          }
          else
          {
              onFileClick(o);
          }
      }
      private void onFileClick(Item o)
      {
          //Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show();
          Intent intent = new Intent();
          intent.putExtra("GetPath",currentDir.toString());
          intent.putExtra("GetFileName",o.getName());
          setResult(RESULT_OK, intent);
          finish();
      }
  
  
  
  }
  
  
  
  XML for main activity shows main menu when you open application 
  
  
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_marginBottom="142dp"
          android:text="Browse" /&gt;
  
      &lt;Button
          android:id="@+id/button2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button1"
          android:layout_alignTop="@+id/button1"
          android:layout_marginTop="63dp"
          android:text="Exit" /&gt;
  
      &lt;Button
          android:id="@+id/button3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/button2"
          android:layout_below="@+id/button2"
          android:layout_marginTop="14dp"
          android:text="Next" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>file_view XML  shows all file in phone</p>
  
  <pre><code>file view 
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="50dip"
          android:layout_height="50dip" /&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_above="@+id/textView2"
          android:layout_marginLeft="36dp"
          android:layout_toRightOf="@+id/imageView1"
          android:text="TextView" /&gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBottom="@+id/imageView1"
          android:layout_alignLeft="@+id/textView1"
          android:text="TextView" /&gt;
  
      &lt;TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/textView2"
          android:layout_alignBottom="@+id/textView2"
          android:layout_alignParentRight="true"
          android:layout_marginRight="19dp"
          android:text="TextView" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>