hello guys, am very new to android development, ans am trying to do my best to learn it,I have json output created from one of my PHP projects
here is my output

[
  {
    "id": "1",
    "name": "BEiN HD 1",
    "link": "http:\/\/www.mysite.com\/tv4.ogg",
    "image": "http:\/\/www.mysite.com\/images\/jsc8.png"
  },
  {
    "id": "2",
    "name": "BEiN HD 2",
    "link": "http:\/\/www.mysite.com\/tv4.ogg",
    "image": "http:\/\/www.mysite.com\/images\/jsc2.png"
  },
  {
    "id": "3",
    "name": "Comedy",
    "link": "http:\/\/www.mysite.com\/tv4.ogg",
    "image": "http:\/\/www.mysite.com\/images\/comedy.png"
  }
]

I want to read this file and create ImageButons dynamically for it src image will be the picture from JSON array.
any tutorials to achive this ?

Dani AI

Generated

Shortest workable plan: parse the JSON into a list of model objects off the UI thread, show those objects in a RecyclerView (GridLayout if you want tiles) and use an image-loading library (Glide or Picasso) to populate each tile’s ImageButton/ImageView. Handle clicks by reading the parsed link and starting the appropriate Intent or player.

Build steps (simple workflow)

  1. Add Internet permission in AndroidManifest.
  2. Fetch the JSON on a background thread (AsyncTask, executor, or a networking library).
  3. Parse into a List<Channel> — Gson makes this trivial for arrays; use a TypeToken to get a List type.
  4. Give that list to a RecyclerView adapter that inflates a small layout containing an ImageButton (or ImageView + overlay).
  5. Load each image URL into the view with an image loader (handles threading, caching, downsampling) and attach a click listener that uses the channel’s link.

Example parsing and image load snippets

Type listType = new com.google.gson.reflect.TypeToken<java.util.List<Channel>>(){}.getType();
java.util.List<Channel> channels = new com.google.gson.Gson().fromJson(jsonString, listType);
// inside onBindViewHolder
Glide.with(holder.itemView.getContext())
     .load(channel.getImageUrl())
     .placeholder(R.drawable.placeholder)
     .error(R.drawable.error)
     .into(holder.imageView);

Quick troubleshooting & tips

  • Don’t do network or image decoding on the main thread — that’s why libraries are preferred.
  • If images come over plain HTTP, newer Android versions may block them by default (use HTTPS or configure cleartext policy).
  • Use placeholders and .override() or resize to avoid OOM on large images.
  • For simple streaming clicks, launch an ACTION_VIEW Intent with the channel link or hand the URL to your media player.

Notes on this thread: ’s suggestion to use Gson is a good fit; ’s DIY Drawable loader proves the concept but will miss caching, automatic scaling and background execution that Glide/Picasso provide.

Recommended Answers

All 4 Replies

Am sorry i forgot to add my test code for creating buttons, how to set image src to buttons from JSON url.

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LinearLayout lm = (LinearLayout) findViewById(R.id.ll);
        // create the layout params that will be used to define how your
        // button will be displayed
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //Create four
        for(int j=0;j<=6;j++)
        {
            // Create LinearLayout
            ScrollView SV = new ScrollView(this);
            // Create Button
            final Button btn = new Button(this);
            // Give button an ID
            btn.setId(j+1);
            btn.setText("CH" + (j+1));
            // set the layoutParams on the button
            btn.setLayoutParams(params);

            final int index = j;
            // Set click listener for button
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),
                            "Clicked Button Index :" + index,
                            Toast.LENGTH_SHORT).show();

                }
            });

            //Add button to LinearLayout
            SV.addView(btn);
            //Add button to LinearLayout defined in XML
            lm.addView(SV);
        }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

To understand your question better:

A. Are you asking about how to process JSON in Java?
or
B. Are you asking how to set button image URL from parsed/processed JSON?

Well honestly I was asking for how to process JSON in java, but I relized I can't put direct links for images as source,then I found a way to make this happen

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

so my problem now is
A. Are you asking about how to process JSON in Java? :)
sorry for this mess.

Simplest way (well by me) is GSON (user guide and ). Create model of the class,

public class Movie {
    @SerializedName("id")
    int id;
    @SerializedName("name")
    String name;
    @SerializedName("link")
    String link;
    @SerializedName("image")
}

get your JSON (not sure if you fetching raw stream that you push to reader or you actually getting string) and tell GSON to parse it based on this sample would be

Gson gson = new Gson();
Movie[] movies = gson.fromJson(jsonString, Movie.class);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.