Search code examples
javaandroidandroid-studioandroid-studio-3.0android-studio-2.3

how to display image without onclick button


help guys how to display image without using a button, I tried to put it on create but not working and I tried to make it as a function but still not working,but when in button (on click) it show image. I want to show the image without clicking the button.

I want to load the image without clicking the button, so when the user come the image automatically load without push of a button.

public class MainActivity extends AppCompatActivity {

TextView textViewdatashow;
EditText editTextvalue;
ImageView imageView;
Button buttonfetch;
String url ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textViewdatashow = (TextView) findViewById(R.id.tvshowdata);
    editTextvalue = (EditText) findViewById(R.id.etvalue);
    imageView = (ImageView) findViewById(R.id.image);
    buttonfetch = (Button) findViewById(R.id.buttonfetchdata);

    buttonfetch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String id  = editTextvalue.getText().toString();

            url  = "https://PASTE_YOUR_IMAGE_URL"+id+".jpg";
            getData();


        }
    });

}

private void getData() {


    String id = editTextvalue.getText().toString().trim();


    if (id.equals("")) {

        Toast.makeText(this, "Check Detail!", Toast.LENGTH_LONG).show();
        return;
    }

    String url = Config.DATA_URL + editTextvalue.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            showJSONS(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

private void showJSONS(String response) {
    String name = "";


    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        name = collegeData.getString(Config.KEY_NAME);


    } catch (JSONException e) {
        e.printStackTrace();
    }

    textViewdatashow.setText("" + name);
    ImageRetriveWithPicasso();
}

private void ImageRetriveWithPicasso() {


    Picasso.get()
            .load(url)
            .placeholder(R.drawable.imageholder)
            .into(imageView);
}

}


Solution

  • Put this code inside on create view

        final Handler myHandler = new 
        Handler(Looper.getMainLooper());
    
        new Thread(new Runnable() {
    
        @Override
        public void run() {
       
        myHandler.post(new Runnable() {                           
    
        @Override
        public void run() {
           getData() 
              }
           });
        }
        })).start();                
        }