3

I want to change the background color of my edit text using color picker. I have got a class and in it there is an editText. I am using fragment and in the fragment that particular editText of that class is being used. In the fragment there is an image view clicking on which the color picker dialog is shown.

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   EditText et = (EditText) view.findViewById(R.id.txtTest);

   ImageView imgChooser = (ImageView) view.findViewById(R.id.imgPallete);

}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    imgChooser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AmbilWarnaDialog colorpicker = new AmbilWarnaDialog(getContext(), Color.BLACK, new AmbilWarnaDialog.OnAmbilWarnaListener() {

                @Override
                public void onCancel(AmbilWarnaDialog dialog) {

                }

                @Override
                public void onOk(AmbilWarnaDialog dialog, int color) {
                    et.setBackgroundColor(color);
                }
            });
            colorpicker.show();
        }
    });

}

but this is not working and shows java.lang.NullPointerException. Can anybody please help me? I am new to android studio.

The error log is as follow

java.lang.NullPointerException
                                                                                                 at com.example.sudeepbajracharya.myapplication.BackGround$1$1.onOk(BackGround.java:61)
                                                                                                 at yuku.ambilwarna.AmbilWarnaDialog$6.onClick(AmbilWarnaDialog.java:179)
                                                                                                 at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                                                 at android.os.Looper.loop(Looper.java:194)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5371)
                                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
                                                                                                 at dalvik.system.NativeStart.main(Native Method)

the problem is being showed in line 61 and In line BackGround.java:61 there is
et.setBackgroundColor(color);

Sudeep
  • 97
  • 8
  • `java.lang.NullPointerException` share your whole crash log with question – AskNilesh Dec 26 '17 at 06:11
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Dec 26 '17 at 06:17
  • @Nilu I have shared my crash log. Please look at it. :) Thank You – Sudeep Dec 26 '17 at 06:20
  • Share full activity code please with XML. – Syed Hamza Hassan Dec 26 '17 at 06:32
  • Can you Toast what this color value in onOk . `Toast.makeText(getApplicationContext(), color, Toast.LENGTH_SHORT).show();` – vm345 Dec 26 '17 at 06:54
  • 1
    Your et variable which you initialized is local to onViewCreated. As you have not provided whole code, i assume the et variable declared globally is not initialized and due to that is giving you NPE. – Rajen Raiyarela Dec 26 '17 at 06:58
  • @Sudeep, i hope your problems has been solved. – Prags Dec 26 '17 at 17:28

2 Answers2

0

your EditText not reference properly so try below code

private EditText et;
private ImageView imgChooser;

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);
   et = (EditText) view.findViewById(R.id.txtTest);

   imgChooser = (ImageView) view.findViewById(R.id.imgPallete);

}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    imgChooser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AmbilWarnaDialog colorpicker = new AmbilWarnaDialog(getContext(), Color.BLACK, new AmbilWarnaDialog.OnAmbilWarnaListener() {

                @Override
                public void onCancel(AmbilWarnaDialog dialog) {

                }

                @Override
                public void onOk(AmbilWarnaDialog dialog, int color) {
                    et.setBackgroundColor(color);
                }
            });
            colorpicker.show();
        }
    });

}
OmiK
  • 2,606
  • 1
  • 19
  • 35
  • thank you for your response brother but it still does not work :( i am having problem in et.setBackgroundColor(color); – Sudeep Dec 26 '17 at 06:42
0

You can use

edittext.setBackgroundResource(R.color.white);

or if above code snipt not work then you can try below code

If you are in activity use this

edittext.setBackground(ContextCompat.getColor(this,R.color.yourcolor));

if you are in fragment use below code

edittext.setBackground(ContextCompat.getColor(getActivity(),R.color.yourcolor));

if you are in recyclerview adapter use below code

edittext.setBackground(ContextCompat.getColor(context,R.color.yourcolor));

Because, edittext.setBackgroundColor() takes a color in numeric form (e.g., 0xFFFF0000 for red). R.color.white is also a number.

and make sure you have correct reference of edittext

Prags
  • 2,277
  • 2
  • 18
  • 32