Home >> Miscellaneous
After working on various examples on ListView element from Android API,
I have tried to demonstrate a simple (at least I think so) file/folder
explorer user interface for showing list of files/folders those are
present under a folder as specified by the text box entry.
As depicted in the screen shown above, there is a EditText view, a button
and a ListView. These Android components are laid out using combination
of frame layout and linear layout. I have used a layout as shown below:
I have used FrameLayout->LinearLayout->EditText
->Button
->LinearLayout->ListView
More details of the layout is as shown below:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#449955">
<LinearLayout android:layout_gravity="top"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/edTxt"
android:layout_width="200dip"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List"/>
</LinearLayout>
<LinearLayout android:layout_gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:background="#334433"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
|
|
|  |
|
This is the main Activity for the main screen with the Edit box and
ListView component placed on the main FrameLayout instance.
OnClick listener is registered with the button instance, and is
over-ridden to provide onclick handler method. This method calls
helper class to bet list of folders and files present under the
edit box supplied text.
DirLister.java
package demo.dir.list;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class DirLister extends Activity {
private DirAdapter dirAdapter = null;
private ListView lstView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lstView = (ListView) findViewById(R.id.listView);
Button btn = (Button) findViewById(R.id.btn);
final EditText edTxt = (EditText) findViewById(R.id.edTxt);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
String[] listFill = DirListHelper.getLabelsList(edTxt
.getText().toString());
if (dirAdapter == null) {
dirAdapter = new DirAdapter();
}
lstView.setAdapter(dirAdapter);
dirAdapter.setFiles(listFill);
dirAdapter.notifyDataSetChanged();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT);
}
}
});
}
private class DirAdapter extends BaseAdapter {
private String[] files;
public void setFiles(String[] argFiles) {
this.files = argFiles;
}
@Override
public int getCount() {
if (files != null)
return files.length;
else
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView txtView = new TextView(getApplicationContext());
txtView.setText(files[position]);
return txtView;
}
}
}
|
Following code is that of a helper class that just helps in finding list
of files and folders for a given folder path as argument to it.
DirListHelper.java
package demo.dir.list;
import java.io.File;
import android.content.Context;
public class DirListHelper {
public DirListHelper(Context context) {
}
public static String[] getLabelsList(String path) throws Exception {
String[] filesList = null;
try {
File file = new File(path);
filesList = file.list();
} catch(Exception ex) {
throw ex;
}
return filesList;
}
}
|
If anything missed out , please let me know at
techienjoy at yahoo . com
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android List View
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
|
|
|
| Android Examples : |
List of ANDROid examples
with source code and output
screens captured and shown.
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens
captured and shown.
|
|
| Android Gallery Example : |
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
| Android Sensors Example : |
Example on Android Sensors Listed and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
| Google GWT Example : |
Example using GWT and some design patterns and various
ways of implementing this example.
|
|
| Android Tab View Example : |
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|