Registration condition not working? Android - android

I am trying to make registration activity without using SQLite in android.
In which user enters data at run time and if he do not fill all sections then user will not proceed to another activity. But the problem is if user is not entering anything then its also proceeding to another activity.
here is my code. Plz help me whats the error.
public void onClick(View v) {
EditText us = (EditText)findViewById(R.id.us);
EditText pas = (EditText)findViewById(R.id.pas);
EditText em = (EditText)findViewById(R.id.em);
EditText ph = (EditText)findViewById(R.id.ph);
String UserName= us.getText().toString();
String Email= em.getText().toString();
String Password= pas.getText().toString();
String PhoneNo= ph.getText().toString();
us.getEditableText().toString();
em.getEditableText().toString();
pas.getEditableText().toString();
ph.getEditableText().toString();
if ((us != null) && (em != null) && (pas != null) &&(ph != null)){
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
SendOfferActivity.class);
startActivity(intent);
}
else{
AlertDialog.Builder builder1 = new AlertDialog.Builder(Registration.this);
builder1.setMessage("Enter Valid Information.");
builder1.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
}});
}

But the problem is if user is not entering anything then its also
proceeding to another activity. here is my code
you are just checking if the object are not null, but what you want really check is if the EditTexts contain something. You use
if (!TextUtils.isEmpty(am.getText().toString()) /* do the same for the other */) {
}
to check the content of your EditTexts

You are just checking the EditText elements and you show test if the user typed something.
String usTxt = us.getText().toString();
String emTxt = em.getText().toString();
String pasTxt = pas.getText().toString();
String phTxt = ph.getText().toString();
if (!usTxt.isEmpty() && !emTxt.isEmpty() && !pasTxt.isEmpty() && !phTxt.isEmpty())
{
// your code
}

if ((!UserName.isEmpty()) && (!Email.isEmpty()) && (!Password.isEmpty() &&(!PhoneNo.isEmpty())){}

Related

if (String == null or String == "") doesn't activate [duplicate]

This question already has answers here:
String comparison - Android [duplicate]
(17 answers)
Closed 6 years ago.
I got this problem where I have three if statements and every one of them has IF String == null or String == "" and even the getText doesn't give any value, these doesn't activate.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_two:
final String hei = h.getText().toString();
final String wei = w.getText().toString();
String waist = wc.getText().toString();
if (hei == null || wei == null ){
final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
checker.setText("Please, put your information in these spots!");
checker.startAnimation(animationFadeIn);
}
else if (waist == null){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Are you sure?");
builder.setMessage("If you let Waist circumference empty, we will ask it from you later.")
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getApplicationContext(), number3.class);
i.putExtra("height" , hei);
i.putExtra("weight" , wei);
startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
});
AlertDialog alert = builder.create();
alert.show();
}
else{
Log.d("BMI Height" , String.valueOf(wei));
Log.d("BMI Weight" , String.valueOf(hei));
Intent i = new Intent(getApplicationContext(), healthcalculator3.class);
i.putExtra("height" , hei);
i.putExtra("WC" , waist);
i.putExtra("weight" , wei);
startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}
And even if all those (waist, wei, hei) are null or not value set, it only activates the last ELSE and the machine thinks that they have some data/value.
You can also try this like
if (hei == null || wei == null || hei.equals("") || wei.equals("")){
// your body here
}
Try this check
if(!TextUtils.isEmpty(string){
//body here
}
The best way to compare strings is use stringInstance.equals("what to compare"), so your code will be:
hei.equals("")
Also you can use length of string:
hei.length() > 0

Posting something to status on Facebook

I am using this tutorial to learn how to connect to Facebook via Android app. I am particularly interested in the section where he shows how to post something on the wall / status of the user.
The tutorial is fairly straightforward however my requirement is that I need to let the app append things along with what the user posts.
How can that be achieved ?
You can do something like this:
private void publishToFacebook(String message)
{
long songId = MusicUtils.getCurrentAudioId();
long albumId = MusicUtils.getCurrentAlbumId();
String albumartUrl = MusicUtils.getArtworkUrlFromExtrasCache(getApplicationContext(),albumId);
Bitmap bm = MusicUtils.getArtworkFromExtrasCache(getApplicationContext(),albumId,false,false);
if(bm == null)
bm = MusicUtils.getDefaultArtwork(getApplicationContext());
shareFacebookConnector = new ShareConnector(MediaPlaybackActivity.this, getApplicationContext());
shareFacebookConnector.setCurrentAlbum(MusicUtils.getCurrentAlbumName());
shareFacebookConnector.setCurrentArtist(MusicUtils.getCurrentArtistName());
shareFacebookConnector.setCurrentTrack(MusicUtils.getCurrentTrackName());
shareFacebookConnector.setCurrentCoverArt(bm);
if(albumartUrl != null)
{
shareFacebookConnector.setCurrentCoverUrl(albumartUrl);
}
LayoutInflater inflater = this.getLayoutInflater();
final View dialoglayout = inflater.inflate(R.layout.facebook_share, (ViewGroup) findViewById(R.id.facebook_share_root));
final ImageView image = (ImageView) dialoglayout.findViewById(R.id.facebook_cover_view);
input = (EditText) dialoglayout.findViewById(R.id.facebook_share_content);
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AlertDialogCustom);
AlertDialog.Builder alert = new AlertDialog.Builder(ctw);
if(message != null)
{
shareFacebookConnector.publishToFacebook(alert, input, message);
}
else
{
shareFacebookConnector.publishToFacebook(alert, input, null);
}
alert.setView(dialoglayout);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
bAlertInProgress = false;
String messageToPost = input.getText().toString();
shareFacebookConnector.postMessageToWall(messageToPost, false);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
bAlertInProgress = false;
Toast.makeText(MediaPlaybackActivity.this, "Wall post cancelled !", Toast.LENGTH_SHORT).show();
//finish();
}
});
bAlertInProgress = true;
mShareKey = "********";
alertDialog = alert.create();
alertDialog.show();
}
You have to open the session as session.openForPublish(); and you required permission "publish_action"

How to disable other buttons until the first button is clicked

I'm a newbie in android. I have got four buttons when I click my first tab. I want user to click the first button first rather than randomly clicking other buttons first. so need to disable other buttons until my first button is clicked and once the first button is clicked and when the user returns back all the buttons need to be enabled. How do I do this? Please help!!
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.imgbtn_details)
{
attendees_imgbtn.setEnabled(true);
resources_imgbtn.setEnabled(true);
contacts_imgbtn.setEnabled(true);
count = 1;
Intent detail_intent=new Intent(getActivity().getApplicationContext(),DetailsActivity.class);
detail_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(detail_intent);
}
else if(v.getId() == R.id.imgbtn_attendees && count == 0)
{
if(count == 1)
{
Intent attendee_intent = new Intent(getActivity().getApplicationContext(),AttendeesActivity.class);
attendee_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(attendee_intent);
}
else
{
attendees_imgbtn.setEnabled(false);
resources_imgbtn.setEnabled(false);
contacts_imgbtn.setEnabled(false);
}
}
else if(v.getId() == R.id.imgbtn_resources && count == 0)
{
if(count == 1)
{
Intent resources_intent = new Intent(getActivity().getApplicationContext(),ResourcesActivity.class);
resources_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(resources_intent);
}
else
{
attendees_imgbtn.setEnabled(false);
resources_imgbtn.setEnabled(false);
contacts_imgbtn.setEnabled(false);
}
}
else if(v.getId() == R.id.imgbtn_contacts && count == 0)
{
if(count == 1)
{
Intent contact_intent = new Intent(getActivity().getApplicationContext(),ContactsActivity.class);
contact_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(contact_intent);
}
else
{
attendees_imgbtn.setEnabled(false);
resources_imgbtn.setEnabled(false);
contacts_imgbtn.setEnabled(false);
}
}
}
Use setEnable(true) for buttons to enable and disable buttons
I think you need to pass variable specifying that you want to enable buttons or not.
First time, pass it as:
Intent intent = new Intent (this, NextAct.class);
intent.putExtra ("ENABLE", "No");
When you want to enable the buttons, pass String as Yes:
Intent intent = new Intent (this, NextAct.class);
intent.putExtra ("ENABLE", "YES");
Then check in your NextAct class for this string.
Intent intent = getIntent();
String enableBtn = intent.getStringExtra("ENABLE");
if (enableBtn != null && enableBtn.equalsIgnoreCase("YES")) {
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
} else {
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
}
So your buttons will be enabled only when you want them to.
Hope that helps.
define a static Boolean variable wasClicked....if but1 was clicked make variable true otherwise false...override this method....as user comes back it will check first than enable or disable as 1st but was clicked or not.
#Override
protected void onResume() {
// TODO Auto-generated method stub
if(!wasClicked){
but2.setClickable(false);
but3.setClickable(false);
but4.setClickable(false);
}else{
but2.setClickable(true);
but3.setClickable(true);
but4.setClickable(true);
}
}

How to page curl for pdf file

hi i m new in android page curl application, i show the pdf file using mupdf opensource via. here using intent action view, so only scroll is working but i need page curl.if any body know the answer please help this concept. thank you.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mAlertBuilder = new AlertDialog.Builder(this);
if (core == null) {
core = (MuPDFCore)getLastNonConfigurationInstance();
if (savedInstanceState != null && savedInstanceState.containsKey("FileName")) {
mFileName = savedInstanceState.getString("FileName");
}
}
if (core == null) {
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
if (uri.toString().startsWith("content://media/external/file")) {
// Handle view requests from the Transformer Prime's file manager
// Hopefully other file managers will use this same scheme, if not
// using explicit paths.
Cursor cursor = getContentResolver().query(uri, new String[]{"_data"}, null, null, null);
if (cursor.moveToFirst()) {
uri = Uri.parse(cursor.getString(0));
}
}
core = openFile(Uri.decode(uri.getEncodedPath()));
}
if (core != null && core.needsPassword()) {
requestPassword(savedInstanceState);
return;
}
}
if (core == null)
{
AlertDialog alert = mAlertBuilder.create();
alert.setTitle(R.string.open_failed);
alert.setButton(AlertDialog.BUTTON_POSITIVE, "Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
return;
}
createUI(savedInstanceState);
}
Get the code from here android_page_curl and link
From here you can download the source code for page curl effect.
Check this link for checkout source code from Google Code.

Android AlertDialog remembers string from previous shown?

The problem I am having with my Alert dialog is that, I have a custom dialog which has 3 edittext end 3 textview on it. when I pick up a contact from contacts I just filled the information in the dilaog edtitext like contactName.setText(bla); however the strange thing happens here that if I cancel the dialog and again pick another contact the details on the dialog is not getting changed and remembers the details from the first picked contact? is int weird? it seems that once it creates the dialog even though if I call the same process creating again the dialog it keeps the first dialog and keeps showing the same dialog. is there anyone whom have had the same experience and know how to solve this?
Here is the code that handles the result comes back from the contact picker.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI,
null, Email.CONTACT_ID + "=?", new String[] { id },
null);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
contactUEmail=email;
Log.v(DEBUG_TAG, "Got email: " + email);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
// EditText emailEntry = (EditText) findViewById(R.id.invite_email);
// emailEntry.setText(email);
if (email.length() == 0) {
Toast.makeText(this, "No email found for contact.",
Toast.LENGTH_LONG).show();
}
}
showDialog(DIALOG_ADD_NEW_CALL);// this is where I call the dialog.
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
and here is the dialog DIALOG_ADD_NEW_CALL;
case DIALOG_ADD_NEW_CALL:
{
builder = new AlertDialog.Builder(this);
entryView = getLayoutInflater().inflate(R.layout.entry, null);
builder.setView(entryView);
CNameEditor = (EditText) entryView.findViewById(R.id.cName);
CEmailEditor = (EditText) entryView.findViewById(R.id.cEmail);
CPhoneEditor = (EditText) entryView.findViewById(R.id.cPhone);
if(!contactUEmail.equals(""))//here is the code that I am setting the text.
CEmailEditor.setText(contactUEmail);
else{}
builder.setTitle(R.string.addDialogTitle);
builder.setPositiveButton(R.string.addItem,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.cancelItem,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
return builder.create();
}
To destroy the dialog, you can call removeDialog. Alternatively, you could override onPrepareDialog and update the dialog before it's shown. Just FYI, both methods are deprecated in favor of DialogFragment along with FragmentManager but you probably have to redo a lot of code to use those.
Override onPrepareDialog
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_ADD_NEW_CALL:
AlertDialog callDialog = (AlertDialog) dialog;
View entryView = callDialog;
CNameEditor = (EditText) entryView.findViewById(R.id.cName);
CEmailEditor = (EditText) entryView.findViewById(R.id.cEmail);
CPhoneEditor = (EditText) entryView.findViewById(R.id.cPhone);
// do stuff to those variables here
}
}

Categories

Resources