reset button in signup page is not working in android - android

I tried to make a signup page in android where I use a reset button that should clear all fields in the page. Please see the code below and correct it as my code is not working.
Button btnreset = (Button) findViewById(R.id.btnreset);
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
this is an signup page and fields are first name,last name,user id,password.when user click on reset button it clear all the fields who's filled the user.I'm give a complete source code to you please check this:
package com.boyzcorn.android.fyp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class signup extends Activity{
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.signup);
Button b = (Button) findViewById(R.id.btnClick2);
Button btnreset = (Button) findViewById(R.id.btnreset);
final EditText eText1 = (EditText)findViewById(R.id.firstname);
final EditText eText2 = (EditText)findViewById(R.id.lastname);
final EditText eText3 = (EditText)findViewById(R.id.userid);
final EditText eText4 = (EditText)findViewById(R.id.password);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
{
if(eText1.getText().toString().equals("") ||eText2.getText().toString().equals("") || eText3.getText().toString().equals("") ||eText4.getText().toString().equals(""))
{
Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
}
else
{
Intent i = new Intent(signup.this,login.class);
startActivity(i);
}
}
}
});
}
btnreset.setOnClickListener(new View.OnClickListener() {
public void restartActivity(Activity act){
Intent intent=new Intent();
act.finish();
intent.setClass(act, act.getClass());
act.startActivity(intent);
}
}
public void onClick(View arg0) {
}
}

You've only defined the function; you're not calling it. You will need to execute restartActivity(signup.this) from the OnClickListener.
Also, your intent will likely not execute, because its parent has been finished. Perhaps rearranging the lines of code might help, but a better solution would be having the parent activity start it. Try replacing the click listener with:
btnreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Activity act = signup.this;
Intent intent = new Intent(act, act.getClass());
act.startActivity(intent);
act.finish();
}
}

Related

Nothing happens when I click on Button except when the EditText is empty

am new in android and facing a problem in edit text view need your help. I just want to check EditText before going on next intent. If EditText is filled by at least 1 String then it will go to next intent. May be this is simple for you to implement but this is to difficult for me and I wasted half day on this. Is anyone here which can tell me where is the mistake in this code.
Thank you in advance
package com.example.tricknearn;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Posttittle extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posttittle);
final EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Button postTittleButton = (Button) findViewById(R.id.postTittleButton);
postTittleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tittleInput.getText().length() == 0) {
tittleInput.setError("Please enter some text");
}
}
});
}
public void postTittleClick(View view) {
EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Intent i = new Intent(this, Postdescription.class);
if (tittleInput.getText().toString().equals("")){
}else{
startActivity(i);
}
}
}
Try this code instead...
public void postTittleClick(View view) {
EditText tittleInput = (EditText) findViewById(R.id.tittleInput);
Intent i = new Intent(this, Postdescription.class);
if (tittleInput.getText().toString().isEmpty()){
// Here you can place code while edit text is empty
}else{
startActivity(i);
}
}
postTittleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (tittleInput.getText().toString().isEmptry()) {
//edittext is empty
tittleInput.setError("Please enter some text");
} else {
//Edittext is not empty
//start new Activity
}
}
});
And remove postTittleClick method
Replace if (tittleInput.getText().length() == 0) with if (tittleInput.getText().toString.equals(""))

Connecting Two buttons in one activity to two other activities

I'm trying to make a simple math game with two modes, addition and subtraction. I figured out how to create a button that will link the "Addition button" to the addition activity but I can't seem to figure out how to create a second "Subtraction Button" that will link to the subtraction activity. Here's my broken code:
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Your code just have some syntax error which is solved as per given code...
package com.example.kirky_000.madmath;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.content.Intent;
public class MainMenu extends ActionBarActivity {
Button button;
Button button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
}
Your second instruction to add listener is inside the first OnClickListener.
So the listener is never added to the second button. You code should be like this :
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
});
}
You need to have different click listeners for different buttons. Right now, you're putting the click listener for the second button inside the click listener for the first button. Separate them like this..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
)};
Some points before the solution (those will help you for further coding).
Declare context after buttons declaration : So after Button
button, button2, write :
final Context context;
Always keep in mind : Always initialize objects in onCreate()
method. So in OnCreate() after
setContentView(R.layout.activity_main_menu);, write :
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
//then define context
context = MainMenu.this;
//or context = getApplicationContext();
Now addListenerOnButton() function will be like this (just replace
addListenerOnButton() with following code):
public void addListenerOnButton(){
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
});
}
Now what is the wong in your code ?
Your onClick listener code is wrong...
enjoy the coding :) (and android :))...
you can even do like this
public void mainClickHandler(View v)
{
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(context, Addition.class);
startActivity(intent);
case R.id.button2:
Intent intent = new Intent(context, Subtraction.class);
startActivity(intent);
}
}

Only one of three buttons working

Hey I am trying to get different buttons to open up different pages in a android project but only on of the buttons is opening up a new page.
I am new to programming so my terminology may not be correct but I was following a youtube tutorial and it showed how to create a button and make it open up a new page. I tried to do this for multiple buttons but I think I am making the mistake in the main activity. Sorry if I haven't provided the write information to help me solve the problem.
package test.activity.today;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(myIntent);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.question_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Question.class);
v.getContext().startActivity(myIntent);
}
});
}
public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.owner_cost);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Owner.class);
v.getContext().startActivity(myIntent);
}
});
}
}
you should only have one onCreate() method.. check android activity's life cycle to understand it
package test.activity.today;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityTutorialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.next_button);
next.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
v.getContext().startActivity(myIntent);
}
});
Button question = (Button) findViewById(R.id.question_button);
question.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Question.class);
v.getContext().startActivity(myIntent);
}
});
Button ownerCost = (Button) findViewById(R.id.owner_cost);
ownerCost.setOnClickListener(new OnClickListener(){
public void onClick (View v){
Intent myIntent = new Intent(v.getContext(), Owner.class);
v.getContext().startActivity(myIntent);
}
});
}
}
You are duplicating the onCreate() method... this method is called natively by Android and, thus, none of your other methods will get called (you'd have other problems if they were). To create more than one button, you need to add new buttons to your layout and then add them to onCreate().
There is another way to implement the onClick.
In your layout you can specify the function to call onClick
<ImageButton
android:id="#+id/imageButtonNext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#color/back_color"
android:onClick="RegistraterCompanyOnClick"
android:src="#drawable/ic_next" />
Then in your activity you can filter which button by it's ID. See below.
public void RegistraterCompanyOnClick(View v){
switch(v.getId()){
case R.id.imageButtonNext1:
String cname = company.getText().toString();
if (cname.length()== 0){
message = getString(R.string.company_required);
ShowDialog(message);
}
else{
company_name = company.getText().toString();
VerifyClient(company_name);
}
break;
case R.id.imageButtonInfo1:
//message = getString(R.string.registration_info);
message = "Device ID:\n" + deviceID;
// TODO Auto-generated method stub
ShowDialog(message);
break;
case R.id.imageButtonHelp1:
message = getString(R.string.registration_contact);
// TODO Auto-generated method stub
ShowDialog(message);
break;
case R.id.imageButtonPrevious1:
Intent resultIntent = new Intent();
// TODO Auto-generated method stub
resultIntent.putExtra("company_name", company.getText().toString());
resultIntent.putExtra("company_id", companyID);
resultIntent.putExtra("location_name", location_name);
resultIntent.putExtra("location_id", locationID);
setResult(Activity.RESULT_CANCELED, resultIntent);
finish();
default:
break;
}
}

Displaying Another Screen Onclick in Android

package com.example.example;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.activity_main);
this.btn = (Button)this.findViewById(R.id.button);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
}
}
I don't get any errors however my program also dont run as I want. I am new in android and I want to change the screen after the button is clicked for that I am using two classes so in one class my program should invoke another one onclick. How can I do this ? My code is as above.
First remove this.setContentView(R.layout.activity_main); because you declared it twice . Then declare
btn = (Button)this.findViewById(R.id.button);
after
setContentView(R.layout.activity_main);
Declare your profile activity in manifest file.Check my code below.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
1) your oncreate method should look like below one.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, profile.class);
startActivity(intent);
}
});
2) Add profile Activity into Manifest.
3) add logs in onClick method so you should know if it is getting called on not.
Happy coding!!

How to restore values of a activity when i move to next activity and come back again?

I want the values of edit text restored when user comes back to my first activity?
Please help me out.
Thanks in advance
this is my first activity code for getting user values in edit text
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
/** Called when the activity is first created. */
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
ed1_val = Float.parseFloat(ed1.getText().toString());
ed2_val = Float.parseFloat(ed2.getText().toString());
Log.v("TAG", "inside saved instance");
savedInstanceState.putFloat("ed1", +ed1_val);
savedInstanceState.putFloat("ed2", +ed2_val);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v("TAG", "inside on restore");
float ed_val = savedInstanceState.getFloat("ed1");
float ed2_val = savedInstanceState.getFloat("ed2");
ed1.setText("" + ed_val);
ed2.setText("" + ed2_val);
}
}
this is my second activity code
public class Second_activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
IntentActivity.class);
startActivity(intent);
}
});
}
}
It is not a good idea to start the first activity again on back pressed. Call finish() in the second activity. This will lead to the resume of the first activity which is what you need.
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish(); }
});
}
You don't need neither onSaveInstanceState nor onRestoreInstanceState.
Just call finish in the onClick listener for the button in the second Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivity(intent);
}
});
}
}
This is the second one:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
That way you are resuming the previous Activity instead of starting new one.
If you need to pass data between them you could use startActivityForResult / onActivityResult and setResult methods:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class IntentActivity extends Activity {
private static final int GET_VALUES_REQUEST_ID = 1;
public static final String FIRST_VALUE_ID = "first_value_id";
public static final String SECOND_VALUE_ID = "second_value_id";
private static final float DEFAULT_VALUE = 0;
EditText ed1, ed2;
float ed1_val, ed2_val;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText) findViewById(R.id.editText1);
ed2 = (EditText) findViewById(R.id.editText2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
Second_activity.class);
startActivityForResult(intent, GET_VALUES_REQUEST_ID);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case GET_VALUES_REQUEST_ID: {
if (Activity.RESULT_OK == resultCode) {
ed1_val = data.getFloatExtra(FIRST_VALUE_ID, DEFAULT_VALUE);
ed2_val = data.getFloatExtra(SECOND_VALUE_ID, DEFAULT_VALUE);
setValues();
}
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
protected void setValues() {
ed1.setText(Float.toString(ed1_val));
ed2.setText(Float.toString(ed2_val));
}
}
The second activity could be something like that:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Second_activity extends Activity {
// TODO Auto-generated method stub
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_xml);
Button back = (Button) findViewById(R.id.button1);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent data = new Intent();
data.putExtra(IntentActivity.FIRST_VALUE_ID, 324f);
data.putExtra(IntentActivity.SECOND_VALUE_ID, 32234f);
setResult(Activity.RESULT_OK, data);
finish();
}
});
}
}
This is a very basic example so I just hardcoded some return values - please implement something more meaningful.
Beside that you could avoid using underscores as word separator in class names - camel case is much more accepted as name convention.
just finish the second activity don't start it via intent.
When you finish the second activity first activity will be automatically resumed,
you can go for overriding protected void onSaveInstanceState(Bundle outState) and protected void onRestoreInstanceState(Bundle savedInstanceState)
Here is an example
Remember it will work when you will not finish your previous activity.Do this in your first activity.
You are starting a new activity by pressing back button on Second Activity. The new activity instance is not the previous one that has started the Second Activity hence onRestoreInstanceState callback is not getting called.
Its easy if you do not need to send data back to the first activity. How do send back data without using an intent.

Categories

Resources