Problem with onClickListener - android

this actually does nothing when i click the button.
The button is like:
Button Confirmar = (Button)findViewById(R.id.btConfirma);
Confirmar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String Login = edLogin.getText().toString();
String Senha = edSenha.getText().toString();
if(Login.length() == 0 || Senha.length() ==0) {
Toast.makeText(getuser.this, "Por favor preencha o login e a senha!", Toast.LENGTH_LONG).show();
return;
}
if (chkKeep.isChecked() && (edLogin.getText().toString() != Settings.getUser() || edSenha.getText().toString() != Settings.getPass())) {
Settings.setUser(edLogin.getText().toString());
Settings.setPass(edSenha.getText().toString());
Settings.setKeepUser(chkKeep.isChecked());
jXML.updateConfigXml();
}
Intent i = getIntent();
Bundle bD = new Bundle();
bD.putStringArray("Login", new String[] {edLogin.getText().toString(), edSenha.getText().toString()});
i.putExtras(bD);
finishActivity(555);
}
});
As asked --> Button XML:
<Button android:layout_width="180dip" android:layout_height="wrap_content" android:id="#+id/btOkLogin" android:text="Confirmar"></Button>
SOLVED: Had to use setResult(ResulCode, Intent) before finish();
Answered by: #Sam-Quest

i guess you have to set the result before calling the finish
...
Intent i = getIntent();
Bundle bD = new Bundle();
bD.putStringArray("Login", new String[] {edLogin.getText().toString(), edSenha.getText().toString()});
i.putExtras(bD);
setResult(RESULT_OK, i);
finishActivity(555);
check this link if you have any doubt. LINK

Put a breakpoint on first line inside onClick listener, run on debug mode and see where it goes on the code.

That's a strange error. I'd try to maybe setOnClickListener(this) and let your activity implement onClick(View). Otherwise you can add android:onClick tag to the xml button object.

Related

Finish Activity not close in Android (Xamarin)

I have Two Activity one is InventoryActivity and Second is StoneDetailActivity. In my InventoryActivity have RecycleView In RecycleView Button Click I start the StoneDetailActivity using StartActivityForResult below code.
Intent stonedetailIntent = new Intent(context, typeof(StoneDetailActivity));
stonedetailIntent.PutExtra("SearchitemObject", stoneJson);
stonedetailIntent.PutExtra("position", position);
context.StartActivityForResult(stonedetailIntent, 1000);
context.OverridePendingTransition(Resource.Animation.Slide_in_right, Resource.Animation.Fade_back);
In StoneDetailActivity Button click I use this code to Finish the current Activity and go to OnBackPressed().
public override void OnBackPressed()
{
Intent intent = new Intent();
intent.PutExtra("BoolCheck", postflag);
intent.PutExtra("Position", position);
SetResult(Result.Ok, intent);
Finish();
}
and In InventoryActivity I have set this code.
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
bool checkflag = data.GetBooleanExtra("BoolCheck", false);
int position = data.GetIntExtra("Position", -1);
if (checkflag && position > -1)
{
searchItems.RemoveAt(position);
inventAdapter.NotifyDataSetChanged();
txt_totalStone.Text = searchItems.Count.ToString();
txt_totalCarat.Text = searchItems.Sum(c => c.Weight.Value).ToString();
txt_totalAmount.Text = searchItems.Sum(c => c.Rate.Value).ToString();
mainActivityBool = true;
badgeCounttextView.Text = BizApplication.BADGE_COUNT.ToString();
}
}
}
Button Click code :
add_to_cart_button.Click += async (sender, e) =>
{
ProgressDialog pdialog = new ProgressDialog(this);
pdialog.SetMessage("Please Wait...");
pdialog.Show();
cartItem = new CartItem();
cartItem.StoneId = searchItem.PacketId;
cartItem.UserId = BizApplication.getCredential().Id;
cartItem.Amount = searchItem.Rate.Value;
cartItem.Discount = searchItem.Discount;
postflag = await InventoryService.AddToCart(cartItem);
if (postflag)
{
OnBackPressed();
BizApplication.BADGE_COUNT += 1;
}
pdialog.Dismiss();
};
this code work fine for first Time. But Again if I do the same process, the StoneDetailActivity set open eventhough if I click finish.
UpDate :
When I full debug my code and i found that when I click on Second time OnBackPressed(). and Finish it my debug again start the OnCreate activity that's why it happening. But I am not starting Again then Why is Happening.
What happen I don't understand. Any Help be Appreciated.
As per this Post the Problem was that inside ListView or RecycleView if we are perform some task like OnclickListener then we have check is OnclickListener like below way other it will fire multiple event.
if (!button.HasOnClickListeners)
{
button.Click += this.clickHandler;
}
Then the code is working fine.
For more detail visit this :
https://forums.xamarin.com/discussion/9244/single-click-on-button-invoking-multiple-clicks
try by adding flag Intent
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);

Check Boxes to Open a Specific Activity

I am developing an App using Eclipse. I have a page where it has different check boxes. I want the user if checking lest say options A and B and D then Activity 7 will open and if the user checks options A and C then Activity 5 will open.
Thank you
You can do so like this:
Get the IDs of the checkboxes. Then add an OnClickListener to the checkboxes like so:
OnClickListener checkBoxListener;
checkBoxListener = new OnClickListener()
{
#Override
public void onClick(View arg0) {
if (checkboxA.isChecked() && checkboxC.isChecked())
{
Intent i = new Intent(this,Activity5.class)
startActivity(i);
}
else if (checkboxA.isChecked() && checkboxB.isChecked() && checkboxD.isChecked())
{
Intent i = new Intent(this,Activity7.class)
startActivity(i);
}
}
};
checkboxA.setOnClickListener(checkBoxListener);
checkboxB.setOnClickListener(checkBoxListener);
checkboxC.setOnClickListener(checkBoxListener);
checkboxD.setOnClickListener(checkBoxListener);
Please give this a try.

comparing two strings in Android ( if-else statemente)

I am working on a login page in an Android App.
As you know, the app must check if the username and password are valid, and then grant the user access to the application.
I have used the following code:
...
EditText un = (EditText) findViewById(R.id.username1);
EditText pw = (EditText) findViewById(R.id.password1);
String u = un.getText().toString();
String p = pw.getText().toString();
//////// Now on the click of the Login Button:
public void onClickL (View view){
if ( (u.equals("Android")) && (p.equals("1234"))) /////// move to a new activity
else ///////Display a warning message: Try again
}
when I run this code this only executes the else part. why its not executing the if part? what should I do ?
The reason is that you are fetching the EditText's value while declaring the EditText. Actually you nee to fetch the Text from EditText while clicking on the button, hence you need to move you code to onClick() method like below,
#Override
public void onClick (View view)
{
String u = un.getText().toString();
String p = pw.getText().toString();
if ( (u.equals("Android")) && (p.equals("1234"))) /////// move to a new activity
{
....
}
else ///////Display a warning message: Try again
{
....
}
}
please try this:
public void onClickL (View view){
u = un.getText().toString();
p = pw.getText().toString();
if ( u.equals("Android") && p.equals("1234") ) /////// move to a new activity
{
}
else ///////Display a warning message: Try again
{
}
}
try Following code
need to clear space in username if it is available.
public void onClick (View view){
String username = un.getText().toString().trim();
String password = pw.getText().toString();
if ((username.equals("Android")) && (password.equals("1234"))) {
//do something
} else{
//do something
}
}
Try this..
get the text inside Click function like below
public void onClick (View view){
String u = un.getText().toString().trim();
String p = pw.getText().toString().trim();
if ((u.equals("Android")) && (p.equals("1234"))) {
//do something
}
else{
//do something
}
}

Android: Error transfer data after click of buttons

Hi there I am new to Android Programming
I am trying to create an application in which, the user clicks button on the first page
the text color in the buttons change color and the change is reflected in another activity page.
To do this I have
1) one fragment class(BookLockerFragment) which reference to an xml file containing the buttons
2) The parent activity file (TabActivity.java)
3) The activity file to reflect the change ( complainResponse.java)
Here is the code:
LodgeComplaintFragment.java
ArrayList<String>userSelectedOptions = new ArrayList<String>();
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS");
}
Button but = (Button) root.findViewById(R.id.searchButton);
.....
but.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
buttonListener.onMakeBookingButtonPressed(userSelectedOptions);
}
});
TabMainActivity.java
public void onMakeBookingButtonPressed(ArrayList<String> list) {
// TODO Auto-generated method stub
Intent intent = new Intent(TabMainActivity.this,
complainResponse.class);
intent.putStringArrayListExtra("userSelectOptions",list);
startActivity(intent);
}
complainResponse.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
setContentView(R.layout.complainresponse);
userInput = intent.getStringArrayListExtra("userSelectOptions");
// Creates the window used for the UI
if (userInput != null) {
if (userInput.get(0) != null) {
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(userInput.get(0));
}
}
}
Error occurs at this line:
if (userInput != null) {
//of complainResponse.java
Logcat:
java.lang.IndexOutOfBoundsException
Please help
There's nothing in the ArrayList that you pass to your activity.
I suspect this bit of code isn't being executed -
if(btnSis.getCurrentTextColor()==Color.BLUE){
userSelectedOptions.add("SIS"); <------------ never gets here
}
To verify this, run the application in debug mode, and place a breakpoint at the if statement
userInput.get(0) != null
this is the cause of error in my opinion, list can be initialized but empty.
instead you should use,
if (!userInput.isEmpty())

How to use .setError

I use .setError function. But it checks the text field only when the save button is clicked, go to SecondActivity and press Back button on device to see it can't be left empty!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_card);
save=(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText Name2 = (EditText)findViewById(R.id.txtName);
if( Name2.getText().toString().length() == 0 )
Name2.setError( "First name is required!" );
Intent intent = new Intent(NewCard.this, Template.class);
startActivity(intent);
}
});
Any idea on how i can check when button is clicked but not proceeding to SecondActivity if Name2 is blank? And beside length(), i also want to check for numbers, undesired characters etc. if possible. Thanks for assistance.
You just need to create an else for your if, then move your call to startActivity() into that like so:
// Expand this condiditional to perform whatever other validation you want
if( Name2.getText().toString().length() == 0 ) {
Name2.setError( "First name is required!" );
} else {
// Validation passed, show next Activity
Intent intent = new Intent(NewCard.this, Template.class);
startActivity(intent);
}

Categories

Resources