So basically, I have 2 buttons: Cancel, and Create Recipe. Clicking both would take me back to my home page.
"Create Recipe" requires some EditTexts beforehand to be filled before moving to the homepage and "Cancel"
"Cancel" works fine. But "Create Recipe" crashes the emulator
Below are my codes
// createRecipe button onClickListener
Button createButton = findViewById(R.id.createButton);
createButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Checks if there are empty fields
if (isEmpty(etRecipeName, etDuration, etIngredient, etDesc) == false) {
Toast.makeText(MainActivity.this, "There are empty fields. Please fill up all fields",
Toast.LENGTH_SHORT).show();
}
// Create the recipe
else {
createRecipe(etRecipeName, etDuration, etIngredient, etDesc);
Toast.makeText(MainActivity.this, "Recipe Created", Toast.LENGTH_SHORT).show();
Intent changePage = new Intent(MainActivity.this, afterCancel.class);
startActivity(changePage);
}
}
});
// Cancel button onClickListener
Button cancelButton = findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent changePage = new Intent(MainActivity.this, afterCancel.class);
startActivity(changePage);
}
});
For my home page, its just a
this.getintent()
If you want to share 2 data from the first to second activity then use this method
for sharing intent
createButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Checks if there are empty fields
if (isEmpty(etRecipeName, etDuration, etIngredient, etDesc) == false) {
Toast.makeText(MainActivity.this, "There are empty fields. Please fill up all fields",
Toast.LENGTH_SHORT).show();
}
// Create the recipe
else {
createRecipe(etRecipeName, etDuration, etIngredient, etDesc);
Toast.makeText(MainActivity.this, "Recipe Created", Toast.LENGTH_SHORT).show();
Intent changePage = new Intent(MainActivity.this, afterCancel.class); // is this afterCancel.class your recipe activity?
changePage.putExtra("first", firstData); // put here first data
changePage.putExtra("second", secondData); // put here second data or string
startActivity(changePage);
}
}
});
for getting these data in afterCancel.java
String yourFirstData = getIntent().getStringExtra("first");
String yourSecondData = getIntent().getStringExtra("second");
Toast.makeText(this, yourFirstData, Toast.LENGTH_SHORT).show();
Toast.makeText(this, yourSecondData, Toast.LENGTH_SHORT).show();
code for cancel button
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish(); // use this.
}
});
Hope this helps
Related
iam already did add bookmark in the firebase but i dont know how can i get it
reference = FirebaseDatabase.getInstance().getReference("pagenumber");
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pagenumber = pdfView.getCurrentPage();
reference.setValue(pagenumber);
if (true) {
Toast.makeText(getApplicationContext(), "تم حفظ العلامة", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "لم يتم حفظ العلامة", Toast.LENGTH_SHORT).show();
}
}
});
load.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
There are 5 items in the array list. If the user clicks item 1 program will show item 1 and if the user clicks item 2 program will show item 2 and so on. The problem is users have to click the button twice for their first-time action unless data is not retrieved from the program. I don't know why. I have no idea which part cause this problem. I would really apricate your help if you can point out the cause of the problem.
Here is my code
//start here to retrieve fav books
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Wishlist");
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
List<String> wishlist_item = new ArrayList<String>();
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
wishlist_item.add(postSnapshot.getValue().toString());
}
//list 1
L1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(wishlist_item.size()>=1){
Intent launchWish = new Intent(wish_list.this, wishlist_webview.class);
String a = wishlist_item.get(0);
launchWish.putExtra("k",a);
startActivity(launchWish);
//animation
overridePendingTransition(R.anim.slide_up_in,R.anim.slide_up_out);
}else{
Toast.makeText(wish_list.this, "Favourite 1 is empty", Toast.LENGTH_SHORT).show();
}
}
});
//list 2
L2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(wishlist_item.size()>=2){
Intent launchWish = new Intent(wish_list.this, wishlist_webview.class);
String b = wishlist_item.get(1);
launchWish.putExtra("k",b);
startActivity(launchWish);
//animation
overridePendingTransition(R.anim.slide_up_in,R.anim.slide_up_out);
}else{
Toast.makeText(wish_list.this, "Favourite 2 is empty", Toast.LENGTH_SHORT).show();
}
}
});
//list 3
L3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(wishlist_item.size()>=3){
Intent launchWish = new Intent(wish_list.this, wishlist_webview.class);
String c = wishlist_item.get(2);
launchWish.putExtra("k",c);
startActivity(launchWish);
//animation
overridePendingTransition(R.anim.slide_up_in,R.anim.slide_up_out);
}else{
Toast.makeText(wish_list.this, "Favourite 3 is empty", Toast.LENGTH_SHORT).show();
}
}
});
//list 4
L4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(wishlist_item.size()>=4){
Intent launchWish = new Intent(wish_list.this, wishlist_webview.class);
String d = wishlist_item.get(3);
launchWish.putExtra("k",d);
startActivity(launchWish);
//animation
overridePendingTransition(R.anim.slide_up_in,R.anim.slide_up_out);
}else{
Toast.makeText(wish_list.this, "Favourite 4 is empty", Toast.LENGTH_SHORT).show();
}
}
});
//list 5
L5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(wishlist_item.size()>=5){
Intent launchWish = new Intent(wish_list.this, wishlist_webview.class);
String e = wishlist_item.get(4);
launchWish.putExtra("k",e);
startActivity(launchWish);
//animation
overridePendingTransition(R.anim.slide_up_in,R.anim.slide_up_out);
}else{
Toast.makeText(wish_list.this, "Favourite 5 is empty", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
I guess you need to set the onClickListeners of your button outside the onDataChange method because the onDataChange will be triggered whenever there is a change in data. You also need to make List<String> wishlist_item as an instance variable of the class and repopulate the list every time there is a data change (depends on your business case)
In the current approach there is some time taken to load the data from the server and once the onDataChange is triggered only then the the listeners of the button are set and this could be the reason why nothing happens on the first click.
I guess all your buttons ranging L1..L5 are visible by default, and FirebaseDatabase calls are asynchronous meaning it is fetching data from remote and will take time for IO, so it will take some time to get the response, my best bet would be showing some kind of LOADING state by hiding buttons initially and when you are setting ClickListeners to buttons make them VISIBLE.
I am making a login page for an app and I am making a login screen for the app. But, my login is not working at all.
So, as far as I am concerned, my code is pretty okay. But why is not doing anything when pressing the Login button. What is the error? No toasts or any other pages open up, even when I put in the correct username and password!
public class MainPage extends AppCompatActivity {
public EditText uName, pass;
Button loginBtn;
//ImageView myImageView = (ImageView) findViewById(R.id.mybackground);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
setTitle("Q-GAX");
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
loginBtn = (Button) findViewById(R.id.login_button);
uName = (EditText) findViewById(R.id.uName);
pass = (EditText) findViewById(R.id.pass);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (uName.getText().toString().equals("admin") && pass.getText().toString().equals("pass")) {
Intent myIntent = new Intent(v.getContext(),
TasksPanel.class);
startActivityForResult(myIntent, 0);
finish();
} else {
Toast.makeText(getApplicationContext(), "Incorrect Username or Password", Toast.LENGTH_SHORT);
}
}
});
//myImageView.setBackgroundResource(R.drawable.progressanimation);
//AnimationDrawable frameAnimation = (AnimationDrawable) myImageView.getBackground();
// Start the animation (looped playback by default).
//frameAnimation.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.leaveMode) {
Toast.makeText(getApplicationContext(), "Leave Mode", Toast.LENGTH_SHORT).show();
}
if (id == R.id.officeMode) {
Toast.makeText(getApplicationContext(), "Office Mode", Toast.LENGTH_SHORT).show();
}
if (id == R.id.meetingMode) {
Toast.makeText(getApplicationContext(), "Meeting Mode", Toast.LENGTH_SHORT).show();
}
if (id == R.id.faq) {
Toast.makeText(getApplicationContext(), "App FAQ", Toast.LENGTH_SHORT).show();
}
return true;
}
}
You should add .show(). Read Toast.
Toast.makeText(context, text, duration).show();
if (uName.getText().toString().equals("admin") && pass.getText().toString().equals("pass")) {
Intent myIntent = new Intent(MainPage.this, TasksPanel.class);
startActivity(myIntent);
finish();
} else {
Toast.makeText(getApplicationContext(), "Incorrect Username or Password", Toast.LENGTH_SHORT).show();
}
#Override
public void onClick(View v)
{
if(uName.getText().toString().equals("admin") && pass.getText().toString().equals("pass"))
{
Intent myIntent = new Intent(MainPage.this, TasksPanel.class);
startActivity(myIntent);
finish();
}
else
{
Toast.makeText(getApplicationContext(),"Incorrect Username or Password",Toast.LENGTH_SHORT).show();
}
}
So I basically want to know how would I allow the user to proceed to the next page when they hit pay if either txtCode or StreetCode is filled, only one of these two needs to be filled in order to pass to the next page but how would i get that to work as currently they both must filled to pass to the next page.
btnPay = (Button)findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText)findViewById(R.id.txtReg);
txtCode = (EditText)findViewById(R.id.txtCode);
txtStreetName = (EditText)findViewById(R.id.txtStreetName);
dlCostTime = (Spinner)findViewById(R.id.dlCostTime);
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
Replace this code
if( txtReg.getText().toString().trim().equals(""))
{
txtReg.setError("required!");
}
if( txtCode.getText().toString().trim().equals(""))
{
txtCode.setError("required!");
}
if( txtStreetName.getText().toString().trim().equals(""))
{
txtStreetName.setError("required!");
}
else{
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
}
with this
if( txtReg.getText().toString().trim().equals("")){
txtReg.setError("required!");
}else{
if((txtCode.getText().toString().trim().equals("") && !txtStreetName.getText().toString().trim().equals("")) ||
(!txtCode.getText().toString().trim().equals("") && txtStreetName.getText().toString().trim().equals(""))){
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}else{
//show message enter either code or street name not both
}
Replace this code
btnPay = (Button) findViewById(R.id.btnPay);/** REFERENCE THE PAY BUTTON*/
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {/** SETS ONCLICKLISTENER TO LISTEN FOR BUTTON CLICK*/
/** REFERENCES ALL VARIABLE TO FIELDS IN LAYOUT */
txtReg = (EditText) findViewById(R.id.txtReg);
txtCode = (EditText) findViewById(R.id.txtCode);
txtStreetName = (EditText) findViewById(R.id.txtStreetName);
dlCostTime = (Spinner) findViewById(R.id.dlCostTime);
if (txtCode.getText().toString().trim().length() > 0 || txtStreetName.getText().toString().trim().length() > 0) {
final Button btnPay = (Button) findViewById(R.id.btnPay);
btnPay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent payIntent = new Intent(UserAreaActivity.this, PaymentActivity.class);// creates intent to open payment details
UserAreaActivity.this.startActivity(payIntent);//Performs intent to open payment page
}
});
} else if (txtStreetName.getText().toString().trim().length() == 0) {
txtStreetName.setError("required!");
} else if (txtReg.getText().toString().trim().length() == 0) {
txtReg.setError("required!");
} else if (txtCode.getText().toString().trim().length() == 0) {
txtCode.setError("required!");
}
}
}
I'm Developing an android app in which there is a Login activity and a Questionnaire activity contains Questions.So now I wanted to make the user to access the app only by entering the unique id which will help in blocking unauthorized users. I tried with the code but when i press the login button it pops the alert dialogue even if the password is matching the password I've given. And the password i wanted to keep is 'VISTEON'. If the password entered is rite then it should intent to the next activity.
check my java code and help me to modify it.efforts will be really appreciated.
Thanks in Advance.
Here is my java code
public class Klogin extends Activity {
EditText editText1;
Button login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_login);
addButtonListener();
}
public void addButtonListener()
{
Button login=(Button)findViewById(R.id.button1);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(validationSuccess()){
Intent intent = new Intent(Klogin.this, Login.class);
startActivity(intent);
}
}
});
}
private Boolean validationSuccess()
{
EditText visteon = null;
if(editText1==visteon)
{
alertDialog();
return false;
}
return true;
}
private void alertDialog()
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Klogin.this);
alertDialogBuilder.setMessage("Please ENTER the Correct password").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Try this way first initialized EditText edit_password like
EditText edit_password = (EditText)findViewById(R.id.edit_password);
Now, create validationSuccess(......) like
private Boolean validationSuccess(String str)
{
if(!str.equals("yourcorrectpassword"))
{
alertDialog();
return false;
}
return true;
}
Now called this function like
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(validationSuccess(edit_password.getText.toString())){
Intent intent = new Intent(Klogin.this, Login.class);
startActivity(intent);
}
}
});
And make sure you have EditText with id edit_password in your activity_main_login.xml layout file.
Try this..
public void addButtonListener()
{
Button login=(Button)findViewById(R.id.button1);
editText1=(EditText)findViewById(R.id.editText1);
login.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(validationSuccess(editText1.getText().toString().trim())){
Intent intent = new Intent(Klogin.this, Login.class);
startActivity(intent);
}
}
});
}
and
private Boolean validationSuccess(String value)
{
if(!value.equals("VISTEON"))
{
alertDialog();
return false;
}
return true;
}
For start you are not setting the visteon variable. This only needs to be a String.
Then when you user enters the value, you need to get the String from the editText1 and then check using equals as to whether it is correct or not. The == operator will check for equality of objects, not their values.
What you want is if(editText1.getText().toString().equals("VISTEON")).