Search code examples
androiddialogandroid-textinputedittext

String variable empty after assigning value from editText.getText().toString()


I created a popup menu from a navigation drawer and everything works in regards to the actual popup. However, I have an EditText in the popup and would like the user to enter text into it and then click a button to do other things. My issue is that when I assign the EditText.getText().toString() value to a string variable in the function the string variable is still empty, and when I check for if EditText.getText().toString() is empty, it is not empty. So I don't understand how the assignment to a string variable still returns an empty string in the variable. I'm really new to Android programming so any advice would be appreciated. Thank you.

Snippet of onCreate method

public class workouts extends AppCompatActivity {
    private ActionBarDrawerToggle mToggle;
    private static final String TAG = "Workouts";
    private ExpandableListView listView;
    private ExpandableListAdapter listAdapter;
    private List<String> listHeader;
    private HashMap<String, List<String>> listHashMap;


    // Pop up menu variables
    private EditText categoryTitle;
    private Dialog myDialog;
    private String headerTitle = "";
    private boolean validTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workouts);

        DrawerLayout mDrawerLayout = findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        final NavigationView navigationView = (NavigationView)findViewById(R.id.navigationView);

        listView = (ExpandableListView)findViewById(R.id.expandable_list_view);
        initializeData();
        listAdapter = new expandableListAdapter(this, listHeader, listHashMap);
        listView.setAdapter(listAdapter);

        // Pop-up menu
        myDialog = new Dialog(this);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.back:
                    {
                        finish();
                        break;
                    }
                    case R.id.addCategory:
                    {
                        //View header = getLayoutInflater().inflate(R.layout.list_group, null);
                        //listView.addHeaderView(header);
                        /*
                            1) Create pop-up menu w/ EditText box
                            2) Type in header group to add
                            3) Click create button
                            4) Add new header to list
                         */
                        try {
                            ShowPopup(item);
                        }
                        catch (Exception e)
                        {
                            Log.d(TAG, "Add pop-up: " + e.toString());
                        }
                        break;
                    }

Function to create popup The string that i'm trying to fix is headerTitle

public void ShowPopup(MenuItem menuItem)
    {
        TextView textClose;
        Button btnAddCategory;
        myDialog.setContentView(R.layout.add_category_popup);
        textClose = (TextView)myDialog.findViewById(R.id.textClose);
        btnAddCategory = (Button)myDialog.findViewById(R.id.addCategory);
        categoryTitle = (EditText)myDialog.findViewById(R.id.category_title);
        headerTitle = categoryTitle.getText().toString();    // Header title to be used as hash key
        Log.i(TAG, "ShowPopup: Assigned header title");
        Log.i(TAG, "ShowPopup: " + headerTitle);
        textClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myDialog.dismiss();
            }
        });
        btnAddCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(categoryTitle.getText().toString().isEmpty())
                {
                    categoryTitle.setError("Field cannot be empty");
                    categoryTitle.requestFocus();
                    validTitle = false;
                }
                else
                {
                    categoryTitle.setError(null);
                    validTitle = true;
                }
                if(validTitle) {
                    listHeader.add(headerTitle);    // Add header title to list of headers
                    List<String> newHeader = new ArrayList<>(); // Create new expandable list w/ 0 items
                    listHashMap.put(listHeader.get(listHeader.size() - 1), newHeader);
                    myDialog.dismiss();
                }
            }
        });
        myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        myDialog.show();
    }

Solution

  • You should assign the value to the variable first:

            btnAddCategory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    headerTitle = categoryTitle.getText().toString()
                    if(headerTitle.isEmpty())
                    {
                        categoryTitle.setError("Field cannot be empty");
                        categoryTitle.requestFocus();
                        validTitle = false;
                    }
                    else
                    {
                        categoryTitle.setError(null);
                        validTitle = true;
                    }
                    if(validTitle) {
                        listHeader.add(headerTitle);    // Add header title to list of headers
                        List<String> newHeader = new ArrayList<>(); // Create new expandable list w/ 0 items
                        listHashMap.put(listHeader.get(listHeader.size() - 1), newHeader);
                        myDialog.dismiss();
                    }
                }
            });