Note: Sorry if my problem may be really easy to fix, I'm a total beginner in this, I literally started learning this just 2 days ago.
So anyway, I'm making this basic calculator app for androids. I built the .apk and sent it to my phone but it just shows a blank white screen and then it crashes. Here's the xml code and the java code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/numbs" >
<TextView
android:id="#+id/title"
android:text="Calculator"
android:textColor="#E11608"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35sp"
android:layout_gravity="top|center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#000000"
/>
<EditText
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:inputType="number"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:hint="Type in your next value"
/>
<EditText
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:inputType="number"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:layout_gravity="center"
android:layout_marginBottom="148dp"
android:hint="Type in your first value"
/>
<Button
android:id="#+id/add"
android:text="+"
android:layout_gravity="center|left"
android:onClick="onClick"
/>
<Button
android:id="#+id/minus"
android:text="-"
android:layout_gravity="center"
android:layout_marginRight="40dp"
android:onClick="onClick"
/>
<Button
android:id="#+id/divide"
android:text="÷"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:onClick="onClick"
/>
<Button
android:id="#+id/multiply"
android:text="×"
android:layout_gravity="center|right"
android:onClick="onClick"
/>
<TextView
android:id="#+id/answer"
android:text="Your answer should display here"
android:textColor="#E11608"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_gravity="bottom|center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="#FFFFFF"
/>
</FrameLayout>
Java code:
package com.Drift.app;
import android.app.*;
import android.os.*;
import java.io.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
public class MainActivity extends Activity
{
int val1;
int val2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
work();
}
public void work()
{
//create parameter for add button
Button add = (Button) findViewById(R.id.add);
Button multiply = (Button) findViewById(R.id.multiply);
Button minus = (Button) findViewById(R.id.minus);
Button divide = (Button) findViewById(R.id.divide);
TextView title = (TextView) findViewById(R.id. title);
final TextView answer = (TextView) findViewById(R.id. answer);
final EditText value1 = (EditText) findViewById(R.id. value1);
final EditText value2 = (EditText) findViewById(R.id. value2);
val1 = Integer.parseInt(value1.getText().toString());
final Integer val1 = new Integer(value1.getText().toString());
val2 = Integer.parseInt(value2.getText().toString());
final Integer val2 = new Integer(value2.getText().toString());
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 + val2));
}
});
multiply.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 * val2));
}
});
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 - val2));
}
});
divide.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 / val2));
}
});
}
}
Sorry for not commenting much. Can someone please tell me what I did wrong or maybe propose a better code. P.s sorry again if nearly everything here is wrong but like I said, I'm a total beginner. Thanks.
Okay, thanks for the answers this is is what I ended up with
package com.Drift.app;
import android.app.*;
import android.os.*;
import java.io.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;
public class MainActivity extends Activity
{
int val1;
int val2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
work();
}
public void work()
{
//create parameter for add button
Button add = (Button) findViewById(R.id.add);
Button multiply = (Button) findViewById(R.id.multiply);
Button minus = (Button) findViewById(R.id.minus);
Button divide = (Button) findViewById(R.id.divide);
TextView title = (TextView) findViewById(R.id. title);
final TextView answer = (TextView) findViewById(R.id. answer);
final EditText value1 = (EditText) findViewById(R.id. value1);
final EditText value2 = (EditText) findViewById(R.id. value2);
if(value1.getText()!=null)
{val1 = Integer.parseInt(value1.getText().toString());}
if(value2.getText()!=null)
{val2 = Integer.parseInt(value2.getText().toString());}
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 + val2));
}
});
multiply.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 * val2));
}
});
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 - val2));
}
});
divide.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
answer.setText(String.valueOf(val1 / val2));
}
});
}
}
But it's still giving me a blank white page, I'm using gradle by the way
Here's the Android manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Drift.app" >
<application
android:allowBackup="true"
android:icon="#drawable/calc"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
java.lang.RuntimeException: Binary XML file line #51: You must supply a layout_width attribute.
All views must have android:layout_width and android:layout_height defined. At least your Buttons don't have either.
Try to check EditText value before use it :
if(value1.getText().toString().trim().length()>0){
val1 = Integer.parseInt(value1.getText().toString());
}
if(value2.getText().toString().trim().length()>0){
val2 = Integer.parseInt(value2.getText().toString());
}
It is a probably a problem with a parsing. You are getting value from empty editText. You should not do that in on create method. You should check if value2 and value1 are null before using it.
if(value2.getText()!=null){val2 = Integer.parseInt(value2.getText().toString());}
here is the full working code. i have executed this and its working fine.
ur activity
public class MainAvtivity extends Activity {
int val1;
int val2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbar);
work();
}
public void work() {
// create parameter for add button
Button add = (Button) findViewById(R.id.add);
Button multiply = (Button) findViewById(R.id.multiply);
Button minus = (Button) findViewById(R.id.minus);
Button divide = (Button) findViewById(R.id.divide);
TextView title = (TextView) findViewById(R.id.title);
final TextView answer = (TextView) findViewById(R.id.answer);
final EditText value1 = (EditText) findViewById(R.id.value1);
final EditText value2 = (EditText) findViewById(R.id.value2);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 + val2));
}
});
multiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 * val2));
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 - val2));
}
});
divide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
if (!"".equals(value1.getText().toString())) {
val1 = Integer.parseInt(value1.getText().toString());
}
if (!"".equals(value2.getText().toString())) {
val2 = Integer.parseInt(value2.getText().toString());
}
answer.setText(String.valueOf(val1 / val2));
}
});
}
}
and u have not specified height and weight in xml.
try this xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/numbs"
android:gravity="center" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="top|center"
android:background="#000000"
android:text="Calculator"
android:textColor="#E11608"
android:textSize="35sp" />
<EditText
android:id="#+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginBottom="80dp"
android:background="#FFFFFF"
android:ems="10"
android:hint="Type in your next value"
android:inputType="number" />
<EditText
android:id="#+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginBottom="148dp"
android:background="#FFFFFF"
android:ems="10"
android:hint="Type in your first value"
android:inputType="number" />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:onClick="onClick"
android:text="+" />
<Button
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="40dp"
android:onClick="onClick"
android:text="-" />
<Button
android:id="#+id/divide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="40dp"
android:onClick="onClick"
android:text="÷" />
<Button
android:id="#+id/multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:onClick="onClick"
android:text="×" />
<TextView
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom|center"
android:layout_marginBottom="40dp"
android:background="#FFFFFF"
android:text="Your answer should display here"
android:textColor="#E11608"
android:textSize="10sp" />
</FrameLayout>
Related
i am creating a quiz app that is multiple choice
in my first activity(MainActivity) is the question(text) and choices
and in my second activity( QuizImage1)is the question(image) and choices
how will i get the result of the score in main activity to view also in quizimage1
the second image is my error on quizimage1 because i did not copy the first activity score
i hope you understand what iam talking about
plss fix my codes
iam beginner in java
Main_Activity.java
package com.example.ltoexam;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private com.example.ltoexam.QuestionLibrary nQuestionLibrary = new com.example.ltoexam.QuestionLibrary();
public TextView nScoreView;
private TextView nQuestionView;
private Button nButtonChoice1;
private Button nButtonChoice2;
private Button nButtonChoice3;
private Button quit;
private String nAnswer;
public int nScore = 0;
private int nQuestionNumber = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nScoreView = (TextView) findViewById(R.id.score);
nQuestionView = (TextView) findViewById(R.id.question);
nButtonChoice1 = (Button) findViewById(R.id.choice1);
nButtonChoice2 = (Button) findViewById(R.id.choice2);
nButtonChoice3 = (Button) findViewById(R.id.choice3);
quit = (Button) findViewById(R.id.quit) ;
updateQuestion();
//Start of Button Listener for Button1
nButtonChoice1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//My logic for Button goes in here
if (nButtonChoice1.getText() == nAnswer){
nScore =nScore + 1;
updateScore(nScore);
updateQuestion();
//This line of code is optional
Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong the answer is " + nAnswer, Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button2
//Start of Button Listener for Button2
nButtonChoice2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//My logic for Button goes in here
if (nButtonChoice2.getText() == nAnswer){
nScore =nScore + 1;
updateScore(nScore);
updateQuestion();
//This line of code is optional
Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong the answer is " + nAnswer, Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
quit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
//End of Button Listener for Button2
//Start of Button Listener for Button3
nButtonChoice3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//My logic for Button goes in here
if (nButtonChoice3.getText() == nAnswer){
nScore =nScore + 1;
updateScore(nScore);
updateQuestion();
//This line of code is optional
Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "wrong the answer is " + nAnswer, Toast.LENGTH_SHORT).show();
updateQuestion();
}
}
});
//End of Button Listener for Button2
}
private void updateQuestion(){
if(nQuestionNumber==7){
openQuizImage1();}
else {
nQuestionView.setText(nQuestionLibrary.getQuestion(nQuestionNumber));
nButtonChoice1.setText(nQuestionLibrary.getChoice1(nQuestionNumber));
nButtonChoice2.setText(nQuestionLibrary.getChoice2(nQuestionNumber));
nButtonChoice3.setText(nQuestionLibrary.getChoice3(nQuestionNumber));
nAnswer = nQuestionLibrary.getCorrectAnswer(nQuestionNumber);
nQuestionNumber++;
}
}
public void updateScore(int point){
nScoreView.setText("" + nScore);
}
public void openQuizImage1() {
Intent intent = new Intent(this, QuizImage1.class);
startActivity(intent);
}
}
QuestionLibrary.java
package com.example.ltoexam;
public class QuestionLibrary {
private String nQuestions [] = {
"1.The three colors of the traffic lights are:",
"2.Yellow triangular signs provide what kind of information",
"3.Which of the following traffic signs are blue?",
"4.Steady green light means",
"5.A flashing yellow light at a road crossing signifies",
"6.A solid white line on the right edge of the highway slopes in towards your left. This shows that",
"7.You are in a No-Passing zone when the center of the road is marked by"
};
private String nChoices [] [] = {
{"red, green and yellow", "red, green and blue", "yellow, green and blue"},
{"warning", "hospital across", "speed limit"},
{"regulatory signs", "information signs", "danger warning signs"},
{"you must yield to all pedestrians and other motorists using the intersection", "go, it is safe to do so", "proceed cautiously through the intersection before the light changes to red."},
{"Caution - slow down and proceed with caution", "Stop and stay until light stops flashing", "Wait for the green light"},
{"there is an intersection joint ahead", "the road will get narrower", "you are approaching a construction area"},
{"a broken yellow line","a broken white line","two solid yellow lines"}
};
private String nCorrectAnsers[] = {"red, green and yellow", "warning", "information signs", "go, it is safe to do so", "Caution - slow down and proceed with caution", "the road will get narrower", "two solid yellow lines"};
public String getQuestion(int a) {
String question = nQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice0 = nChoices[a] [0];
return choice0;
}
public String getChoice2(int a) {
String choice1 = nChoices[a] [1];
return choice1;
}
public String getChoice3(int a) {
String choice2 = nChoices[a] [2];
return choice2;
}
public String getCorrectAnswer(int a) {
String answer = nCorrectAnsers[a];
return answer;
}
}
QuizImage1.java (this is my second activity)
package com.example.ltoexam;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizImage1 extends AppCompatActivity {
private Button but1;
private Button but2;
private Button but3;
public TextView nScoreView;
public int nScore;
public TextView nScoreView() {
return nScoreView;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_image1);
but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but3 = (Button) findViewById(R.id.but3);
nScoreView = (TextView) findViewById(R.id.score);
but1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(QuizImage1.this, "wrong the answer is Road goes left or right", Toast.LENGTH_SHORT).show();
openQuizImage2();
}
});
but2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(QuizImage1.this, "wrong the answer is Road goes left or right", Toast.LENGTH_SHORT).show();
openQuizImage2();
}
});
but3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(QuizImage1.this, "correct", Toast.LENGTH_SHORT).show();
openQuizImage2();
nScore =nScore + 1;
updateScore(nScore);
}
});
}
public void updateScore(int point){
nScoreView.setText("" + nScore);}
public void openQuizImage2(){
Intent intent = new Intent(this, QuizImage2.class);
startActivity(intent);
}
}
activity_main.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginBottom="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:id="#+id/score_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:text="0"
android:id="#+id/score"/>
</RelativeLayout>
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="116dp"
android:layout_marginBottom="40dp"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="Which thing is alive?"
android:textSize="20sp"
android:textStyle="bold|italic" />
<Button
android:id="#+id/choice1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#0091EA"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="bird"
android:textColor="#fff" />
<Button
android:id="#+id/choice2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#0091EA"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="door"
android:textColor="#fff" />
<Button
android:id="#+id/choice3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#0091EA"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="rock "
android:textColor="#fff" />
<Button
android:id="#+id/quit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:background="#871C1C"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="Quit"
android:textColor="#fff" />
</LinearLayout>
activity_quiz_image1.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=".QuizImage1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginBottom="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:id="#+id/score_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:text="0"
android:id="#+id/score"/>
</RelativeLayout>
<Button
android:id="#+id/but1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="317dp"
android:layout_marginBottom="238dp"
android:background="#0091EA"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="road ends"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/but2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="396dp"
android:layout_marginBottom="159dp"
android:background="#0091EA"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="crossroads"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/but3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="476dp"
android:layout_marginBottom="79dp"
android:background="#0091EA"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="Road goes left or right"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/quit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="549dp"
android:layout_marginBottom="6dp"
android:background="#871C1C"
android:fontFamily="#font/carter_one"
android:padding="8dp"
android:text="Quit"
android:textColor="#fff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="249dp"
android:layout_height="182dp"
app:srcCompat="#drawable/road_goes_left_right_road_traffic_sign"
tools:layout_editor_absoluteX="81dp"
tools:layout_editor_absoluteY="67dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
To transfer information from one activity to another, use the following :
Intent intent = new Intent(this, ActivityNext.class);
intent.putExtra("NSCORE_KEY", nScore );
startActivity(intent);
And in another activity get :
int nScore = getIntent().getIntExtra("KEY",1);
To send data through activities you have to use the same intent you use to navigate to the activity as below.
Intent intent = new Intent(this, QuizImage1.class);
intent.putExtra("keyScore",nScore);
startActivity(intent);
In the second activity QuizImage1.class in OnCreate method add the following code
int score = getIntent().getIntExtra("keyScore",1);
Please take a look at the official tutorial from android developer website
I have a custom dialog in which I include another layout. In this layout there are 3 buttons and a TextView none of these is null. If I click the buttons they work correctly. But the TextView doesn't show any text. The text should appears when I click another button. That's how it should works
Custom dialog layout:
<LinearLayout
............
.....
<androidx.cardview.widget.CardView
android:id="#+id/result_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="2dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:visibility="gone"
app:cardCornerRadius="8dp">
<include android:id="#+id/result_layout" layout="#layout/result_block" />
</androidx.cardview.widget.CardView>
....
....
</LinearLayout>
The result_block layout
<?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="wrap_content"
android:padding="16dp"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/result_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text=""
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"
android:padding="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_copy_24px_outlined"
android:layout_marginEnd="16dp"
android:tint="#color/colorWhite"
android:background="?attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:tint="#color/colorWhite"
android:src="#drawable/ic_share_24px_outlined"
android:background="?attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#color/colorWhite"
android:src="#drawable/ic_volume_up_24px_outlined"
android:background="?attr/selectableItemBackgroundBorderless"
/>
</LinearLayout>
</LinearLayout>
And now the dialog
private void showDiag() {
final View dialogView = View.inflate(getActivity(), R.layout.custom_dialog_layout,null);
final View resultLayout = View.inflate(getActivity(), R.layout.result_block,null);
final Dialog dialog = new Dialog(getActivity(), R.style.MyAlertDialogStyle);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(dialogView);
final TextView resultTxt = resultLayout.findViewById(R.id.result_txt);
final ImageButton btn1 = resultLayout.findViewById(R.id.btn1);
final CardView resultCardView = dialog.findViewById(R.id.result_card_view);
final TextInputEditText editText = dialog.findViewById(R.id.text_edit);
final ImageButton clearText = dialog.findViewById(R.id.clear_text);
MaterialButton resultConfirm = dialog.findViewById(R.id.result_confirm);
ImageButton btn2 = dialogView.findViewById(R.id.copy_btn);
ImageButton btn3 = dialogView.findViewById(R.id.share_btn);
resultConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText.getText().toString().equals("")) {
String theWord = editText.getText().toString();
String result = Utils.getSecondWord(theWord);
// result it shows me the correct string with no errors or something else
resultTxt.setText(result); // doesn't work. Not set the text
insertWord(theWord, result);
resultCardView.setVisibility(View.VISIBLE);
resultCardView.setVisibility(View.VISIBLE);
} else {
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
// Share btn
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Utils.revealShow(dialogView, true, null, resultConfirm);
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (i == KeyEvent.KEYCODE_BACK){
Utils.revealShow(dialogView, false, dialog, resultConfirm);
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
Everything inside the dialog works correctly but the TextView not. Even if I try to write something else like resultTxt.setText("Hello there"); the text not appears. Any suggestions?
Please you remove the android:text=""in TextView because textview is get default text is empty or you write anything in TextView like android:text="abc"
I am trying to make a calculator application.I want to evaluate the contents of a TextView and display it as a toast message. The eval() statement throws an exception for ScriptException as below.
"Unhandled exception: javax.script.ScriptException"
Even when I have imported javax.script.ScriptException. I am not getting from why it is throwing exception.
Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="95dp"
android:textAlignment="center"
android:textSize="36sp" />
<Button
android:id="#+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginTop="194dp"
android:text="1" />
<Button
android:id="#+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/n1"
android:layout_centerHorizontal="true"
android:text="2" />
<Button
android:id="#+id/n3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/n1"
android:layout_marginEnd="16dp"
android:text="3" />
<Button
android:id="#+id/n4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/n1"
android:layout_centerVertical="true"
android:text="4" />
<Button
android:id="#+id/n5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="5" />
<Button
android:id="#+id/n6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/n3"
android:layout_centerVertical="true"
android:text="6" />
<Button
android:id="#+id/n7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/n1"
android:layout_marginBottom="194dp"
android:text="7" />
<Button
android:id="#+id/n8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/n7"
android:layout_centerHorizontal="true"
android:text="8" />
<Button
android:id="#+id/n9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/n3"
android:layout_alignTop="#+id/n7"
android:text="9" />
<Button
android:id="#+id/n0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="129dp"
android:text="0" />
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/n3"
android:layout_alignTop="#+id/n0"
android:text="CLEAR" />
<Button
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="73dp"
android:text="+" />
<Button
android:id="#+id/equal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/n3"
android:layout_alignTop="#+id/plus"
android:text="=" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
and here is my java code:
package com.example.poopypigeon.calx;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class MainActivity extends AppCompatActivity {
Button n1;
Button n2;
Button n3;
Button n4;
Button n5;
Button n6;
Button n7;
Button n8;
Button n9;
Button n0;
Button clear;
Button plus;
Button equal;
TextView value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value = findViewById(R.id.textView);
n1 = findViewById(R.id.n1);
n1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"1");
}
});
n2 = findViewById(R.id.n2);
n2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"2");
}
});
n3 = findViewById(R.id.n3);
n3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"3");
}
});
n4 = findViewById(R.id.n4);
n4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"4");
}
});
n5 = findViewById(R.id.n5);
n5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"5");
}
});
n6 = findViewById(R.id.n6);
n6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"6");
}
});
n7 = findViewById(R.id.n7);
n7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"7");
}
});
n8 = findViewById(R.id.n8);
n8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"8");
}
});
n9 = findViewById(R.id.n9);
n9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"9");
}
});
n0 = findViewById(R.id.n0);
n0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val+"0");
}
});
clear = findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
value.setText("");
}
});
plus = findViewById(R.id.plus);
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
value.setText(val + "+");
}
});
equal = findViewById(R.id.equal);
equal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String val = value.getText().toString();
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
Object ans = engine.eval(val);
Toast toast = Toast.makeText(MainActivity.this,"the ans is: "+ ans,Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
PS. i need a reply as soon as possible as this is for a school project
I would do it like this to further investigate:
try{
ScriptEngineManager manager = new ScriptEngineManager()
ScriptEngine engine = manager.getEngineByExtension("js");
Object ans = engine.eval(val);
Toast toast = Toast.makeText(MainActivity.this,"the ans is: "+ ans,Toast.LENGTH_SHORT);
toast.show();
catch(ScriptEngineManager e) {
// handle exception
System.err.println("Error making calculation: " + e.getMessage());
}
EDIT: Change:
String val = value.getText().toString();
value.setText(val+"1");
You have only the value in the String val - but you need the content of value textfield. In the String val there has to be something like "4+4".
You just add numbers to the textfield but no operators.
value.setText(val+"1"); -> You just add the Number 1 to the Textfield, so after clicking some Buttons you have like this in your Textfield "512312"
Copy this import statement
import javax.script.ScriptException;
Try is code:
try {
Object ans = engine.eval(val);
System.out.println("It worked!:" + ans.toString());
}
catch (ScriptException se) {
System.out.println("Problem in eval:", se.getmessage());
}
Change ScriptException with Exception
try {
ans = engine.eval(val);
} catch (Exception e) {
e.printStackTrace();
}
You are not sure for which type of exception it may trigger so it will display the same message for all the exceptions and user may not be able to understand which exception occurred. Thats the reason you should place is at the end of all the specific exception catch blocks
To simplify the debugging here, focus on this line:
Object ans = engine.eval(val);
Print the value of val so we can see the source that is being evaluated.
Wrap that line in a try catch block and print the full stack trace so we can see the full error diagnostic.
I want to get Values from EditText dynamically.. I have a lot of EditText generated when user press add button..When User presses add Button it generates 3 Edittext each time. I dont know how to get the values from this dynamically generated EdiTtext . Now My question is how can i get the values from The 3 Edittext on each row. ALso I need to verify that if user removed the view or not. Please help I am new android development.This should happen when a user presses on Save Button. Thanks in advance!
This is class .
public class SecondActivity extends Activity {
Button saveBtn,cancelBtn,addBtn;
RelativeLayout layout;
EditText third,first,second;
LinearLayout Container;
int counter=0;
int all=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
saveBtn=(Button) findViewById(R.id.save);
cancelBtn=(Button) findViewById(R.id.cancel);
Container=(LinearLayout) findViewById(R.id.container);
addBtn=(Button) findViewById(R.id.addBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
Toast.makeText(SecondActivity.this, "The Result: "+all,Toast.LENGTH_LONG).show();
finish();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
// EditText ed1=(EditText) findViewById(R.id.editText1);
// EditText ed2=(EditText) findViewById(R.id.editText2);
// EditText ed3=(EditText) findViewById(R.id.editText3);
// all=all+Integer.parseInt(ed1.getText().toString())+Integer.parseInt(ed2.getText().toString())+Integer.parseInt(ed3.getText().toString());
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Container.addView(addView);
}});
}
}
This is my row.xml .which i am using as a view in java code.
<?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:layout_margin="10dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Field 1" />
<EditText
android:id="#+id/editText1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:inputType="numberPassword" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:inputType="numberPassword" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:text="Remove" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:text="Field 2" />
<EditText
android:id="#+id/editText3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_toRightOf="#+id/editText2"
android:editable="false"
tools:ignore="Deprecated" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_alignLeft="#+id/editText3"
android:text="Field 3" />
</RelativeLayout>
This is my layout which is attached with my activity class.
<?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:layout_alignParentTop="true"
android:orientation="vertical"
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=".SecondActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Add" />
</RelativeLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="300dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/save"
android:layout_width="146dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Save" />
<Button
android:id="#+id/cancel"
android:layout_width="146dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Cancel" />
<EditText
android:id="#+id/editText1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_above="#+id/save"
android:layout_alignParentLeft="true"
android:editable="false"
android:hint="T 1"
tools:ignore="Deprecated" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:editable="false"
android:hint="T 2"
tools:ignore="Deprecated" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_toRightOf="#+id/editText2"
android:editable="false"
android:hint="T 3"
tools:ignore="Deprecated" />
<EditText
android:id="#+id/editText4"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText3"
android:layout_alignParentRight="true"
android:editable="false"
android:hint="T 4"
tools:ignore="Deprecated,HardcodedText" />
</RelativeLayout>
</LinearLayout>
This is your edited SecondActivity.java
public class SecondActivity extends Activity {
Button saveBtn, cancelBtn, addBtn;
RelativeLayout layout;
EditText third, first, second;
LinearLayout Container;
int counter = 0;
int all = 0;
int tag = 0;
ArrayList<Integer> dynamicLayoutsTags;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
saveBtn = (Button) findViewById(R.id.save);
cancelBtn = (Button) findViewById(R.id.cancel);
Container = (LinearLayout) findViewById(R.id.container);
addBtn = (Button) findViewById(R.id.addBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dynamicLayoutsTags.size() > 0) {
for (int i = 0; i < dynamicLayoutsTags.size(); i++) {
View getView = Container
.findViewWithTag(dynamicLayoutsTags.get(i));
EditText editText1 = (EditText) getView
.findViewById(R.id.editText1);
EditText editText2 = (EditText) getView
.findViewById(R.id.editText2);
EditText editText3 = (EditText) getView
.findViewById(R.id.editText3);
Toast.makeText(
SecondActivity.this,
"Row " + i + " : " + "editext 1 is : "
+ editText1.getText()
+ " editext 2 is : "
+ editText2.getText()
+ " editext 3 is : "
+ editText3.getText(),
Toast.LENGTH_LONG).show();
}
}
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
Toast.makeText(SecondActivity.this, "The Result: " + all,
Toast.LENGTH_LONG).show();
finish();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button) addView
.findViewById(R.id.remove);
addView.setTag(tag);
buttonRemove.setTag(tag);
dynamicLayoutsTags.add(tag);
Container.addView(addView);
tag++;
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ((LinearLayout) addView.getParent())
// .removeView(addView);
Integer removeTag = (Integer) v.getTag();
View deleteView = Container.findViewWithTag(removeTag);
Container.removeView(deleteView);
dynamicLayoutsTags.remove(removeTag);
}
});
}
});
}
#Override
protected void onResume() {
super.onResume();
tag = 0;
dynamicLayoutsTags = new ArrayList<Integer>();
}
}
I'm starter programmer on Android environment. And I need your help.
Here is the what I'm trying to do
package com.codeherenow.sicalculator;
import com.codeherenow.sicalculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class SICalculatorActivity extends Activity {
private TextView PA;
private TextView Interest_Rate;
private TextView Years;
private EditText PA_bar;
private EditText IR_bar;
private SeekBar year_bar;
private Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years= (TextView) findViewById(R.id.Years);
PA_bar= (EditText) findViewById(R.id.PA_bar);
IR_bar= (EditText) findViewById(R.id.IR_bar);
year_bar=(SeekBar) findViewById(R.id.year_bar);
calculate=(Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener(){
public void onClick(View v){
}
}
}
}
My 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: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=".SICalculatorActivity" >
<TextView
android:id="#+id/Years"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/IR_bar"
android:layout_centerVertical="true"
android:text="2 Year(s)"
android:textSize="20sp" />
<SeekBar
android:id="#+id/year_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Years"
android:layout_below="#+id/Years"
android:layout_marginTop="21dp" />
<Button
android:id="#+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/year_bar"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/year_bar"
android:text="Calculate" />
<EditText
android:id="#+id/IR_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Years"
android:layout_marginBottom="14dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/Interest_Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/IR_bar"
android:layout_alignLeft="#+id/IR_bar"
android:layout_marginBottom="15dp"
android:text="Interest Rate"
android:textSize="20sp" />
<TextView
android:id="#+id/PA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/PA_bar"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Principal Amount"
android:textSize="20sp" />
<EditText
android:id="#+id/PA_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Interest_rate"
android:layout_below="#+id/PA"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
Actually , I need help for not xml part. I need help for java coding part. Even if i'm trying to use onclicklistener , compiler always show error on coding page? Could someone help me?
Try this:
public class MainActivity extends Activity {
TextView PA;
TextView Interest_Rate;
TextView Years;
EditText PA_bar;
EditText IR_bar;
EditText year_bar;
Button calculate;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years = (TextView) findViewById(R.id.Years);
PA_bar = (EditText) findViewById(R.id.PA_bar);
IR_bar = (EditText) findViewById(R.id.IR_bar);
year_bar = (EditText) findViewById(R.id.year_bar);
calculate = (Button) findViewById(R.id.calculate);
t1 = (TextView) findViewById(R.id.t1);
calculate.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
int num1 = Integer.parseInt(PA_bar.getText().toString());
int num2 = Integer.parseInt(IR_bar.getText().toString());
int num = Integer.parseInt(year_bar.getText().toString());
int si = (num1 * num2 * num) / 100;
t1.setText(Integer.toString(si));
}
});
}};
You have a syntactical error in your code. You are missing the closing parentheses and semi-colon.
calculate.setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
}); //<--- Change is here.