i have activity and want to connect it through JDBC the connection is working fine but when i click the ok button as previous connection discard and app crash but when i click ok button with wrong values and toast show message for correct values then that connection string runs and activity would be in override state.
i just want to get the solution that how can i fix that override issue as i can get the result and call the function without calling the connection string again and again because it takes time to connect and when click on button without input mistake app crash.
here is the code:
i want that the connection string works without clicking the button and no affect of override. please help
dbcon=DBConnection.instance(this);
final TextView voternumber=(TextView)findViewById(R.id.txt);
final EditText ed=(EditText)findViewById(R.id.vnumberedit);
final ImageView vok=(ImageView)findViewById(R.id.vnumberok);
dbcon.connect("ip:1433", "pass", "login", "db");
vok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dbcon.connect("ip:1433", "pass", "login", "db");
final String st=ed.getText().toString();
int len=st.length();
if(TextUtils.isEmpty(st)){
Toast.makeText(getApplicationContext(), "Provide Input", Toast.LENGTH_SHORT).show();
}
else{
String id=dbcon.GetID(st,);
txt.setText(id);
}
}
});
Related
I am creating an android application wherein I have mentioned the phone number, email, website of the business similar to the screenshot of google maps as attached.
Just like in the g maps where
Single click makes an intent to the respective app (opens dialer with phone number copied) etc.
Long Click/Press & hold will copy the number to clipboard displaying a toast that "Phone number is copied to clicpboard".
Now I have set up the intents properly and they are working fine --> step-1 achieved.
When I try to display the toast using "OnLongClickListener" method it is taking quite some time to display the toast message. Is there any way we can speed this up or am I missing something or should I use some other alternative like onTouch() method?
(Stuck with this, yet to learn and try out copying to clipboard thing. Inputs in this regards are also welcome!)
In the g maps app, the toast message appears almost instantly on press & hold. So the same is to be achieved.
The code extract from MainActivity.java (The website, phone number, email id are text views with drawableLeft. And I found out that we cannot use setOnItemLongClick on TextView as it is only for ListView)
// This method is called when the website text view is clicked and opens the compnay website
public void openWebsite(View v) {
TextView textView = (TextView) findViewById(R.id.webURL_text_view);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openWebsiteIntent = new Intent(Intent.ACTION_VIEW);
openWebsiteIntent.setData(Uri.parse("https://www.company.com"));
if (openWebsiteIntent.resolveActivity(getPackageManager()) != null) {
startActivity(openWebsiteIntent);
}
}
});
textView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
showMessage();
return true;
}
});
}
public void showMessage(){
Toast.makeText(this,"Information copied to Clipboard", Toast.LENGTH_LONG).show();
}
I am new to android development and just trying out something, so please be as elaborative as possible. Thank You.
So I have stored password as hash in shared preferences, when user puts password I need to make a hash of it and compare with stored one.
Should it be done in AsyncTask or Thread because calculation and comparison could freeze UI? And then do you know a clean way to recieve result (true, false) from asynctask or thread?
public void startGenerateCode(View view) {
String pinCompare = pin; //pin is class variable obtained from editText
pinCompare = tools.bin2hex(tools.getHash(pinCompare));
if(pinCompare.compareTo(session.getDetails("Pin"))==0){
generateCode();
}
else
Toast.makeText(this, "Wrong PIN", Toast.LENGTH_SHORT).show();
}
public void generateCode(){
Intent i = new Intent(this, GeneratedCode.class);
startActivity(i);
overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
finish();
}
This is done in activity after button is pressed.
Zolo,
I guess this process is triggered when someone presses a Button, such as login. I don't think you need any extra Thread to process the Hash calculus.
If you then have to connect to a server and send/receive data, then you should use it due to the asynchronous flow.
Response to comments on main post:
Yes, you can start an Activity in onPostExecute.
Code example:
public void startGenerateCode(View view) {
// Disable button
Button button = (Butto) view;
button.setEnabled(false);
String pinCompare = pin; //pin is class variable obtained from editText
pinCompare = tools.bin2hex(tools.getHash(pinCompare));
if(pinCompare.compareTo(session.getDetails("Pin"))==0){
generateCode();
} else {
// If the login fails, re-enable the button to try again
button.setEnabled(true);
Toast.makeText(this, "Wrong PIN", Toast.LENGTH_SHORT).show();
}
}
I did it by heart, so there may be mistakes.
I am getting an error when I set the counter to subtract and close the application. I get an error "cannot assign value to final variable counter". If the user logins in 3 times with no success quit the application.
final int counter = 3;
//Set the OKButton to accept onClick
OKButton.setOnClickListener(new View.OnClickListener() {
#Override
//once onClick is initalized it takes user to page menu
public void onClick(View v) {
//display text that was inputed for userText and passText
user = userText.getText().toString();
pass = passText.getText().toString();
//create if loop which checks if user and pass equals the credentials
if (user.equals("pshivam") && pass.equals("Bway.857661")) {
//display toast access welcome
String welcome = "Access Granted.";
//Create a Toast to display the welcome string in the MainActivity.
Toast.makeText(MainActivity.this, welcome, Toast.LENGTH_SHORT).show();
setContentView(R.layout.account_main);
}
//create else if loop which checks if user or pass does not equals the credentials
else if (!user.equals("pshivam") || !pass.equals("Bway.857661")){
//displays previous entry
userText.setText(user);
passText.setText(pass);
//allows user to re-enter credentials.
user = userText.getText().toString();
pass = passText.getText().toString();
//display toast access fail
String fail = "Access Denied! Please Try again.";
//Create a Toast to display the fail string in the MainActivity.
Toast.makeText(MainActivity.this, fail, Toast.LENGTH_SHORT).show();
counter--;
if(counter == 0){
finish();
}
}
}
});
}
}
Do something like this :
OKButton.setOnClickListener(new View.OnClickListener() {
int counter = 3;
#Override
//once onClick is initalized it takes user to page menu
public void onClick(View v) {
You can also call a function from inside onClick which will decrement the variable, or use a static field declared in your class
This How to increment a Counter inside an OnClick View Event and How do I use onClickListener to count the number of times a button is pressed? might help.
Edit:
What you are doing in else part doesn't make sense. You are setting text for userText and passText that you just got using getText() from these. Then you are storing these same values to user and pass. But you aren't using these variables anywhere and they get new values when onClick is called again. Why not keep it simple :
else {
//display toast access fail
String fail = "Access Denied! Please Try again.";
//Create a Toast to display the fail string in the MainActivity.
Toast.makeText(MainActivity.this, fail, Toast.LENGTH_SHORT).show();
counter--;
if(counter == 0){
finish();
}
}
this is the question Shows the log-in form; the app must generate a message box notifying the user “Please Complete the required field” when the user leave the two text boxes blank ,while clicking the login button; wherein a generated message box pop and notify the user that “Wrong password!” when the user input the correct username and wrong password it will automatically clear the password textbox; wherein the app must generate a message box that notify the user that “Wrong Username!” if the user input the wrong username and correct password it will and automatically clear the username textbox; wherein the app must generate a messages box that notify the user that “Wrong Username and password!” if the user input both wrong username and wrong password it will automatically clear the password and username textboxes and set the text focus to the username textbox; wherein ;the app must notify the user “WELCOME” if when the user input the correct username and password it will automatically close the login activity and open the next activity.
Note: the Username and password must be both “admin”.
Button login;
EditText user, pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
user = (EditText)findViewById(R.id.editText1 );
pass = (EditText)findViewById(R.id.editText2 );
login = (Button)findViewById(R.id.button1 );
login.setOnClickListener((OnClickListener) this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(user.equals("admin") && (pass.equals("admin"))){
System.out.println("Welcome!");
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}else if (user.equals(null) && (pass.equals("admin"))) {
System.out.println ("Please Complete Required Field!");
}else if (user.equals("admin") && (pass.equals(null))) {
System.out.println ("Please Complete Required Field!");
}else {
System.out.println ("Wrong Username! or Wrong Password!");
}
}
}
To detect whether the editText is empty or not:
Change (user.equals(null)) to user.getText().toString().trim().length() == 0
and pass.equals(null) to pass.getText().toString().trim().length() == 0
In your button implementation, write like login.setOnClickListener(this); and make sure your Activity/Fragment implement OnClickListener like #pcg26 said.
Make sure you have implemented onClickListener.
In your onClick method, You must use a switch or if statement to make sure that the button is clicked.
Something like this:
public void onClick(View v) {
if(v.getId() == R.id.yourButtonID){
// do here
}
}
Is the activity implementing onclicklistener?
Simple a comment.. You can use Log.e("MyActivity"," the error"); instead System.out.println ^^
i have to show some operations in Android,,,
but Im having some noob problem,
public void calculateButtonPressed() {
// TODO Auto-generated method stub
Button button = (Button) findViewById(R.id.result);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.d("myTag", "result biatch");
String text=et.getText().toString();
//toast msg
//Toast msg = Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG);
// msg.show();
//set Text
tv.setText(text);
//result_tv.setText("copao");
int total = 1+2+3;
result_tv.setText(total);
}
});
}
so the app crashes , as im not using the variable correctly??
If I print the result_tv.setText("copao"); it works fine
but on my basic operation the app crashes,
int total = 1+2+3;
result_tv.setText(total);
so what Im I missing?
thanks!
It is assuming your total value as a string resource Id which might not be available in the R class as a valid resource Id, causing the app to crash.
Change:
result_tv.setText(total);
To:
result_tv.setText(String.valueOf(total));
//OR
result_tv.setText(total + ""); //to parse it as string
use
result_tv.setText(total+""); or result_tv.setText(String.valueOf(total));