0

So I have two EditText fields. One is for a category and the other one to place a link in it. If you click on the button "weiter" there is a control if the EditText are empty. In the next step I want to make sure that in the EditText link is an real link and not just e.g. only a word. So I want to check if it contains www. and now the problem is how can I make a break if it doesn't contain it?

That the new Intent starts because I set it in the next if-else that's clear.

So how can I make it better?

Here's my code thank you all:

((Button) findViewById(R.id.weiter)).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i = new Intent(MainActivity2.this, MainActivity8.class);

        EditText et1 = (EditText) findViewById(R.id.editText2);
        String Link1 = et1.getText().toString();

        EditText et = (EditText) findViewById(editText1);
        String Kategorie1 = et.getText().toString();

        if (Link1.contains("www.")) {

        } else {
            et1.setError("Link ist nicht gültig");
        }

        if (Link1.matches("") || Kategorie1.matches("")) {
            et.setHintTextColor(RED);
            et1.setHintTextColor(RED);
        } else {
            i.putExtra("ersteActivityKategorie", Kategorie1);
            i.putExtra("ersteActivityLink", Link1);
            startActivity(i);
        }
    }
});
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Luca486
  • 53
  • 1
  • 10
  • I would be inclined to disable the button by default, until the link text field contains a valid link. Do a check whenever the link text field is updated. – rossum Jan 07 '17 at 12:08

3 Answers3

0

Just return when you didn't find the www on the start of the String.

if (Link1.contains("www.")) {
    // Do nothing
} else {
    et1.setError("Link ist nicht gültig");
    et1.setHintTextColor(RED);
    return;   // Return from here
}

if (Link1.matches("") || Kategorie1.matches("")) {
    et.setHintTextColor(RED);
    et1.setHintTextColor(RED);
} else {
    i.putExtra("ersteActivityKategorie", Kategorie1);
    i.putExtra("ersteActivityLink", Link1);
    startActivity(i);
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0
public class Trackfolio extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    public EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        editText = (EditText)findViewById(R.id.editText1);

        editText.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        editText.setText("");
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
0
Intent i = new Intent(MainActivity2.this, MainActivity8.class);

        EditText et1 = (EditText) findViewById(R.id.editText2);
        String Link1 = et1.getText().toString();

        EditText et = (EditText) findViewById(editText1);
        String Kategorie1 = et.getText().toString();

        if (isValidUrl(Link1) == true) {

        } else {
            et1.setError("Link ist nicht gültig");
        }

        if ((Link1.equals("") && Kategorie1.equals("")) || (Link1.length() == 0 && Kategorie1.length() == 0) || (Link1.matches("") || Kategorie1.matches(""))) {
            et.setHintTextColor(RED);
            et1.setHintTextColor(RED);
        } else {
            i.putExtra("ersteActivityKategorie", Kategorie1);
            i.putExtra("ersteActivityLink", Link1);
            startActivity(i);
        }

        private boolean isValidUrl(String url) {
            Pattern p = Patterns.WEB_URL;
            Matcher m = p.matcher(url.toLowerCase());
            if(m.matches())
                return true;
            else
                return false;
        }
    }
nzala
  • 374
  • 4
  • 10