Android Beginner need help-1.button not displaying text 2. is logic correct



Question 1) For some reason, when i click "generate" it does not display any text.I want the button to do the following: after the user input 3 values (int) for example : 1,1,2 and click the "generate" button and the display message should be "Isosceles Triangle: 2 Congruent Sides"---this works before but my logic was wrong and I asked a question here : If statement not working for android programming . then i try to implement the suggestions and now it has a different error


Question 2) Is my logic correct? Will I be able to display the correct result after fixing this problem?


Please help, I am new to android programming. Thanks so much in advance.


java code:



package com.example.trianglegame;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TriangleGame extends Activity {// implement-have to use all of the
// methods
// set up the variables here

Button Gen;
EditText Input1;
EditText Input2;
EditText Input3;
String input1;
String input2;
String input3;

TextView Display;

int a;
int b;
int c;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game);

// assigning the values
Input1 = (EditText) findViewById(R.id.editText1);
Input2 = (EditText) findViewById(R.id.editText2);
Input3 = (EditText) findViewById(R.id.editText3);

Display = (TextView) findViewById(R.id.textView5);

Gen = (Button) findViewById(R.id.button1);

Gen.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

// getting the text and converting it to string value
input1 = Input1.getText().toString();
input2 = Input2.getText().toString();
input3 = Input2.getText().toString();

// converting those text values back into int
a = Integer.parseInt(input1);
b = Integer.parseInt(input2);
c = Integer.parseInt(input3);

// displaying the message

if ((a == b && b != c) || (a == c && b != c)
|| (b == c && a != c)) {
Display.setText("Isosceles Triangle: 2 Congruent Sides");
} else if (a == b && a == c) {
Display.setText("Equilateral Triangle:All sides are equal");
}

else if (a != b && a != c && b != c) {
Display.setText("Scalene Triangle: No Congruent Sides");
} else {
Display.setText("Error");
}

}
});
}
}


xml code



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/enter_text" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/side_1" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/type_hint"
android:inputType="number" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/side_2" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/type_hint"
android:inputType="number" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/type_hint"
android:text="@string/side_3" />

<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/type_hint"
android:inputType="number" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/generate" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear" />
</LinearLayout>

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

</LinearLayout>

No comments:

Post a Comment