Pergunta

So I'm trying to make two dialogs (dialogInfo & dialogGuide) in one class. One is for showing info about the app, another one for showing the guide how to use it. I can access both of them by click an image button (imbtnInfo & imbtnGuide) respectively on the main class (startover)

Info dialog works fine. But I can't achieve to use textview in Guide dialog so it becomes my problem now. I want to show a text file from /res/raw/legal.txt on Guide dialog (I created the raw folder)

My text file contains html codes too.

Here are my codes:

startover.java (the main class)

    package com.fitria.MedicalRecord;


   import com.fitria.cobalogin.R;

   import android.content.Context;
   import android.widget.ImageButton;
   import android.app.Activity;
   import android.app.Dialog;
   import android.content.Intent;
   import android.os.Bundle;
   import android.view.View;
   import android.widget.Button;
   import android.widget.TextView;
   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import android.text.Html;
   import android.text.util.Linkify;

   public class startover extends Activity
   {
ImageButton imbtnInfo, imbtnGuide;
Button btnRecord, btnData, btnExit;
Dialog dialogInfo, dialogGuide;
TextView txt;

@Override
   public void onCreate(Bundle savedInstanceState) {         

   super.onCreate(savedInstanceState);    
   setContentView(R.layout.startover);
   dialogInfo = new Dialog(this); 
   dialogInfo.setContentView(R.layout.info);
   dialogInfo.setTitle("Info");
   dialogInfo.setCancelable(true);

   dialogGuide = new Dialog(Context); 
   dialogGuide.setContentView(R.layout.guide);
   dialogGuide.setTitle("Guide");
   dialogGuide.setCancelable(true);

   txt = (TextView)findViewById(R.id.textbox);
   imbtnInfo=(ImageButton)findViewById(R.id.imageInfo);
   imbtnGuide=(ImageButton)findViewById(R.id.imageGuide);
   btnRecord=(Button)findViewById(R.id.buttonRecord);
   btnData=(Button)findViewById(R.id.buttonData);
   btnExit=(Button)findViewById(R.id.buttonExit);



   btnRecord.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        /// Create Intent for SignUpActivity  and Start The Activity
        Intent intentSignUP=new    Intent(getApplicationContext(),signup.class);
        startActivity(intentSignUP);
        }
    });

   btnData.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentSignUP=new Intent(getApplicationContext(),signup.class);
            startActivity(intentSignUP);
            }
        });
   btnExit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentHome=new Intent(getApplicationContext(),Home.class);
            startActivity(intentHome);
            }
        });
   imbtnInfo.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           dialogInfo.show();
       }
   });
   imbtnGuide.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           txt.setText(readRawTextFile(R.raw.legal));
           dialogInfo.show();
       }
   });

}



}

info.xml (for dialogInfo)

<?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" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Mobile Medical Record "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="1.0.0"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Compatible with : Android 4.0.0 and up"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="fill"
            android:text="Mobile Medical Record is an application supported by a hardware that able to display and save data of your heart and respiratory rate."
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:text="Copyright © 2014"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:text="Fitria Handayani"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:text="All rights reserved."
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>
 </ScrollView>

</LinearLayout>

guide.xml (for dialogGuide) not the final layout but the final will look like this

<?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" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="In order to operate this application and device you must sign up and fill information about yourself; name and age."
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="fill"
            android:text="There are two function in Mobile Medical Record, they are 'Record' and 'Data'. Record enables you to communicate with the hardware supporting this application so you will get results of your heart rate and respiratory measurement. Data enables you to save last 10 measurement of your heart rate and respiratory rate."
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Record"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>
</ScrollView>

</LinearLayout>

Since I'm so new to Android app making (less than a week), please help me. Thanks in advance for anyone who tries to help me >.<

Edited : Java codes edited based on Sagar's suggestion :

package com.fitria.MedicalRecord;


import com.fitria.cobalogin.R;

import android.content.Context;
import android.widget.ImageButton;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.text.Html;
import android.text.util.Linkify;

public class startover extends Activity
{
ImageButton imbtnInfo, imbtnGuide;
Button btnRecord, btnData, btnExit;
Dialog dialogInfo, dialogGuide;
TextView mtxt1;


@Override
public void onCreate(Bundle savedInstanceState) {         

super.onCreate(savedInstanceState);



   setContentView(R.layout.startover);
   dialogInfo = new Dialog(this); 
   dialogInfo.setContentView(R.layout.info);
   dialogInfo.setTitle("Info");
   dialogInfo.setCancelable(true);

   dialogGuide = new Dialog(this); 
   dialogGuide.setContentView(R.layout.guide);
   dialogGuide.setTitle("Guide");
   dialogGuide.setCancelable(true);
   mtxt1 = (TextView)dialogGuide.findViewById(R.id.textbox);
   mtxt1.settext(ImportTextFile());

   imbtnInfo=(ImageButton)findViewById(R.id.imageInfo);
   imbtnGuide=(ImageButton)findViewById(R.id.imageGuide);
   btnRecord=(Button)findViewById(R.id.buttonRecord);
   btnData=(Button)findViewById(R.id.buttonData);
   btnExit=(Button)findViewById(R.id.buttonExit);





   btnRecord.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        /// Create Intent for SignUpActivity  and Start The Activity
        Intent intentSignUP=new         Intent(getApplicationContext(),signup.class);
        startActivity(intentSignUP);
        }
    });

   btnData.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentSignUP=new Intent(getApplicationContext(),signup.class);
            startActivity(intentSignUP);
            }
        });
   btnExit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentHome=new Intent(getApplicationContext(),Home.class);
            startActivity(intentHome);
            }
        });
   imbtnInfo.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           dialogInfo.show();
       }
   });
   imbtnGuide.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {

           dialogInfo.show();
       }
   });

}



String ImportTextFile()
{
       String myText = "";
       try 
       {
       InputStream fileStream = getResources().openRawResource(R.raw.legal);
       int fileLen = fileStream.available();
       // Read the entire resource into a local byte buffer.
       byte[] fileBuffer = new byte[fileLen];
       fileStream.read(fileBuffer);
       fileStream.close();
       displayText = new String(fileBuffer);
       }
       catch (IOException e) 
       {
         // exception handling
       }
       return myText;


 }   
}
Foi útil?

Solução

I can't achieve to use textview in Guide dialog so it becomes my problem now.

So here is your solution:

TextView mtxt1 = (TextView)dialogGuide.findViewById(R.id.textView1);
mtxt1.setText(ImportTextFile());

For reading Text from file:

String ImportTextFile()
{
    String myText = "";
    try {
    InputStream fileStream = getResources().openRawResource(
                        R.raw.legal);
    int fileLen = fileStream.available();
    // Read the entire resource into a local byte buffer.
    byte[] fileBuffer = new byte[fileLen];
    fileStream.read(fileBuffer);
    fileStream.close();
    displayText = new String(fileBuffer);
    } catch (IOException e) {
      // exception handling
    }
    return myText;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top