I am trying to display all layout programmatically
actually i can do it in pure java but not in android
here is my first class:
public DiskFileExplorer(String path, Boolean subFolder) {
super();
this.initialpath = path;
this.recursivePath = subFolder;
}
public String[] list() {
return this.listDirectory(this.initialpath);
}
@SuppressWarnings("null")
private String [] listDirectory(String dir) {
String[] values = null;
File file = new File(dir);
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory() == true) {
//Log.v("Dossier" , files[i].getAbsolutePath());
this.dircount++;
}
else {
//Log.v("Fichier" ,files[i].getName());
values[i] = files[i].getName();
this.filecount++;
}
if (files[i].isDirectory() == true && this.recursivePath == true) {
this.listDirectory(files[i].getAbsolutePath());
}
}
}
return values;
}
and the second one:
public class MainActivity extends Activity {
public String []DiskFileExplorer(String path, Boolean subFolder){
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.mylist);
String pathToExplore = "./res/layout";
DiskFileExplorer d_expl = new DiskFileExplorer(pathToExplore,true);
String[] values = d_expl.list();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
}
}
and my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
and eveentually the pure java class that can do what i want to do in android:
import java.io.File;
/**
* Lister le contenu d'un répertoire
* @author fobec 2010
*/
public class DiskFileExplorer {
private String initialpath = " ";
private Boolean recursivePath = false;
public int filecount = 0;
public int dircount = 0;
/**
* Constructeur
* @param path chemin du répertoire
* @param subFolder analyse des sous dossiers
*/
public DiskFileExplorer(String path, Boolean subFolder) {
super();
this.initialpath = path;
this.recursivePath = subFolder;
}
public void list() {
this.listDirectory(this.initialpath);
}
private void listDirectory(String dir) {
File file = new File(dir);
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory() == true) {
System.out.println("Dossier" + files[i].getAbsolutePath());
this.dircount++;
} else {
System.out.println("" + files[i].getName());
this.filecount++;
}
if (files[i].isDirectory() == true && this.recursivePath == true) {
this.listDirectory(files[i].getAbsolutePath());
}
}
}
}
/**
* Exemple : lister les fichiers dans tous les sous-dossiers
* @param args
*/
public static void main(String[] args) {
String pathToExplore = "C:/Users/R_nkusi/workspace/Copy of AppList_bis/res/layout";
DiskFileExplorer diskFileExplorer = new DiskFileExplorer(pathToExplore, true);
Long start = System.currentTimeMillis();
diskFileExplorer.list();
System.out.println("----------");
System.out.println("Analyse de " + pathToExplore + " en " + (System.currentTimeMillis() - start) + " mses");
System.out.println(diskFileExplorer.dircount + " dossiers");
System.out.println(diskFileExplorer.filecount + " fichiers");
}
}
You have to change of course the”pathToExplore” and give one that exists in your own computer.
Thanks for the help.
You can read out your own XML layout files, but you cant find it in your filesystem, because it’s all compiled, thus not human readable.
Get all layout resource id’s and their actual names:
However, you need to parse the XML yourself. Here’s a small example:
I hope these pieces of code will help you get to where you want.