2013年9月24日 星期二

Android Progress Bar Example (part 1)

Android Progress Bar Example


Android spinning wheel progress bar
Android horizontal progress bar
Android horizontal progress bar file download complete

Android Manifest

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    package="com.as400samplecode"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
 <uses-permission android:name="android.permission.INTERNET" />
  
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Android String Resources - strings.xml

?
1
2
3
4
5
6
7
8
9
10
11
<resources>
    <string name="app_name">Progress Bar Tutorial</string>
    <string name="menu_settings">Settings</string>
    <string name="start">Start</string>
    <string name="stop">Stop</string>
    <string name="start_download">Start File Download</string>
    <string name="my_file">My Downloaded File</string>
    <string name="finished">Finished downloading&#8230;</string>
</resources>

Android Application main Layout - activity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent" android:layout_height="match_parent"
 <Button android:id="@+id/start" style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
  android:text="@string/start" />
 <Button android:id="@+id/stop" style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_alignParentTop="true" android:layout_toRightOf="@id/start"
  android:text="@string/stop" />
 <ProgressBar android:id="@+id/progressBar1"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_alignParentTop="true" android:layout_toRightOf="@+id/stop" />
 <Button android:id="@+id/download" style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content" android:layout_height="wrap_content"
  android:layout_alignParentLeft="true" android:layout_below="@+id/start"
  android:text="@string/start_download" />
 <ProgressBar android:id="@+id/progressBar2"
  style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:layout_alignParentLeft="true"
  android:layout_below="@id/download" android:padding="5dp" />
 <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_alignParentLeft="true"
  android:layout_below="@+id/progressBar2" android:contentDescription="@string/my_file" />
 <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_alignBottom="@+id/download"
  android:layout_toRightOf="@+id/download" android:text="@string/finished"
  android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

2013年9月6日 星期五

rename_folder()

private void rename_folder(String location,String oldname, String newname) {
// TODO Auto-generated method stub
File file = new File(getFilesDir() +location,oldname);
File file2 = new File(getFilesDir() +location,newname);
boolean success = file.renameTo(file2);

}

------------------------------------------

2013年9月5日 星期四

Sorting a list based on another list's values

List<String> MyAppNameList = new ArrayList<String>();
List<String> MyAppList = new ArrayList<String>();
List<String> MyNewAppList = new ArrayList<String>();

MyAppNameList.add("7");
MyAppNameList.add("6");
MyAppNameList.add("2");
MyAppNameList.add("1");
MyAppNameList.add("3");
MyAppNameList.add("4");
MyAppNameList.add("5");
MyAppNameList.add("8");

MyAppList.add("app1");
MyAppList.add("app2");
MyAppList.add("app3");
MyAppList.add("app4");
MyAppList.add("app5");
MyAppList.add("app6");
MyAppList.add("app7");
MyAppList.add("app8");


Map<String, String> m = new TreeMap<String, String>();
for (int i = 0; i < MyAppNameList.size(); ++i)
m.put(MyAppNameList.get(i), MyAppList.get(i));
MyAppList.clear();
MyAppList.addAll(m.values());


for (int i = 0; i < MyAppNameList.size(); i++) {
Toast.makeText(getApplicationContext(), "" + MyAppList.get(i),
Toast.LENGTH_SHORT).show();
}

2013年9月3日 星期二

監聽應用程序安裝卸載/ app install BroadcastReceiver

package com.example.testgetinstalledapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class refreshapp_br extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub

String action = arg1.getAction();

if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
//Log.d(TAG, "package has been removed");
String packageName = arg1.getData().getSchemeSpecificPart();
//Log.d(TAG, packageName);
Toast.makeText(context, "   "+packageName, Toast.LENGTH_SHORT).show();
} else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
//Log.d(TAG, "package has been added");
String packageName = arg1.getData().getSchemeSpecificPart();
Toast.makeText(context, "   "+packageName, Toast.LENGTH_SHORT).show();
}
}

}

----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testgetinstalledapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testgetinstalledapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <receiver android:name="com.example.testgetinstalledapp.refreshapp_br" >
            <intent-filter android:priority="999" >
                <action android:name="android.intent.action.PACKAGE_ADDED" />
                <action android:name="android.intent.action.PACKAGE_REMOVED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

2013年9月1日 星期日

write_in() & read_in() (folder in folder) (internal)

protected void write_in(String string, String string2, String string3) {
// TODO Auto-generated method stub
// This will get the SD Card directory and create a folder named MyFiles
// in it.
File sdCard = getFilesDir();
File directory = new File(sdCard.getAbsolutePath() + "/" + string);

// Now create the file in the above directory and write the contents
// into it
File file = new File(directory, string2);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(fOut);
try {
osw.write(string3);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
osw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

-------------------------------------------------------------------

private String read_in(String directory, String string) {
// TODO Auto-generated method stub
// Read in. st. from File"fi"
String fileName = string;
int readed;
String content = "";
byte[] buff = new byte[256]; // input stream buffer
// Input stream
try {
File file = new File(getFilesDir() + "/" + directory, string);
FileInputStream reader = new FileInputStream(file);
// FileInputStream reader =
// context.getApplicationContext().openFileInput(fileName);
while ((readed = reader.read(buff)) != -1) {
content += new String(buff).trim();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// End of Read in. st. from File"fi"
return content;
}

Internal Storage Create Folder (Folder in Folder)

File myDir = new File(getFilesDir(), "folder");
myDir.mkdir();


File myDir2 = new File(getFilesDir()+"/folder", "folder2");
myDir2.mkdir();

Related Posts Plugin for WordPress, Blogger...