2013年12月5日 星期四

Sort File List

File[] directoryList = currentFolder.listFiles(); 
if (directoryList != null) {
   List<File> directoryListing = new ArrayList<File>();
   directoryListing.addAll(Arrays.asList(directoryList));
   Collections.sort(directoryListing, new SortFileName());
   Collections.sort(directoryListing, new SortFolder());
}


//sorts based on the files name
public class SortFileName implements Comparator<File> {
    @Override
    public int compare(File f1, File f2) {
          return f1.getName().compareTo(f2.getName());
    }
}

//sorts based on a file or folder. folders will be listed first
public class SortFolder implements Comparator<File> {
    @Override
    public int compare(File f1, File f2) {
         if ((f1.isDirectory() && f2.isDirectory())
                || (!f1.isDirectory() && !f2.isDirectory()))
            return 0;
         else if (f1.isDirectory() && !f2.isDirectory())
            return -1;
         else
            return 1;
          }
}
public class SortFileDate implements Comparator<File> {
    @Override
    public int compare(File f1, File f2) {
     return Long.compare(f1.lastModified(), f2.lastModified());
    }
}

2013年12月4日 星期三

Go to Settings Pages

if (openbrain[i + 1].equals("0"))
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS));
else if (openbrain[i + 1].equals("1"))
startActivity(new Intent(
android.provider.Settings.ACTION_WIFI_SETTINGS));
else if (openbrain[i + 1].equals("2"))
startActivity(new Intent(
android.provider.Settings.ACTION_AIRPLANE_MODE_SETTINGS));
else if (openbrain[i + 1].equals("3"))
startActivity(new Intent(
android.provider.Settings.ACTION_APPLICATION_SETTINGS));
else if (openbrain[i + 1].equals("4"))
startActivity(new Intent(
android.provider.Settings.ACTION_BLUETOOTH_SETTINGS));
else if (openbrain[i + 1].equals("5"))
startActivity(new Intent(
android.provider.Settings.ACTION_DISPLAY_SETTINGS));
else if (openbrain[i + 1].equals("6"))
startActivity(new Intent(
android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS));
else if (openbrain[i + 1].equals("7"))
startActivity(new Intent(
android.provider.Settings.ACTION_SOUND_SETTINGS));
else if (openbrain[i + 1].equals("8")) {
Intent powerUsageIntent = new Intent(
Intent.ACTION_POWER_USAGE_SUMMARY);
ResolveInfo resolveInfo = getPackageManager()
.resolveActivity(powerUsageIntent, 0);
// check that the Battery app exists on this device
if (resolveInfo != null) {
startActivity(powerUsageIntent);
}

2013年12月2日 星期一

icon app list listview


Please check out my solution for getting the list of icon of application
In this getting the list of ResolveInfo of all application
This is the way to get the list of icon of application in android, hope this example will be help for you
    import java.util.List;


import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class IconListActivity extends ListActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadApps();  

    }



    private void loadApps() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        List<ResolveInfo> mApps = getPackageManager().queryIntentActivities(mainIntent, 0);

        ListView listView = getListView();
        listView.setAdapter(new AppsAdapter(this,mApps));
     }

    public class AppsAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private List<ResolveInfo> mApps;

        public AppsAdapter(Context context, List<ResolveInfo> mApps) {
            this.inflater = LayoutInflater.from(context);
            this.mApps = mApps;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHendler hendler;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.row_view, null);
                hendler = new ViewHendler();
                hendler.textLable = (TextView)convertView.findViewById(R.id.textViewLable);
                hendler.iconImage = (ImageView)convertView.findViewById(R.id.imageViewIcon);
                convertView.setTag(hendler);
            } else {
                hendler = (ViewHendler)convertView.getTag();
            }
            ResolveInfo info = this.mApps.get(position);
            hendler.iconImage.setImageDrawable(info.loadIcon(getPackageManager()));
            hendler.textLable.setText(info.loadLabel(getPackageManager()));

            return convertView;

        }
        class ViewHendler{
            TextView textLable;
            ImageView iconImage;
        }


        public final int getCount() {
            return mApps.size();
        }

        public final Object getItem(int position) {
            return mApps.get(position);
        }

        public final long getItemId(int position) {
            return position;
        }
    }

}
share|improve this answer

Custom Dialog (自訂dialog)

1 Android Layout Files

Two XML files, one for main screen, one for custom dialog.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />
 
</LinearLayout>
File : res/layout/custom.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
 
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />
 
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image"/>/>
 
     <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />
 
</RelativeLayout>

2. Activity

Read the comment and demo in next step, it should be self-explorary.
File : MainActivity.java
package com.mkyong.android;
 
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 final Context context = this;
 private Button button;
 
 public void onCreate(Bundle savedInstanceState) {
 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  button = (Button) findViewById(R.id.buttonShowCustomDialog);
 
  // add button listener
  button.setOnClickListener(new OnClickListener() {
 
    @Override
    public void onClick(View arg0) {
 
   // custom dialog
   final Dialog dialog = new Dialog(context);
   dialog.setContentView(R.layout.custom);
   dialog.setTitle("Title...");
 
   // set the custom dialog components - text, image and button
   TextView text = (TextView) dialog.findViewById(R.id.text);
   text.setText("Android custom dialog example!");
   ImageView image = (ImageView) dialog.findViewById(R.id.image);
   image.setImageResource(R.drawable.ic_launcher);
 
   Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
   // if button is clicked, close the custom dialog
   dialogButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     dialog.dismiss();
    }
   });
 
   dialog.show();
    }
  });
 }
}
Related Posts Plugin for WordPress, Blogger...