Adding a Icon to notification area in android app - java

O.K i have looked around this site and others and i see something similar to what i am wanting but yet it isn't what i am wanting...let me explain
i am learning to build an android app using eclipse that has a media player linked to my shoutcast server i am running, so far the apps works great but it doesn't have an icon on the top were the notification area is and when i go to a different app (a game) to play while leaving the music playing it minimizes the app .. thats fine but when i reopen the app it doesnt stop the mediaplayer infact if i play the player i will have 2 players running i will then have to force stop it using the task manager,, i know this can be done as i have apps on my phone that has icons and it will do what i am wanting
when i searched this site i found a link to youtube that has soemthing similar to what i am wanting
http://www.youtube.com/watch?v=e74z0_Z5QWI&feature=relmfu the code that is shown is as followed
import android.app.Activity;
public class StatusBar extends Activity implements OnclickListener{
NotificationManager nm;
#Override
Protected void onCreate(bundle savedInstance){
//TODO Auto-generated method Stub
super.oncreate(savedInstaceState);
setContentView(R.layout.statusbar);
Button stat = (Button)findViewById(R.id.bStatusBar);
stat.setOnClickListener(this);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public void onClick(View v){
//TODO Auto-generated method Stub
Intent intent = new Intent(this, StatusBar.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
}
i have add this code to my MainActivity.java but it spits several errors to me that i have tried to fix using quickfix here is the script i currently have in my MainActivity.java file
package com.myapp.mytapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MainActivity extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{
private Button btn_play,
btn_pause,
btn_stop;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private int lengthOfAudio;
private final String URL = "http://link to url:8002";
private final Handler handler = new Handler();
private final Runnable r = new Runnable() {
#Override
public void run() {
updateSeekProgress();
}
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btn_play = (Button)findViewById(R.id.btn_play);
btn_play.setOnClickListener(this);
btn_pause = (Button)findViewById(R.id.btn_pause);
btn_pause.setOnClickListener(this);
btn_pause.setEnabled(true);
btn_stop = (Button)findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(this);
btn_stop.setEnabled(true);
seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnTouchListener(this);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
}
#Override
public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
seekBar.setSecondaryProgress(percent);
}
#Override
public void onCompletion(MediaPlayer mp) {
btn_play.setEnabled(true);
btn_pause.setEnabled(false);
btn_stop.setEnabled(false);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (mediaPlayer.isPlaying()) {
SeekBar tmpSeekBar = (SeekBar)v;
mediaPlayer.seekTo((lengthOfAudio / 100) * tmpSeekBar.getProgress() );
}
return false;
}
#Override
public void onClick(View view) {
try {
mediaPlayer.setDataSource(URL);
mediaPlayer.prepare();
lengthOfAudio = mediaPlayer.getDuration();
} catch (Exception e) {
//Log.e("Error", e.getMessage());
}
switch (view.getId()) {
case R.id.btn_play:
playAudio();
break;
case R.id.btn_pause:
pauseAudio();
break;
case R.id.btn_stop:
stopAudio();
break;
default:
break;
}
updateSeekProgress();
}
private void updateSeekProgress() {
if (mediaPlayer.isPlaying()) {
seekBar.setProgress((int)(((float)mediaPlayer.getCurrentPosition() / lengthOfAudio) * 100));
handler.postDelayed(r, 1000);
}
}
private void stopAudio() {
mediaPlayer.stop();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
seekBar.setProgress(0);
}
private void pauseAudio() {
mediaPlayer.pause();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
}
private void playAudio() {
mediaPlayer.start();
btn_play.setEnabled(true);
btn_pause.setEnabled(true);
btn_stop.setEnabled(true);
}
public void homepage (View view) {
goToUrl ( "http://myhomepage.com/");
}
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
}
here is what is in my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black"
android:icon="#drawable/ic_launcher"
android:orientation="vertical"
startForeground()>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/homepage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:cursorVisible="true"
android:linksClickable="true"
android:onClick="homepage"
android:text="Home Page" />
</TableRow>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_play"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#drawable/play"
android:clickable="true"
android:cursorVisible="true"
android:src="#drawable/play" >
</Button>
<Button
android:id="#+id/btn_pause"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#drawable/pause"
android:clickable="true"
android:cursorVisible="true"
android:src="#drawable/pause" >
</Button>
<Button
android:id="#+id/btn_stop"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="#drawable/stop"
android:clickable="true"
android:cursorVisible="true"
android:src="#drawable/stop" >
</Button>
</LinearLayout>
<SeekBar
android:id="#+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp" >
</SeekBar>
<ImageView
android:id="#+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/scast" />
<TextView
android:id="#+id/Textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="#string/version" >
</TextView>
</LinearLayout>
can someone tell me what i did wrong or can rewrite what i have and still keep everything working
Thanks in advance
Rob

Related

Why the status of text to speech is error rather than successful implementation?

I am trying to make a simple Text to Speech application but it is throwing an error and thus, as a result, the toast is appearing when the button is clicked instead of getting TextToSpeech.SUCCESS as the int status.
Here is the code I used:
package com.example.notifyme;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
EditText txt;
Button b;
String s;
TextToSpeech tts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.input);
b = findViewById(R.id.button);
}
public void notify(View view) {//for creating tts activity
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
s = txt.getText().toString();
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.ENGLISH);
tts.setSpeechRate(1.0f);
tts.speak(s, TextToSpeech.QUEUE_FLUSH, null, null);
Toast.makeText(getApplicationContext(),"Here it is",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Something unexpected happened", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onPause();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/input"
android:layout_width="330dp"
android:layout_height="72dp"
android:layout_marginTop="156dp"
android:ems="10"
android:hint="#string/add_your_text_here"
android:gravity="start|top"
android:layout_marginBottom="50dp"
android:inputType="textMultiLine"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="114dp"
android:layout_height="55dp"
android:backgroundTint="#4CAF50"
android:text="#string/speak"
android:textAllCaps="true"
android:onClick="notify"
android:textFontWeight="40"
app:layout_constraintTop_toBottomOf="#+id/input"
android:layout_marginTop="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="266dp" />
</androidx.constraintlayout.widget.ConstraintLayout/>
Kindly let me know what is the mistake.

What should i do to move to another activity when only special conditions are met

I am making a quiz app. I want the exit page to be displayed when the next button is pressed after last question has been answered. I have a activity_questions.xml and a activity_exit.xml. The xml for activity_questions.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.crystal.questions">
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-thin"
android:padding="10dp"
android:text=""
android:textColor="#440027"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text3"
android:layout_margin="30dp"
android:background="#9775AA"
android:hint="Answer"
android:inputType="text"
android:padding="10dp"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edit2"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/answer"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onAnswerClick"
android:padding="2dp"
android:text="Okay" />
<Button
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:onClick="onHintClick"
android:padding="2dp"
android:text="Hint" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dp"
android:orientation="vertical"
android:layout_marginTop="26dp"
android:layout_below="#+id/ll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/tickcross"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/ll"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/wierdtick" />
<TextView
android:id="#+id/correctornot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tickcross"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Correct!"
android:textColor="#440027"
android:textSize="30dp" />
<Button
android:id="#+id/nextbutton"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next"
android:onClick="onNextClick"
/>
</LinearLayout>
</RelativeLayout>
And the java is:
package com.example.android.crystal;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class questions extends AppCompatActivity {
Random r = new Random();
private boolean done;
private int QuestionNo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
String[] questions = getResources().getStringArray(R.array.Questions);
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
}
public void onFinishClick(View view){
Intent intent = new Intent(this, exit.class);
startActivity(intent);
}
public static void hideKeyboardFrom(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
public void answersubmitted() {
findViewById(R.id.tickcross).setVisibility(View.VISIBLE);
TranslateAnimation animation = new TranslateAnimation(0, 0, 2000, 0);
animation.setDuration(1000);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
findViewById(R.id.tickcross).startAnimation(animation);
findViewById(R.id.correctornot).setVisibility(View.VISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.VISIBLE);
}
public void onHintClick(View view) {
String[] hints = getResources().getStringArray(R.array.Hints);
Toast toasty = Toast.makeText(getApplicationContext(), hints[QuestionNo], Toast.LENGTH_SHORT);
toasty.show();
}
public void onAnswerClick(View view) {
if (done == false) {
hideKeyboardFrom(this);
String answer = ((EditText) findViewById(R.id.edit2)).getText().toString();
String[] answers = getResources().getStringArray(R.array.Answers);
String correctAnswer = answers[QuestionNo];
correctAnswer = correctAnswer.toUpperCase();
answer = answer.toUpperCase();
if (answer.equals(correctAnswer)) {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("CORRECT!");
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.wierdtick));
answersubmitted();
} else {
TextView t = (TextView) findViewById(R.id.correctornot);
t.setText("Correct Answer: " + correctAnswer);
ImageView i = (ImageView) findViewById(R.id.tickcross);
i.setImageDrawable(getDrawable(R.drawable.weirdcross));
answersubmitted();
}
done = true;
}
}
public void onNextClick(View view){
if (done){
String[] questions = getResources().getStringArray(R.array.Questions);
if (QuestionNo < (questions.length - 1)) {
QuestionNo += 1;
TextView t = (TextView) findViewById(R.id.text3);
t.setText(questions[QuestionNo]);
findViewById(R.id.tickcross).setVisibility(View.INVISIBLE);
findViewById(R.id.correctornot).setVisibility(View.INVISIBLE);
findViewById(R.id.nextbutton).setVisibility(View.INVISIBLE);
EditText et = (EditText) findViewById(R.id.edit2);
et.setText("");
done = false;
}
else {
onFinishClick();
}
}
}
}
But the onFinishClick() gives an error
Thanks in advance for your help
What should i do to move to another activity when only special conditions are met
Basically, an if-statement and an Intent.
Raw code with no variables added:
if(condition(s)){
Intent i = new Intent(this, TargetActivity.class);
//Put arguments into the intent if you need them...
startActivity(i);
}
You can put it in a thread, in an onClickListener, anywhere and any way you want to execute the code.
As to the error you are receiving, I cannot help you with that until you supply the stacktrace from Logcat
You can do like this:
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mQuestionCount < questionList.size()){
// set next question
} else {
// call finish
}
}
});

App seems like launching more than once in landscape mode after splash screen?

I have a splash screen and after that my main activity starts. This works fine in portrait mode but if in case i tilt my phone in landscape mode, the main activity can be seen launching more than once after splash screen.
I tried using android:launchMode="singleInstance" but in that case i am not able to attach files in feedback alert-box.
Following is my code:
MainActivity.java
package com.example.android.tel;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
Toolbar mActionBarToolbar;
TextView toolbar_title_mainActivity, main_textView, disclaimer_txtView;
CardView SearchDept, SearchName, disclaimer, feedback;
ImageView back;
ArrayList<Uri> arrayUri = new ArrayList<Uri>();
ArrayAdapter<Uri> myFileListAdapter;
ListView listViewFiles;
Dialog alertDialog;
final int RQS_LOADIMAGE = 0;
final int RQS_SENDEMAIL = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_main);
} else {
setContentView(R.layout.activity_main);
}
mActionBarToolbar = (Toolbar) findViewById(R.id.tool_bar_main_activity);
toolbar_title_mainActivity = (TextView) findViewById(R.id.toolbar_title);
main_textView = (TextView) findViewById(R.id.main_textView);
main_textView.setPaintFlags(main_textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar_title_mainActivity.setText("Hry. Govt. Telephone Directory");
back = (ImageView) findViewById(R.id.back);
back.setVisibility(View.INVISIBLE);
disclaimer = (CardView) findViewById(R.id.disclaimer);
feedback = (CardView) findViewById(R.id.feedback);
SearchDept = (CardView) findViewById(R.id.cardView1_mainActivity);
SearchName = (CardView) findViewById(R.id.cardView2_mainActivity);
SearchDept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CardViewActivity.class);
startActivity(i);
}
});
SearchName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent j = new Intent(MainActivity.this, ByNameListActivity.class);
startActivity(j);
}
});
disclaimer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog alertDialog = new Dialog(MainActivity.this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.disclaimer);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
alertDialog.show();
}
});
feedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog = new Dialog(MainActivity.this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.feedback);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
alertDialog.setCanceledOnTouchOutside(true);
ImageView send_btn=(ImageView)alertDialog.findViewById(R.id.send);
ImageView attach_btn=(ImageView)alertDialog.findViewById(R.id.attachment);
final TextView to_email_add=(TextView)alertDialog.findViewById(R.id.email_address);
to_email_add.setText("tel#gmail.com");
final EditText email_subject=(EditText)alertDialog.findViewById(R.id.email_subject);
final EditText email_text=(EditText)alertDialog.findViewById(R.id.email_text);
final EditText mobile_no=(EditText)alertDialog.findViewById(R.id.mobile_text);
email_subject.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
email_text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
myFileListAdapter = new ArrayAdapter<Uri>(
MainActivity.this,
android.R.layout.simple_list_item_1,
arrayUri);
listViewFiles = (ListView)alertDialog.findViewById(R.id.filelist);
listViewFiles.setAdapter(myFileListAdapter);
listViewFiles.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
myFileListAdapter.remove(arrayUri.get(position));
myFileListAdapter.notifyDataSetChanged();
Toast.makeText(view.getContext(), "You unattached one item", Toast.LENGTH_LONG).show();
return false;
}
});
attach_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RQS_LOADIMAGE);
}
});
send_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email_add=to_email_add.getText().toString();
String email_sub=email_subject.getText().toString();
String email_txt=email_text.getText().toString();
String emailAddressList[] = {email_add};
String mobileNo=mobile_no.getText().toString();
String info=email_txt+"\n\nPhone Number :"+mobileNo;
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_EMAIL, emailAddressList);
intent.putExtra(Intent.EXTRA_SUBJECT, email_sub);
intent.putExtra(Intent.EXTRA_TEXT,info);
if(arrayUri.isEmpty()&& isValidPhone(mobileNo)&& !(mobileNo.isEmpty())){
//send email without photo attached
intent.setAction(Intent.ACTION_SEND);
intent.setType("plain/text");
new Handler().postDelayed(new Runnable() {
public void run() {
alertDialog.dismiss();
}
}, 5000);
}else if(arrayUri.size() == 1 && isValidPhone(mobileNo)&& !(mobileNo.isEmpty())){
//send email with ONE photo attached
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, arrayUri.get(0));
intent.setType("image/*");
new Handler().postDelayed(new Runnable() {
public void run() {
alertDialog.dismiss();
}
}, 5000);
}else if(arrayUri.size()>1&& isValidPhone(mobileNo)&& !(mobileNo.isEmpty())){
//send email with MULTI photo attached
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayUri);
intent.setType("image/*");
new Handler().postDelayed(new Runnable() {
public void run() {
alertDialog.dismiss();
}
}, 5000);
}
else {
Toast.makeText(v.getContext(), "Phone number is not valid", Toast.LENGTH_LONG).show();
}
startActivity(Intent.createChooser(intent, "Please provide valid details"));
}
});
alertDialog.show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
switch(requestCode){
case RQS_LOADIMAGE:
Uri imageUri = data.getData();
arrayUri.add(imageUri);
myFileListAdapter.notifyDataSetChanged();
break;
case RQS_SENDEMAIL:
break;
}
}
}
public static boolean isValidPhone(String phone)
{
String expression = "^([0-9\\+]|\\(\\d{1,3}\\))[0-9\\-\\. ]{3,15}$";
CharSequence inputString = phone;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputString);
if (matcher.matches())
{
return true;
}
else{
return false;
}
}
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation;
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
// Add code if needed
// listViewFiles.setAdapter(myFileListAdapter);
// myFileListAdapter.notifyDataSetChanged();
setRequestedOrientation(orientation);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weightSum="14"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
tools:context="com.example.android.tel.MainActivity">
<include
android:id="#+id/tool_bar_main_activity"
layout="#layout/toolbar">
</include>
<TextView
android:id="#+id/main_textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_marginTop="10dp"
android:text="How would you like to search?"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#1A237E"
android:gravity="center_horizontal"/>
<RelativeLayout
android:id="#+id/searchby_btns"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="4"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:gravity="center_vertical">
<android.support.v7.widget.CardView
android:id="#+id/cardView1_mainActivity"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp"
android:layout_marginTop="10dp"
card_view:cardBackgroundColor="#e97c1d">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search By Department.."
android:textColor="#android:color/white"
android:textStyle="bold"
android:textSize="18sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2_mainActivity"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_below="#id/cardView1_mainActivity"
card_view:cardCornerRadius="4dp"
android:layout_marginTop="20dp"
card_view:cardBackgroundColor="#e97c1d">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search By Name.."
android:textColor="#android:color/white"
android:textStyle="bold"
android:textSize="18sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="8.5">
<ImageView
android:id="#+id/map_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/hry_map"
android:elevation="4dp"
android:layout_gravity="center"/>
</RelativeLayout>
<RelativeLayout
android:layout_below="#id/map_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:layout_marginTop="2dp"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true">
<android.support.v7.widget.CardView
android:id="#+id/disclaimer"
android:layout_width="150dp"
android:layout_height="30dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
android:layout_marginRight="8dp"
card_view:cardBackgroundColor="#424242">
<TextView
android:layout_width="150dp"
android:layout_height="30dp"
android:text="Disclaimer"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/feedback"
android:layout_toRightOf="#id/disclaimer"
android:layout_width="150dp"
android:layout_height="30dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardBackgroundColor="#424242">
<TextView
android:layout_width="150dp"
android:layout_height="30dp"
android:text="Feedback"
android:gravity="center"
android:layout_gravity="center_vertical"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
SplashScreenActivity.java
package com.example.android.telephonedirectory;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import com.felipecsl.gifimageview.library.GifImageView;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
public class SplashScreenActivity extends AppCompatActivity {
// private GifImageView gifimageview;
private ProgressBar progressBarSplashScreen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_splash_screen);
} else {
setContentView(R.layout.activity_splash_screen);
}
// gifimageview=(GifImageView)findViewById(R.id.gifSplashscreenImage);
progressBarSplashScreen=(ProgressBar)findViewById(R.id.progressbarSplashscreen);
progressBarSplashScreen.setVisibility(progressBarSplashScreen.VISIBLE);
//set GifImageView Resource
/*try {
InputStream inputStream=getAssets().open("splash_Screen.png");
byte[] bytes= IOUtils.toByteArray(inputStream);
gifimageview.setBytes(bytes);
gifimageview.startAnimation();
}catch (IOException ex){
}*/
//Wait for 4 seconds and start activity main
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
SplashScreenActivity.this.startActivity(new Intent(SplashScreenActivity.this,MainActivity.class));
SplashScreenActivity.this.finish();
}
},2000);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation;
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
// Add code if needed
// listViewFiles.setAdapter(myFileListAdapter);
// myFileListAdapter.notifyDataSetChanged();
setRequestedOrientation(orientation);
}
}
Dont use android:launchMode="singleInstance"
Launch mode you should use "singleTask" for this .
Because singleInstance creates separate task stack for Activity and do not check activity in current Task Stack.
while "singleTask" check each time if an Activity exist in Task Stack it can not create new one.

listView won't populate from bottom

I'm making a chat activity for a school project and I want the newest messages at the bottom of the listView. Adding the code below does not work. I simply want the newest message to appear at the bottom and make the listView automatically scroll to the last list item. Thanks in advance!
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true"
Link to image of current situation
XML:
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1a1a1a"
android:id="#+id/relativeLayout_Messages"
android:layout_below="#id/my_toolbar"
android:layout_alignBottom="#+id/imageView_Separator">
<ListView
android:id="#+id/listView_Messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stackFromBottom="true"
/>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView_Separator"
android:background="#1a1a1a"
android:src="#drawable/seperator"
android:layout_above="#+id/relativeLayout_ChatInput"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/relativeLayout_ChatInput"
android:background="#1a1a1a">
<EditText
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="#+id/editText_ChatInput"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="5dp"
android:background="#drawable/search_field"
android:layout_toStartOf="#+id/imageButton_Send"
android:layout_alignBottom="#+id/imageButton_Send"
android:inputType="textPersonName"
android:hint="#string/chat_field_hint"
android:textColor="#f4f5f6"
android:textColorHint="#f4f5f6"/>
<ImageButton
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imageButton_Send"
android:background="#android:color/transparent"
android:src="#drawable/send_button"
android:scaleType="fitCenter"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="6dp"
android:layout_marginBottom="8dp"
android:layout_alignParentEnd="true"
android:onClick="sendButtonClicked"
/>
</RelativeLayout>
</RelativeLayout>
JAVA
package com.example.muhryn.resonatem;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import java.util.*;
public class ChatActivity extends AppCompatActivity{
public void hideKeyboard() {
try {
InputMethodManager imm=(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
} catch (Exception ex) { }
}
private ListView chatListView;
private Button submitButton;
private EditText chatEditText;
private Chat chat;
private ArrayList receivedList=new ArrayList();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatListView=(ListView)findViewById(R.id.listView_Messages);
chatEditText=(EditText)findViewById(R.id.editText_ChatInput);
chat=new Chat("A","Resonate");
chat.setListener(new Chat.Listener(){
public void received(final String received) {
System.out.println("Received: "+received);
runOnUiThread(new Runnable() {
public void run() {
hideKeyboard();
receivedList.add(0, received);
chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList));
}
});
//////((ArrayAdapter)chatListView.getAdapter()).insert(received,0);
}
});
}
public void sendButtonClicked(View view){
String textToSubmit=chatEditText.getText().toString().trim();
if(textToSubmit.isEmpty())
return;
if(chat.submitted(textToSubmit)) {
receivedList.add(0,"Sent: "+textToSubmit);
chatListView.setAdapter(new ChatList(ChatActivity.this, receivedList));
} else
Toast.makeText(this,"Failed to submit "+textToSubmit+".",Toast.LENGTH_SHORT).show();
}
#Override
public void onStop () {
super.onStop();
chat.stop();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.chat_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.view_profile:
finish();
return true;
case R.id.report_match:
finish();
return true;
case R.id.add_match:
finish();
return true;
case R.id.unmatch:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Try the below code.
chatListView.smoothScrollByOffset(receivedList.size()-1);
It will scroll down to latest chat.

Set multiple dialogs in one class and set a textview on one of dialogs

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;
}
}
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;
}

Categories

Resources