2016年4月30日 星期六

Read text from raw file

目的

從android raw中取出文字資料

方法

使用org.apache.commons.io中的IOUtils功能

** org.apache.commons.io.Charsets.UTF_8 已經 deprecated
** 可以改用 java.nio.charset.StandardCharsets.UTF_8 ,但至少要 API 19


java

1
2
3
InputStream is = cxt.getResources().openRawResource(R.raw.car_type_list);
String content = IOUtils.toString(is, Charsets.UTF_8);
IOUtils.closeQuietly(is); // don't forget to close your streams


gradle

1
2
3
dependencies {
    compile 'commons-io:commons-io:2.5'
}


reference:
http://stackoverflow.com/a/13566950

http://mvnrepository.com/artifact/commons-io/commons-io

2016年4月21日 星期四

使用android:Theme.Holo.Dialog設定最小寬度

目的

使用 DialogFragment 添加 AlertDialog,並自訂內容畫面設計,直接設定最小畫面寬度

方法

PhotoDialogFragment.java

 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
public class PhotoListDialogFragment extends DialogFragment {

    private String img;

    public PhotoListDialogFragment(String img) {
        this.img = img;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction

        View content = LayoutInflater.from(getContext()).inflate(R.layout.photo_list_dialog_content, null, false);
        TextView iv = (TextView) content.findViewById(R.id.photoListDialogIV);
        iv.setText(img);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.DialogStyle);
        builder.setView(content)
                .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                    }
                });
        // Create the AlertDialog object and return it

        return builder.create();
    }
}

style.xml

1
2
3
4
5
6
<resources>
    <style name="DialogStyle" parent="android:Theme.Holo.Dialog">
        <item name="android:windowMinWidthMajor">90%</item>
        <item name="android:windowMinWidthMinor">90%</item>
    </style>
</resources>




reference:
http://stackoverflow.com/a/28519059