2

I'm new to android programming and I think my biggest problem is that I can't really wrap my mind around all this inflating stuff. Can you help me with this one?

I'm working on a form, the forms content is seperated into different pages. Before you can proceed to the next page of the form, you need to fill in all obligatory EditTexts. So when the "next" button is clicked, i want to have my form edittexts checked for empty input. If there are Edittexts left empty, a border is drawn around them and you can't proceed.

My Problem is, that even if I fill in some Edittexts, they are still always perceived as empty. So i fill them in, click the "next" button, and a border is drawn around each one, regardless of being empty or not.

basically this is the Method causing the problem:

public void Border(View view){

    if (view instanceof EditText) {

        EditText edt = (EditText) view;


        if (edt.getText().toString().trim().length() == 0) {
        edt.setBackgroundResource(R.drawable.border);
        }
    }
}

So I want to check if (if the given view is an EditText) the EditText is empty. If yes, i want to draw a border around it. Obviously if (edt.getText().toString().trim().length() == 0) is always true. The Method Border() is called from here:

public boolean CheckInput(View v) {
    EditText phone = (EditText) findViewById(R.id.phonenr_et);
    EditText email = (EditText) findViewById(R.id.email_et);
    CheckBox box = (CheckBox) findViewById(R.id.agreement_checkbox);

    if (v.getId() != R.id.job_hunting_btn) {

        LinearLayout layout = (LinearLayout) findViewById(R.id.job_hunt_lay);

        if (layout.getChildCount() != 0) {
            for (int i = 0; i < layout.getChildCount(); i++) {
                View view = layout.getChildAt(i);
                Border(view);  // <-- Border is called
            }
        }
    }
        ...
        }

In CheckInput() I'm looping through a views Children and put put them into my Border Method. CheckInput() is called here:

    public void OnNextButtonClick(View v) {
    if (CheckInput(v)) {
        ViewList.get(CurrentViewId).setVisibility(View.GONE);
        if (v.getId() == R.id.job_hiring_btn)
            CurrentViewId = ArbeitgeberViewId;
        else {

            CurrentViewId++;
            if (CurrentViewId == ArbeitgeberViewId)
                CurrentViewId = LastViewId;
        }
        ViewList.get(CurrentViewId).setVisibility(View.VISIBLE);
    }
}

Here I want to make sure that you first have your Input checked before you can proceed to the next part of your form.

Here is my OnCreate() Method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setContentView(R.layout.content_main);
    setContentView(R.layout.job_hiring_data1);
    setContentView(R.layout.job_hiring_data2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    ViewList.add(findViewById(R.id.content_main_lo));


    ViewList.add(findViewById(R.id.job_hiring_data1_lo));
    ViewList.add(findViewById(R.id.job_hiring_data2_lo));
    ViewList.add(findViewById(R.id.job_hunt_lay));


    ArbeitgeberViewId = ViewList.size();


    ViewList.add(findViewById(R.id.offered_job_data1_lo));


    LastViewId = ViewList.size();
    ViewList.add(findViewById(R.id.formular_finished_lo));

    ImprintViewId = ViewList.size();
    ViewList.add(findViewById(R.id.imprint_lo));


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

Do I need to "inflate" the Edittext first or something? I'm honest, this is a group project, so everything seen in OnCreate() was created by a group member and i don't really know whats going on there -

I hope you can help me !

Thank you in advance!

kalu
  • 169
  • 1
  • 11
  • What do you type in that `EditText`? – Iulian Popescu Nov 22 '16 at 11:04
  • Did you checked if you were iterating throught the correct childs, if there were one EditText, If this was the correct one, if the text was type correctly, if your drawable was correct, ... ? And this is not a [mcve], could you limit this code to a simple snipet ? – AxelH Nov 22 '16 at 11:08
  • @IulianPopescu You mean for testing? Random letters mostly – kalu Nov 22 '16 at 11:09
  • @AxelH i think so , since the correct children, the Edittexts are bordered (well except that the ones with input are bordered aswell) after clicking the Next Button – kalu Nov 22 '16 at 11:12
  • I asked to make sure that you didn't typed any whitespace that would be removed by the `trim` method. But if you type random letters everything should be fine, so let me have a better look at the code – Iulian Popescu Nov 22 '16 at 11:12
  • [Check this post](http://stackoverflow.com/questions/22227950/android-is-using-setcontentview-multiple-times-bad-while-changing-layouts) explaining why you should not call `setContentView` four times in `onCreate` – AxelH Nov 22 '16 at 11:14

3 Answers3

0

Try this..

Declare outside onCreate (scope still inside the class)

private EditText phone;

Then initialise in onCreate (make sure the corresponding layout is set using setContentView before initialising )

 phone = (EditText) findViewById(R.id.phonenr_et);

and use it inside the method checkInput as

phone.getText().equals("");

try to include other layouts inside layout file as the following xml code and then setContentView to a single layout file in onCreate.

<include layout="@layout/content_main" />

or combine all layout files to a a single layout manually then setContentView to the layout file in onCreate

Azi
  • 117
  • 1
  • 4
0

You problem is your layouts (bad group member ;) ).

You are using four time setContentView so only the last one is kept.

setContentView(R.layout.activity_main);
setContentView(R.layout.content_main);
setContentView(R.layout.job_hiring_data1);
setContentView(R.layout.job_hiring_data2);

Use a Layout that will concate those layouts and this should help.

Official doc

void setContentView (int layoutResID)

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

Community
  • 1
  • 1
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • did you mean "contain" ? (Sorry english is not my first language) and do I do this with the tag? – kalu Nov 22 '16 at 11:32
  • Concatenate (adding one next to the other). If you need those layout someplace else, you can include those into a new File. If you don't, well just paste the layouts into one file (careful not to break the layout logic). – AxelH Nov 22 '16 at 11:34
  • I have seperate xml files for all views ... am i supposed to have only one file for all of my layouts and use setContView() for this file? – kalu Nov 22 '16 at 12:03
  • @kalu Yes, `setContentView` replace the current layout so calling it multiple time result in showing only the last one, this is a problem to `findViewById` if they are not presents. – AxelH Nov 22 '16 at 12:05
-1

check it by this code:

edittext.getText().toString().equalsIgnoreCase("")
masoud vali
  • 1,528
  • 2
  • 18
  • 29
  • Sadly still the same result. I feel like I'm missing something major, like saving the input first or inflating/declaring somethinf? Because all kind of input is ignored – kalu Nov 22 '16 at 11:06
  • What is the difference with the length check ? – AxelH Nov 22 '16 at 11:08