Make display data textviews invisible until a city is searched



I set my textviews on my weather app to display the data for cities when searched on my search panel (i.e temp_out.setText(response.body().getMain().getTemp() + " ℃"););. It works quite fine only that the textviews used to display those data still reflect on the app(i.e temp_out). I want to set it up in such a way that the textviews will be invisible until when a city is searched, then the data for such a city will be displayed on the textview section.

These are illustrations of what I’m trying to achieve:

This should be invisible when the app is opened(the part with a red tick): enter image description here

Then this data should be displayed after searching a city online(the part with a red tick): enter image description here

So far, I’ve tried using this code findViewById(R.id.textView9).setVisibility(View.GONE); in activity for one of my textviews(time_field).

It was totally invisible before and after searching a city and also gave this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.viz.lightweatherforecast.Activity.HomeActivity$2$1.onResponse(HomeActivity.java:112)
        at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1.lambda$onResponse$0$DefaultCallAdapterFactory$ExecutorCallbackCall$1(DefaultCallAdapterFactory.java:89)
        at retrofit2.-$$Lambda$DefaultCallAdapterFactory$ExecutorCallbackCall$1$3wC8FyV4pyjrzrYL5U0mlYiviZw.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6819)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)

Then I tried using this on the layout.xml android:visibility="gone" and android:visibility="invisible" for the same textview. It was as well invisible before and after searching a city but gave no exception.

I have no other option but to ask please what is the recommended way of doing this?

Here are the textviews used in displaying the data:

This is for my Fragment:

public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {

                try {
                    assert response.body() != null;
                    current_temp.setText(response.body().getMain().getTemp() + " ℃");
                    current_output.setText(response.body().getWeather().get(0).getDescription());
                    rise_time.setText(response.body().getSys().getSunrise() + " ");
                    set_time.setText(response.body().getSys().getSunset() + " ");
                    temp_out.setText(response.body().getMain().getTemp() + " ℃");
                    Press_out.setText(response.body().getMain().getPressure() + " hpa");
                    Humid_out.setText(response.body().getMain().getHumidity() + " %");
                    Ws_out.setText(response.body().getWind().getSpeed() + " Km/h");
                    Visi_out.setText(response.body().getVisibility() + " m");
                    Cloud_out.setText(response.body().getClouds().getAll() + " %");
                } catch (Exception e) {
                    Log.e("TAG", "No City found");
                    Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
                }

            }

and This is for my Activity:

public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {

                try {
                    assert response.body() != null;
                    time_field.setText("Last Updated:" + " " + response.body().getDt());
                } catch (Exception e) {
                    time_field.setText("Last Updated: Unknown");
                    Log.e("TAG", "No City found");
                    Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
                }
            }

I don’t know if posting the full code is required as I’m trying my possible best to follow the https://stackoverflow.com/help/minimal-reproducible-example, but if it’s needed, please let me know.

Answer

In XML file add android:visibility="gone" and your java class

public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {

                try {
                    assert response.body() != null;
                    current_temp.setVisibility(View.VISIBLE);
                    current_temp.setText(response.body().getMain().getTemp() + " ℃");
//all other stuffs


                } catch (Exception e) {
                    Log.e("TAG", "No City found");
                    current_temp.setVisibility(View.GONE);
                    Toast.makeText(getActivity(), "No City found", Toast.LENGTH_SHORT).show();
                }

            }


Source: stackoverflow