correct text input to next activity - java

I am a completely new programmer and I am looking to make a sort of quiz app as my first app. Just Questions with the right answer that will send the user to the next activity. I figured out how to edit buttons but I am not sure what to add to the JAVA file or XML file that will allow the next activity (screen) to be opened up.
This is my Layout so far. I have decided the password input but the answer to the question will be stored within the app. Not sure if this is the right approach?
`
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="78dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:text="Answer" />
`
Thanks in advance for any help rendered!
#kanwaljit Sngh
I am getting multiple errors like "button cannot be resolved to a type" and "R cannot be resolved to a variable" What does these mean?
`import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Startscreen extends Activity {
EditText editText1 = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = editText1.getText().toString().trim();
if (answer.equals("desired answer")) {
Intent i = new Intent(getApplicationContext(),
CorrectAnswerActivity.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
WrongAnswerActivity.class);
startActivity(i);
}
}
});
`

Get the value of edittext
check it with desired answer
If true, redirect to next activity
Eg :
if(editText1.getText().toString().equalsIgnoreCase("desiredanswer")
{
startActivity(new Intent(this,nextActivity.class));
}
else
{
Toast.makeText(this,"Wrong answer",2000).show();
}

EditText editText1 = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String answer = editText1.getText().toString().trim();
if (answer.equals("desired answer")) {
Intent i = new Intent(getApplicationContext(),
CorrectAnswerActivity.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
WrongAnswerActivity.class);
startActivity(i);
}
}
});
and write new two activity CorrectAnswerActivty and WrongAnswerActivity
and do actions what to display in these activties
and add the following line to the xml file in button tag-
android:id="#+id/button"

Related

Button is not reacting to click

I created a simple activity that is intended to send two pieces of information to another activity. However, my button isn't registering any click events. Does anyone have any idea why this would be? I expected that by adding the onClick = "onClickWrite" to the XML file that they would be linked but there is no response when clicked. If anyone could help me out, I would greatly appreciate it. I have tried implementing the onClickListener, however, with that implementation, it throws an error on my Toasts.
Activity
public class InitializeClass extends Activity {
private Service service;
private Button button;
EditText infoOne;
EditText infoTwo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_control);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(clicked);
infoOne= (EditText) findViewById(R.id.editText);
infoTwo= (EditText) findViewById(R.id.editText1);
}
private View.OnClickListener clicked = new View.OnClickListener(){
#Override
public void onClick(View view) {
if (service != null) {
int size = 100;
byte[] byteArray = new byte[size];
byte[] byteArrayTwo = new byte[size];
byteArray = infoOne.getText().toString().getBytes(Charset.defaultCharset());
byteArrayTwo= infoTwo.getText().toString().getBytes(Charset.defaultCharset());
if ((!(infoOne.getText().toString().isEmpty())) && (!(infoTwo.getText().toString().isEmpty()))) {
service.setInfo(byteArray);
service.setInfoTwo(byteArrayTwo);
intentMethod();
}
}
}
};
public void intentMethod() {
Intent intent = new Intent(this, DeviceScanActivity.class);
startActivity(intent);
}
}
XML:
<?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=".InitializeClass">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/send_info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText1" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="150dp"
android:ems="10"
android:inputType="textPersonName"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHint="#android:color/white"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHint="#android:color/white"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>
You need to initialize your button first.
Button button = (Button) findViewById(R.id.button);
Hope this helps!
In your activity class assign the button to the id of the button in the xml:
private Button btn = findViewById(R.id.button);
You will then create an OnClickListener:
private View.OnClickListener clicked = new View.OnClickListener(){
#Override
public void onClick(View view) {
//Insert Code here
}
};
In your onCreate method in the same activity class you will instantiate the button and assign the button to the onClickListener:
btn = new Button(this);
btn.setOnClickListener(clicked);
There are couple of things you need to do for that.
Create Field.
private Button button;
Initialize button.
Button button = (Button) findViewById(R.id.button);
Set Listener on button.
button.setOnClickListener();
I found the answer. Service in if (service != null) was never being initialized. Stupid mistake on my part. Thanks, everyone for the help and directing me in a way to implement onClickListener!
You need to cast the button (name) with you Java code .
for example on xml you have id/button (name)
declare it on your Java file, and do the casting.
and the onClickListener will look like this :
Cast the button : Button button ;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});

How to change activity after onclick action with data binding

i have the edit view and button in main activity. Here is the button code:
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="#{handler::onButtonClick}"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
how can i change activity in onButtonClick method?
<Button
android:id="#+id/button"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="onButtonClick"
android:text="#string/button_send"
app:layout_constraintBaseline_toBaselineOf="#+id/editText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/editText" />
And add this function to your java code:
public void onButtonClick(View view) {
Intent intent = new Intent(context, YourActivityClass.class);
context.startActivity(intent);
}
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here you can edit the view and change activity
// Change Activity using Intent
startActivity(new Intent(CurrentActivity.this, SecondActivity.class));
}
});
}
Hope this answer helps you.
In your main Activity
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getContext(),YOURACTIVITYNAME.class);
startActivity(intent);
}
});

How to update text field on button click?

I need help in the MainActivity.java coding such that the value of the text field is stored into a variable on button click so that i could use it for search query later.
The activity_main.xml contains the following:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:text="Search"
android:ems="10"
android:id="#+id/SearchText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2" />
Could anyone suggest what the MainActivity.java file should contain as to recieve the text value as i am new to coding?
Add onClick on your button
onClick:"btnClicked"
Like
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/button2"
android:onClick="btnClicked" />
Add these code in Java file
public void btnClicked(View view){
EditText et = (EditText)findViewById(R.id.SearchText);
String value = et.getText().toString();// your edit text value
}
//Variable to store value.
String variable;
//References to the views.
Button btnClick = (Button)findViewById(R.id.button2);
EditText etText = (EditText)findViewById(R.id.SearchText);
//Setting click to the button
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
// Do Your stuff.
variable = etText.getText().toString();
}
});
what the MainActivity.java file should contain as to receive the text value as i am new to coding?
Write This Code in Your MainActivity.java Whatever you will give in Text field and click Button , It will update and show in Toast Message.
public class MainActivity extends Activity {
EditText et;
Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
et = (EditText)findViewById(R.id.SearchText);
bt = (Button)findViewById(R.id.button2);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String eText = et.getText().toString();
Toast.makeText(getApplicationContext(),"Your Text is here"+eText,Toast.LENGTH_SHORT).show();
}
});
}
String YOUR_VARIABLE = "";
Button btn= (Button)findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText edtText = (Edittext)findViewById(R.id.SearchText);
if(!edtText.isEmpty()){
YOUR_VARIABLE = edtText.getText().toString();
}
});
This will store your EditText text in your Variable
if you want to use That variable Globally you should make if public static
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = SearchText.getText().toString();
}
});

How to make multiple buttons open in second activity?

I was working on a different code site before Android studio, and how they made buttons in a second activity open was different than here. So far I have my second activity button and it opens..
My fifthactivity.java is below
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
}
}
I understand I need to make a new .java and a new layout to direct the button, I just need help with the code to put into my fifth activity.java
Below is my layout for the other button that i need to open.
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
Button button;
Button anotherButton; // the second button OP required
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
anotherButton = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this, AmazonActivity.class);
FifthActivity.this.startActivity(intent);
}
});
/* new button to open a new activity */
anotherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// creating the intent
Intent intent = new Intent(FifthActivity.this, AnotherActivity.class);
// starting activity with the created intent
startActivity(intent);
}
});
}
}
Add new Button to your xml file and style it how do you like and add new id like
android:id="#+id/button6" :
<Button
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PlayStation"
android:drawableLeft="#drawable/playstation"
android:drawableStart="#drawable/playstation"
android:layout_weight="0.07"
android:textSize="35sp"
android:id="#+id/button5" />
<Button
android:id="#+id/button6"
tools:ignore="HardcodedText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SomeText"
android:textSize="35sp" />
In your fifthactivity.java add new Button:
Button button, button2;
and, like your previous button, add click listener to that button. Create new Java class with its own layout and open it thru Intent with that button.

"Wiring-up" Buttons to change View in android

Im building an application that requires when you press the button I.E. (search) then it opens the search page, click the (home) button then it goes back to the home view. what I'm missing is the knowledge to make that connection between those two views. this is what the home page looks like in XML.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
//Title
//Search Student Button
<Button
android:id="#+id/button1"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Search Student" />
//New Student Button
<Button
android:id="#+id/button2"
android:layout_width="99dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="New Studetn " />
//Legal Button
<Button
android:id="#+id/button3"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:longClickable="false"
android:text="Legal Info" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Student Registration "
android:textAppearance="?android:attr/textAppearanceLarge" />
some pictures of the application.
http://imgur.com/aVpqUCZ
Button searchStudentButton;
Button newStudentButton;
Button legalButton;
searchStudentButton = (Button) findViewById(R.id.button1);
searchStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, searchStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
newStudentButton = (Button) findViewById(R.id.button2);
newStudentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, newStudentActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
legalButton = (Button) findViewById(R.id.button3);
legalButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent myIntent = new Intent(CurrentActivity.this, legalActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
Don't forget to add your new activity in the AndroidManifest.xml:
<activity android:label="#string/app_name" android:name="searchStudentActivity"/>
<activity android:label="#string/app_name" android:name="newStudentActivity"/>
<activity android:label="#string/app_name" android:name="legalActivity"/>
When dealing with multiple views and/or buttons like you are, I usually prefer using only one instance of onClickListener for all the views, to keep the code cleaner.
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
Button btnSearchStudent = (Button) findViewById(R.id.button1);
Button btnNewStudent = (Button) findViewById(R.id.button2);
Button btnLegalInfo = (Button) findViewById(R.id.button3);
btnSearchStudent.setOnClickListener(this);
btnNewStudent.setOnClickListener(this);
btnLegalInfo.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1: {
Intent intent = new Intent(this, SearchStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button2: {
Intent intent = new Intent(this, NewStudentActivity.class);
startActivity(intent);
break;
}
case R.id.button3: {
Intent intent = new Intent(this, LegalInfoActivity.class);
startActivity(intent);
break;
}
}
}
}
I would recommend that you change the android:id attribute of your buttons to a more meaningful name. This makes it easier to see what you're referencing in the code. I personally prefer prepending my views with an abreviation of the view class such as btn_ for Button and tv_ for TextView. Remember to update your calls to findViewById() and the id's used in the switch statement.
Finally, don't forget to add your activities to the AndroidManifest.xml file as Sagar Pilkhwal posted, if you haven't already.

Categories

Resources