Thursday, February 21, 2019

Read poem app

Copy txt files in to raw folder, name them from t1 to t8.
File Activitymain.xml contain a textView and a ListView.

In class MainActivity create an String array contain name of poems.
Declare textView, listView, adapter.

In folder Layout create a file name li.xml for listView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="42sp"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/im"
        android:layout_width="40sp"
        android:layout_height="45sp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5sp"
        android:contentDescription="@null"
        android:src="@drawable/hoa" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5sp"
        android:layout_toEndOf="@+id/im"
        android:layout_toRightOf="@+id/im"
        android:textColor="#0000aa"
        android:textSize="18sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:contentDescription="@null"
        android:src="@drawable/muiten" />

</RelativeLayout>
Set adpater cho ListView.
adapter=new ArrayAdapter<String>(this,R.layout.li,R.id.text, ten);
Command in onItemClick of ListView.
int sodong = arg2;
                    
Intent in = new Intent(MainActivity.this, newclass.class);                                 
Bundle bun = new Bundle();              
bun.putInt("gi", sodong);               
bun.putString("gi2",ten[sodong]);                 
in.putExtras(bun);
startActivity(in);


Run to see first result like this.

Declare newclass in file AndroidManifest.
<activity
 android:name="com.bai.baidau.newclass"           
 android:screenOrientation="portrait">
 </activity>
Xml file of newclass like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageSwitcher
        android:id="@+id/ImageSwitcher01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:background="@drawable/nen" >
    </ImageSwitcher>
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:gravity="center_horizontal"
        android:textColor="#0000cd"
        android:textSize="18sp" />
    <ScrollView
        android:id="@+id/tz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"
        android:layout_marginTop="10dp" >
    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:lineSpacingExtra="2dp"
        android:textColor="#0f0f0f"
        android:textSize="18sp"
        android:textStyle="italic" />
    </ScrollView>
    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tz"
        android:layout_marginTop="10dp"
        android:gravity="center_horizontal"
        android:textColor="#800000"
        android:textSize="18sp" />

</RelativeLayout>
The  <ImageSwitcher make backgroind image fit with screen.
If use LinearLayout and set background image, it look not like original picture. You can try to see.
In class new, declare 3 textView tv, tv2, tv3.
Remove status bar and keep screen on.


Create a String array contain names of author.
String[] tacgia = { "Teika","Jakuren", "Narihita", "Komachi", "Saigyo", "Teika", "Shikibu", "Teika" };
Create an Object like this.
Object[]mang={R.raw.t1,R.raw.t2,R.raw.t3,R.raw.t4,R.raw.t5,R.raw.t6,R.raw.t7,R.raw.t8};
Object like array, but we can put in uit anything, here is address of poems.
Copy these lines to class new.
Intent in = getIntent();
Bundle bun = in.getExtras();       
int vitri = bun.getInt("gi");
String tenbai = bun.getString("gi2");
tv.setText(tenbai);
tv.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
tv3.setText(tacgia[vitri]);
We use InputStream to read txt file, set to textView tv2.
InputStream inp = this.getResources().openRawResource((Integer) mang[vitri]);
     try
    {
    byte[] buffer = new byte[inp.available()];
        while (inp.read(buffer) != -1);
        String jsontext = new String(buffer);

        tv2.setText(jsontext);       
      
    } 
    catch (IOException e)
    {
        //return null;
    }
See the line openRawResource((Integer) mang[vitri]); it access address of poem in Object mang.
Code look like this.

Run to see this result.

No comments:

Post a Comment