-5

I'm working on an Android app, most of it was with the help of tutorials and reading material, my app is somewhat basic, make some calculations and display the result to the user

What I have

I created a fragment guided by this tutorial and I just replaced the button and textview with my own EditText and Button

I have an EditText, a Button, and a TextView, these are the widgets the user see when the fragment is opened

My goal

I want the user to input values in the EditText and when s/he click the button the TextView displays the result of some calculations

What I did

I've been trying some other answers from here, here, here, here, but I don't know how to wire up the code, every time I write some pieces of code my app crashes and I don't know what else to do

Here is my code

public class Circle_Perimeter extends Fragment
{
    EditText edtxt;
    Button btn;
    TextView txt;

    public static Circle_Perimeter newInstance()
    {
        Circle_Perimeter fragment = new Circle_Perimeter();
        return fragment;
    }

    public Circle_Perimeter()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_circle__perimeter,container,false);

        edtxt = (EditText)rootView.findViewById(R.id.editText);
        btn = (Button)rootView.findViewById(R.id.button6);
        txt = (TextView) rootView.findViewById(R.id.textView6);

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

            }
        });

        return rootView;
    }
}

Question

How and where do I write the necessary code to make the EditText send a Toast warning the user the EditText is empty? or disable the Button if the EditText is empty?

EDIT: Really?? A downvote?? Instead of downvoting... Just give a warning or something else

EDIT 2: I already said that I did follow another answers from here and I couldn't make my code work that's why I'm here looking for an answer WITH my piece of code

Community
  • 1
  • 1
NAYIR55
  • 105
  • 5
  • 17
  • where exactly is your code crashing?? can you post the `error log`? – himanshu1496 Aug 29 '16 at 07:50
  • Why not leave the button disabled and implement a onChangeListener for the editText? just curious the approach. – Danny Beaumont Aug 29 '16 at 07:52
  • 1
    Unrelated to the merits of your question, please refrain from begging. I edited out the noise this caused. If your question has merit, it will not be downvoted. Begging for answers and votes will only lead to the opposite effect. – Magisch Aug 29 '16 at 08:00

3 Answers3

2

To notify the text changes in EditText you need to use TextWatcher. Please have a look at this document https://developer.android.com/reference/android/text/TextWatcher.html

public class Circle_Perimeter extends Fragment
{
    EditText edtxt;
    Button btn;
    TextView txt;

    public static Circle_Perimeter newInstance()
    {
        Circle_Perimeter fragment = new Circle_Perimeter();
        return fragment;
    }

    public Circle_Perimeter()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_circle__perimeter,container,false);

        edtxt = (EditText)rootView.findViewById(R.id.editText);
        btn = (Button)rootView.findViewById(R.id.button6);
        txt = (TextView) rootView.findViewById(R.id.textView6);

        //Initially set it as disabled
        btn.setEnabled(false);

        //Add textWatcher to notify the user
        edtxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //Before user enters the text
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //On user changes the text
                if(s.toString().trim().length()==0) {
                    btn.setEnabled(false);
                    Toast.makeText(getActivity(), "Text can not be empty..",
                            Toast.LENGTH_SHORT).show();
                } else {
                    btn.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //After user is done entering the text

            }
        });

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //do your final job here
            }
        });

        return rootView;
    }
}
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
pooja
  • 393
  • 8
  • 16
  • @NAYIR55 try this out. Hope it helps! – pooja Aug 29 '16 at 08:05
  • 1
    @downvoters, would you please be kind enough to leave the reason for down vote, that will help me improve this answer and in future – pooja Aug 30 '16 at 03:11
  • i did not down vote but i think you got that due to unnecessary code because according to his requirement no need of text watcher just find the length and validate the edit text value is empty or not when button click is enough. – brahmy adigopula Aug 30 '16 at 05:42
  • @brahmyadigopula I have mentioned about TextWatcher because his requirement is "How and where do I write the necessary code to make the EditText send a Toast warning the user the EditText is empty? or disable the Button if the EditText is empty?" So if he does the check of empty string on click of a Button and disables the Button, again how will he know that user has entered the text to enable it again ? – pooja Aug 30 '16 at 05:59
  • you can achieve that feat under button click also.i think you also how to do that..disable means make it dead so you can achieve that through validation using length().If his requirement like button visibility then the above code is preferable.don't get angry on me mam am just an analyzer. – brahmy adigopula Aug 30 '16 at 06:07
  • @brahmyadigopula Take this scenario, you have entered a text and erased it and then clicked on Button, now text length is 0 so you disable the Button and give a Toast and immediately enable the button for next click. If you again give empty text and enter and keep doing this, how is it a good practice ? PS I am a analyzer too, there is nothing to get angry about, I just want to know your scenario that it. – pooja Aug 30 '16 at 06:13
  • mam let's discuss in chat,gist here. – brahmy adigopula Aug 30 '16 at 06:15
  • Sure, the here link has not come properly – pooja Aug 30 '16 at 06:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122162/discussion-between-brahmy-adigopula-and-pooja). – brahmy adigopula Aug 30 '16 at 06:17
  • Using `TextChangeListener` is correct approach as this question is marked duplicate and the original questions answer also has same answer. – Nikhil Aug 30 '16 at 07:42
  • @pooja Do you work in agathsya technologies, B'lore? – Siddhivinayak Aug 21 '17 at 10:52
0

Its simple to check that edit text is empty

if{edittext.getText == null){}

or

if(editText.isEmpty){}

Also best way is make for button

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(edittext != null){
//do what u want for no empty button
}else{
//block ur button}

                }
            });
Genehme
  • 69
  • 9
0

You can check the EditText for null instances using the TextUtils API that comes bundled with Android

if(!TextUtils.isEmpty(editText) {
    //Do your desired work
}else {
    //Notify user for rectification
}

To play around with the button, you can use either of them:

  • button.setVisibility(VIEW.GONE)
  • button.setEnabled(false)
CodeWalker
  • 2,281
  • 4
  • 23
  • 50