How do you turn a set string into speech in android studio? - java

I'm trying to find out how to set a string that will be read out-loud.
For example:
String text = "Hello";

You can find a very detail example that show how to convert text to speech on this site. HTH.
Your MainActivity.java should look something like this
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
public class MainActivity extends Activity {
TextToSpeech t1;
EditText ed1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
b1=(Button)findViewById(R.id.button);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}
And here is the content of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:transitionGroup="true">
<TextView android:text="Text to Speech" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point"
android:id="#+id/textView"
android:layout_below="#+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:src="#drawable/abc"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:theme="#style/Base.TextAppearance.AppCompat" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_below="#+id/imageView"
android:layout_marginTop="46dp"
android:hint="Enter Text"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ff7aff10"
android:textColorHint="#ffff23d1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text to Speech"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />
</RelativeLayout>

Related

Why I can't run the two activities together? the two activities and the image shows that I reached MainActivity directly without clicking homebutton

knowing that there is no errors, only MainActivity shows without homebutton................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
MainActivity.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etfirstvalue,etsecondvalue;
Button btnadd,btnsubs,btnmultiply,btndivide;
Double num1,num2;
TextView tvresult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etfirstvalue=findViewById(R.id.etfirstvalue);
etsecondvalue=findViewById(R.id.etsecondvalue);
btnadd=findViewById(R.id.btnadd);
btndivide=findViewById(R.id.btndivision);
btnmultiply=findViewById(R.id.btnmultiply);
btnsubs=findViewById(R.id.btnsubs);
tvresult=findViewById(R.id.tvresult);
Clicklistener();
}
public void Clicklistener(){
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1+num2;
tvresult.setText(String.valueOf(result));
}
});
btnsubs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1-num2;
tvresult.setText(String.valueOf(result));
}
});
btnmultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1*num2;
tvresult.setText(String.valueOf(result));
}
});
btndivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num1=Double.parseDouble(etfirstvalue.getText().toString());
num2=Double.parseDouble(etsecondvalue.getText().toString());
Double result=num1/num2;
tvresult.setText(String.valueOf(result));
}
});
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#FFFFFF"
android:backgroundTint="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Simple Calculator"
android:textSize="25sp" />
<EditText
android:id="#+id/etfirstvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter First Value"
android:inputType="number" />
<EditText
android:id="#+id/etsecondvalue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter Second Value"
android:inputType="number" />
<TextView
android:id="#+id/tvresult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Result"
android:textColor="#color/purple_500"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnadd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="ADD" />
<Button
android:id="#+id/btnsubs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Subs" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginHorizontal="10dp"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/btnmultiply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Multiply" />
<Button
android:id="#+id/btndivision"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Divide" />
</LinearLayout>
</LinearLayout>
homebutton.java
package com.example.AppCalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class homebutton extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homebutton);
Button btncalc=findViewById(R.id.btncalc);
btncalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(homebutton.this, MainActivity.class);
startActivity(intent);
}
});
}
}
homebutton.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".homebutton"
tools:ignore="ExtraText">
<Button
android:id="#+id/btncalc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:layout_weight="1"
android:backgroundTint="#color/purple_500"
android:text="Calculator" />
</LinearLayout>
You need to clarify your question more. However I believe you meant homebutton should start first and after you click calculate it should take you to the second one.
If that's the case, then you need to define your start activity from the AndroidManifest because currently your app is starting on MainActivity.java

How to get getTag() value using OnClickListener

If I use the OnClick method the system can read the tag from each textView, but if I change to textView.SetOnClickListener the system only read the textview with tag 0 only. when I tried to click other textview the system cannot read its own tag. Where did i went wronggg?? what should I do??
package com.example.brainapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
GridLayout gridLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayout = (GridLayout) findViewById(R.id.gridLayout);
textView = (TextView) findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Tag : ", v.getTag().toString());
}
});
}
}
here is my xml
<?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">
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginTop="80dp"
android:columnCount="2"
android:rowCount="2"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_row="0"
android:layout_column="0"
android:background="#FF0B0B"
android:gravity="center"
android:tag="0"
android:text="Tag 0" />
<TextView
android:id="#+id/textView"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_row="0"
android:layout_column="1"
android:background="#712EE8"
android:gravity="center"
android:tag="1"
android:text="Tag 1" />
<TextView
android:id="#+id/textView"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_row="1"
android:layout_column="0"
android:background="#0D23EA"
android:gravity="center"
android:tag="2"
android:text="Tag 2" />
<TextView
android:id="#+id/textView"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_row="1"
android:layout_column="1"
android:background="#54FD5B"
android:gravity="center"
android:tag="3"
android:text="Tag 3" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Help me!!!
You can use the same onClickListener and retrieve the tag from multiple views:
TextView a = findViewById(R.id.A);
TextView b = findViewById(R.id.B);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String string = (String)v.getTag();
Log.d("debug", string);
}
};
a.setOnClickListener(clickListener);
b.setOnClickListener(clickListener);
So you don't need to write more than one onClickListener just to do the same thing.

Click a button and be directed to new activity, Android

MainActivity.Java
package com.example.drexsprint.login;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
b2=(Button)findViewById(R.id.button2);
tx1=(TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("1234")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arduino Security"
android:id="#+id/textView"
android:textColor="#ff7aff24"
android:textSize="35dp"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="Enter Name"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:textSize="25dp"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:textColor="#ddec0d"
android:background="#0e17c2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:background="#df0c0c"
android:textColor="#0dca07"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:text="Please Login:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:textSize="35dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Second Screen App
screen.Java
package com.example.drexsprint.login;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Screen extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
public void Left(View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button2");
}
public void Right (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button3");
}
public void Temp (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button1");
}
public void Hum (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/button1");
}
public void Photo (View view) {
goToUrl("http://admin:0000000AA#10.0.1.6/image.jpg");
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
/** public void Photo (View view) {
// myWebView.loadUrl("http://admin:0000000AA#10.0.1.6/image.jpg");
}**/
}}
screen_layout
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="invisible"
android:layout_above="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Left"
android:id="#+id/button"
android:onClick="Left"
android:backgroundTint="#0f50e8"
android:textColor="#f4d318"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/button5"
android:layout_alignRight="#+id/button4"
android:layout_alignEnd="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Right"
android:id="#+id/button2"
android:onClick="Right"
android:backgroundTint="#0f50e8"
android:textColor="#f4d318"
android:layout_below="#+id/webView"
android:layout_alignRight="#+id/webView"
android:layout_alignEnd="#+id/webView"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Temp."
android:id="#+id/button3"
android:onClick="Temp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:backgroundTint="#369d7c"
android:textColor="#f4d318" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Hum."
android:id="#+id/button4"
android:onClick="Hum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:backgroundTint="#369d7c"
android:textColor="#f4d318" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo"
android:id="#+id/button5"
android:onClick="Photo"
android:backgroundTint="#589d36"
android:textColor="#f4d318"
android:layout_below="#+id/webView"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/webView"
android:background="#0a0a0a"
android:clickable="true"
android:contextClickable="true"
android:focusable="true" />
</RelativeLayout>
Okay! So I got two aplications, the first one is a login screen and the second one is a main menu of what the application should do, I want to find a way so that when a user insert the correct credentials and click on the Login button the app executes the second screen the main menu, how can i do this, Join the two apps together.
Wat i wanna achieve is on the the click of "Login Button" on the first app it Launches the Second app as a window! Thanks!! Kind of new in android any detailed help would be much appreciated.
Use startActivityForResult() to get the result from another activity.
By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa.
Intent intent=new Intent(this,Class2.class);
startActivityForResult(intent,10);

i want to calculate the number and display results in textboxes of next screen layout in android

main activity.java
package abhilmohan.blogspot.com;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
EditText amount1;
EditText amount2;
EditText amount3;
EditText amount4;
Button calculate;
double w=0;
double x=0;
double y=0;
double z=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
public void initcontrols() {
amount1=(EditText)findViewById(R.id.editText1);
amount2=(EditText)findViewById(R.id.editText2);
amount3=(EditText)findViewById(R.id.editText3);
amount4=(EditText)findViewById(R.id.editText4);
calculate=(Button)findViewById(R.id.button1);
}
public void calculate() {
w=Double.parseDouble(amount1.getText().toString());
x=Double.parseDouble(amount2.getText().toString());
y=w/12;
amount3.setText(Double.toString(y));
z=w*x/100;
amount4.setText(Double.toString(z));
}
public void gotoactivity (View v) {
Intent intent = new Intent(this,ResultPage.class);
calculate();
startActivity(intent);
}
am not getting result while calling calculator() void method on button click.i want my results to be published in two textviews created in reulst_page layout
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="left"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="abhilmohan.blogspot.com.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/ctc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:inputType="number"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/TDS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:inputType="number"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/ok"
android:textColor="#ffffff"
android:onClick="gotoactivity" />
</LinearLayout>
</RelativeLayout>
resultpage.java
package abhilmohan.blogspot.com;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class ResultPage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_page);
getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
result_page.xml
<?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:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/amount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/tdsamount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/rate"
android:textColor="#ffffff" />
</LinearLayout>
At the moment you are executing calculate() in the MainActivity.java second activity called ResultPage doesn't exist, therefore you can't change it's view (editText3and editText4).
In order to pass data to another activity you should fill your Intent with some extra data and then in your ResultPage activity's onCreate you would get underlying extras.
EDIT
MainActivity.java
public void gotoactivity (View v) {
calculate();
Intent intent = new Intent(this, ResultPage.class);
intent.putExtra("AMOUNT_3", y);
intent.putExtra("AMOUNT_4", z);
startActivity(intent);
}
ResultPage.java inside onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
int amount3 = extras.getInt("AMOUNT_3");
int amount4 = extras.getInt("AMOUNT_4");
}

Dialog + callback?

I have made a CustomTypeDialog class and what I want is to use EditText which is not in the active layout. I get a nullpointer exception when I try to click one of the buttons, which I think is because they are not in the active layout. Can you help me solve this? The dialog is called in an activity from another class.
package dk.droidrun.droidrunapp;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class CustomTypeDialog extends Dialog {
ImageButton routeType;
EditText txtType;
Button imageRun, imageBike, imageWalk;
public CustomTypeDialog(final Context context) {
super(context);
this.setContentView(R.layout.customtype_dialog);
routeType = (ImageButton)findViewById(R.id.saveRoute_activityType);
txtType = (EditText)findViewById(R.id.saveRoute_typeTxt);
imageRun = (Button)findViewById(R.id.dialog_btn1);
imageBike = (Button)findViewById(R.id.dialog_btn2);
imageWalk = (Button)findViewById(R.id.dialog_btn3);
setTitle("Select activity type");
show();
imageRun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
imageBike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Bike");
routeType.setBackgroundResource(R.drawable.track_bike);
}
});
imageWalk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Walk");
routeType.setBackgroundResource(R.drawable.track_walk);
}
});
}
}
This is my customtype_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AutoMode"
android:background="#color/black" >
<RelativeLayout
android:id="#+id/dialog_relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/dialog_relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/dialog_btn1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/track_run"
android:layout_alignRight="#+id/dialog_relativeLayout1"
android:layout_alignTop="#+id/dialog_relativeLayout1"
/>
<Button
android:id="#+id/dialog_btn2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/track_bike"
android:layout_alignTop="#+id/dialog_relativeLayout1"
android:layout_toRightOf="#+id/dialog_btn1"
/>
<Button
android:id="#+id/dialog_btn3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/dialog_btn2"
android:background="#drawable/track_walk"
/>
</RelativeLayout>
</RelativeLayout>
saveroutes.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black"
tools:context=".SaveRouteActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="#string/saveRoute"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Enter a name for the route" />
<EditText
android:id="#+id/saveRoute_nameRoute"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="#string/saveRoute_name"
android:textColor="#color/white"
android:background="#4e4751"
android:inputType="textPersonName" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Describe your route" />
<EditText
android:id="#+id/saveRoute_desciptionTxt"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="#string/saveRoute_description"
android:textColor="#color/white"
android:background="#4e4751"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Activity type (e.g. running)"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<EditText
android:id="#+id/saveRoute_typeTxt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="17dip"
android:ems="10"
android:layout_marginLeft="30dp"
android:hint="#string/saveRoute_type"
android:textColor="#color/white"
android:background="#4e4751" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/saveRoute_activityType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/track_walk" />
</LinearLayout>
<Button
android:id="#+id/saveRoute_saveBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#color/white"
android:text="#string/saveRoute_savebutton" />
<Button
android:id="#+id/saveRoute_cancelBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#color/white"
android:text="#string/saveRoute_cancel" />
</LinearLayout>
You cannot access views from another layout (your activity) using findViewById within the dialog view.
You need to add a callback listener for when the buttons on your dialog are clicked:
public interface OnDialogClickListener {
void onDialogImageRunClick();
}
public class CustomTypeDialog extends Dialog {
private final OnDialogClickListener listener;
public CustomTypeDialog(final Context context, OnDialogClickListener listener) {
this.listener = listener;
}
....
imageRun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onDialogImageRunClick();
}
);
}
Then when you create your dialog in your Activity where you have access to the view:
new CustomTypeDialog(context, new CustomTypeDialog.OnDialogClickListener() {
#Override
public void onDialogImageRunClick() {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});

Categories