I am new to coding.I want to code
in order to go to second activity using a button,
an editText and a pasdword in case it maches.
But it does n`t work.Please help me.Following is my code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eText= (EditText)findViewById(R.id.mainEditText1);
String myCode=eText.getText().toString();
Button nextPage= (Button)findViewById(R.id.mainButton1);
if(myCode.equals("Titan")){
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
startActivity(new Intent(MainActivity.
this,actor.class));
}
});
}
}
}
Move your condition and getText() call in listener
nextPage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan")){
startActivity(new Intent(MainActivity.
this,actor.class));
}
}
}
So, here's what's happening:
You have an EditText field in your app, where the user types their password.
You capture that EditText in your code using EditText eText = ...
You then check to see if eText.equals("Titan");
If it matches Titan, you see the onClick listener.
But, when this code is ran, the EditText is always empty, so you never set the onClick listener!
The fix is fairly straightforward:
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
String myCode=eText.getText().toString();
if(myCode.equals("Titan")) {
startActivity(new Intent(MainActivity.this,actor.class));
}
}
});
Now, when the button is clicked:
You capture the value of eText as myCode.
You check if myCode is equal to Titan
If true (ie it matches), you then run startActivity(new Intent(MainActivity.this,actor.class));
You have written class name actor.class starting with a smaller case. Make it starts with uppercase Actor.class. Bellow is the answer to your questions.
Button nextPage = (Button)findViewById(R.id.mainButton1);
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan"))
{
startActivity(new Intent(MainActivity.
this,Actor.class));
}else{
//password not matching
}
});
Related
So I created a pop-up dialog window to serve as the tutorial for the user, I decided to run tests on it since it was working well but saw that when you added an active button to it, it would not work. Each time I pressed the button that brings up the tutorial(which worked before the addition of the button code), the addition I made with the button taking the user to another part now renders that button inactive and instead takes the user back to the start screen when the tutorial button is pressed.
This is the code I used for it:
package com.example.rockpaperscissors;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import static com.example.rockpaperscissors.R.id.tutorialButton;
public class MainMenu extends AppCompatActivity {
Button tutorialButton, playGameButton, testbutton;
Dialog tutorial_popup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tutorial_popup = new Dialog(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu);
playGameButton = findViewById(R.id.playGameButton);
tutorialButton = findViewById(R.id.tutorialButton);
testbutton = findViewById(R.id.testbutton);
tutorial_popup = new Dialog(this);
playGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
testbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
});
}
}
As you can see, there's another setOnClickListener within the tutorial which is basically supposed to take the user to another class/activity when pressed on. However, it does not do it and instead just ignores it, showing that there is an error.
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
testbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
});
Is there something I missed when doing this? Please let me know how I can fix this.
Try setting the onclick on the button before showing the pop up
testbutton.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) {startActivity(new Intent(MainMenu.this, MainGame.class));}}); tutorial_popup.show();
So I found a solution to this answer, being that there was actually no need to add a buttonListener to the pop-up dialog as not only was it unnecessary, but it was also breaking the code. With how pop-up dialogs work in Android Studio, I realised through watching videos that you are able to click off of it without needing an "X" button or whatsoever, so I removed the buttonListener and tried again. Safe to say it ran perfectly this time with no problems whatsoever.
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
}
});
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(""))
I am trying to move from one activity to another activity by clicking to the ImageButton. But when I click to button, it doesn't move to the activity which I specify in the code, and even it does not throw an error. I'm not getting where is problem
Here is my code which calls next activity :
package com.birthdayreminder;
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;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
ImageButton view, add, edit;
TextView tvadd, tvedit, tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
}
public void innicialize() {
// assigning buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// assign textview
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
I have also declared the class name in the manifest.xml but it does not working
Logcat Log file : 5 lines of last logcat file after clicking the button :
05-15 18:19:25.495: W/AudioFlinger(33): write blocked for 69 msecs, 1245 delayed
writes, thread 0xc658
05-15 18:19:28.964: I/ActivityManager(60): Starting: Intent {
act=com.birthdayreminder.REMINDER cmp=com.birthdayreminder/.Reminder } from pid 548
05-15 18:19:29.409: I/ActivityManager(60): Displayed
com.birthdayreminder/.Reminder: +426ms
try using getApplicationContext() in place of Remainder.this
check if you have a click listener on you imagebutton, you have declared what to do on click, but if you don't have any listener on the imagebutton, it won't work anyway.
You can try something like this :
yourImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reminder.this, Addreminder.class);
startActivity(intent);
}
});
After doing this, you must have declared in your XML file the activity
<activity android:name="packageName.className" />
Finally, check if the new class is working well too but it must be ok!
==================================================================================
You mustn't declare a function or procedure in a function (like you did in onCreate), you can simply do what you do in initialize in your onCreate function like this :
package com.birthdayreminder;
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;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
private ImageButton view;
private ImageButton add;
private ImageButton edit;
private TextView tvadd;
private TextView tvedit;
private TextView tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
// Instantiating Buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// Instantiating Views
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
// Adding a clickListener on add button
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
// Adding a clickListener on edit button
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
// Adding a clickListener on view button
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
If you still have a problem, check your XML files, and look if you have well declared your buttons.
Problem Fixed: Never define a function in a function, because everytime you call the first function you'll define again the second function.
What you did which is wrong :
onCreate { //WRONG VERSION
bla bla bla bla
initialisation {
bla bla bla
}
}
What you have to do :
onCreate { //GOOD VERSION
initialisation();
bla bla bla
}
initialisation {
bla bla bla
}
you can call a function in a function, but you can't define a function in a function.
Moreover, you never call your initialisation function, you must call it now in your onCreate function or nothing will happen.
have you set onClickListener on add button.
btnAdd.setOnClickListener(this);
also check the
is your v.getId() and case R.id.bAdd: same?
debug and check that your case R.id.bAdd: is getting executed..
Seperately initialize the buttons in the first activity as
Button bAddbutton= (Button) findViewById(R.id.bAdd);
Button bEditBtn= (Button) findViewById(R.id.bEdit);
Button bViewBtn= (Button) findViewById(R.id.bView);
Then add seperate onClick listeners for each button as given below,
bAddbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
bEditBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
bViewBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
initialize the button and add listener first in oncreate()
Button btnAdd = (Button) findViewById(R.id.bAdd);
btnAdd.setOnClickListener(this);
I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. Here is my activity:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class test extends Activity {
private Button btnChangeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salesticketoilui);
mainProgram();
}
public void mainProgram() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
} // end onClick
}); // end setOnClickListener
Button buttonExit = (Button)findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitActivity();
} // end onClick
}); // end OnClickListener
// setup button listener for saving data and exit to main
Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
buttonSaveExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveExit();
} // end onClick
}); // end OnClickListener
} // end MainProgram ()
public void saveExit() {
// does stuff
}
public void exitActivity () {
// does stuff
}
} // end class
any thoughts?
Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. Either call mainProgram from onCreate or just put that code directly into onCreate.
I believe the onClickListeners need to go inside of your onCreate method.
Listen to Scott
and looks like your missing the #Override
new View.OnClickListener() {
#Override
public void onClick(View view) {
exitActivity();
} // end onClick
}
Make sure your java settings are to 1.6 to avoid code completion missing this.
I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste.
To make it work, I had to create new buttons in my layout and declare the events manually. Weird!
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();
}
}