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 ?

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 API). 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.