0

How can I use method speakOut from (Detail java) in (NoteEdit java) so I use it after menu_search instead of viewDetails(mDateText). This is Detail class

public class Detail extends Activity implements TextToSpeech.OnInitListener{
    private Button prsstospeak;
public String data;
    private int result=0;
      private TextToSpeech tts;
private TextView passedView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disp);
         tts = new TextToSpeech(this, this);



        prsstospeak.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View arg0) {
                  speakOut();
                }
            });
          }
    @Override
      public void onDestroy() {
      // Don't forget to shutdown!
      if (tts != null) {
        tts.stop();
        tts.shutdown();
       }
       super.onDestroy();
      }
      //It will called before TTS started
      @Override
      public void onInit(int status) {
      // TODO Auto-generated method stub
      //check status for TTS is initialized or not
      if (status == TextToSpeech.SUCCESS) {
      //if TTS initialized than set language
      result = tts.setLanguage(Locale.US);

      // tts.setPitch(5); // you can set pitch level
      // tts.setSpeechRate(2); //you can set speech speed rate

      //check language is supported or not
      //check language data is available or not
     if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
       Toast.makeText(this, "Missing data", Toast.LENGTH_LONG).show();
       //disable button
       prsstospeak.setEnabled(false);
      } else {

          prsstospeak.setEnabled(true);
       }
      } else {
          Log.e("TTS", "Initilization Failed");
         }
      }

      private void speakOut() {

      if(result!=tts.setLanguage(Locale.US))
      {
      Toast.makeText(getApplicationContext(), "Enter right Words...... ", Toast.LENGTH_LONG).show();
      }else
       {
        //speak given text
        tts.speak(data, TextToSpeech.QUEUE_FLUSH, null);
       }
      }
}

This is NoteEdie:

public class NoteEdit extends Activity {
    public static int numTitle = 1; 
    public static String curDate = "";
    public static String curText = "";  
    private EditText mTitleText;
    private EditText mBodyText;
    private TextView mDateText;
    private Long mRowId;

    private Cursor note;

    private NotesDbAdapter mDbHelper;

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

        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();        

        setContentView(R.layout.note_edit);

        setTitle(R.string.app_name);

        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        mDateText = (TextView) findViewById(R.id.notelist_date);

        long msTime = System.currentTimeMillis();  
        Date curDateTime = new Date(msTime);

        SimpleDateFormat formatter = new SimpleDateFormat("d'/'M'/'y");  
        curDate = formatter.format(curDateTime);        

        mDateText.setText(""+curDate);


        mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                    : null;
        }

        populateFields();

    }

      public static class LineEditText extends EditText{

            public LineEditText(Context context, AttributeSet attrs) {
                super(context, attrs);
                    mRect = new Rect();
                    mPaint = new Paint();
                    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                    mPaint.setColor(Color.BLUE);
            }

            private Rect mRect;
            private Paint mPaint;       

            @Override
            protected void onDraw(Canvas canvas) {

                int height = getHeight();
                int line_height = getLineHeight();

                int count = height / line_height;

                if (getLineCount() > count)
                    count = getLineCount();

                Rect r = mRect;
                Paint paint = mPaint;
                int baseline = getLineBounds(0, r);

                for (int i = 0; i < count; i++) {

                    canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
                    baseline += getLineHeight();

                super.onDraw(canvas);
            }

        }
      }

      @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            saveState();
            outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
        }

        @Override
        protected void onPause() {
            super.onPause();
            saveState();
        }

        @Override
        protected void onResume() {
            super.onResume();
            populateFields();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.noteedit_menu, menu);
            return true;        
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
            case R.id.menu_about:


                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.cancel();

                       }
                   });
                   dialog.show();              
                   return true;
            case R.id.menu_delete:
                if(note != null){
                    note.close();
                    note = null;
                }
                if(mRowId != null){
                    mDbHelper.deleteNote(mRowId);
                }
                finish();
                return true;
                //speech button
            case R.id.menu_search:
                viewDetails(mDateText);
                finish();           
                return true;


            case R.id.menu_save:
                saveState();
                finish();           
                return true;
           /* case android.R.id.home:
                finish();  */   
           default:
                return super.onOptionsItemSelected(item);
            }


        }




        private void saveState() {
            String title = mTitleText.getText().toString();
            String body = mBodyText.getText().toString();

            if(mRowId == null){
                long id = mDbHelper.createNote(title, body, curDate);
                if(id > 0){
                    mRowId = id;
                }else{
                    Log.e("saveState","failed to create note");
                }
            }else{
                if(!mDbHelper.updateNote(mRowId, title, body, curDate)){
                    Log.e("saveState","failed to update note");
                }
            }
        }


        private void populateFields() {
            if (mRowId != null) {
                note = mDbHelper.fetchNote(mRowId);
                startManagingCursor(note);
                mTitleText.setText(note.getString(
                        note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
                mBodyText.setText(note.getString(
                        note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
                curText = note.getString(
                        note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
            }

        }
public void viewDetails(View view){

String data= mDbHelper.getALLData();



}

}

How can I use method speakOut from (Detail java) in (NoteEdit java) so I use it after menu_search instead of viewDetails(mDateText).

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Sny
  • 13
  • 4

1 Answers1

0

Its not a good way to call a method in activity from another activity ! You have 2 solution :

  1. the easiest way (I think) : if you have a method (or methods) that you use alot and in different classes (such as activity or fragment), put them in a class and import this class to your activity and use this method.

  2. call the activity that contain this method with startActivityForResult and intent and return the result your activity. For more info about how is this way:

How to manage `startActivityForResult` on Android?

Community
  • 1
  • 1
YFeizi
  • 1,498
  • 3
  • 28
  • 50