How to Create dynamic layout and parse JSON data to build app like HOTSTAR

We will see here how to create dynamic layout in android and parsing of JSON data inside the dynamic layout. We will also see how to show image inside the dynamic layout. Movie and Channel category app like Hotstar, Youtube , Netflix uses the same logic to show there data but design and functionality are different.

I have created one simple activity and loaded JSON data from server. We will also learn how to use and create LinearLayout, TextView, ImageView dynamically.

Hotstar.java:

Here AsyncTask class is an abstract class which helps us to use the UI thread. It is mostly used when we are doing any network related operation. This class overrides the methods doInBackground, onPostExecute, onPreExecuteon, ProgressUpdate. See the official doc Here.

In method doInBackground HTTP POST method used to connect and fetch JSON data from server. See the post if you want to know how to connect android to MySQL/PHP . When doInBackground method complete it’s processing then it’s result passed to onPostExecute method.

You can create dynamically layout like this:

You can add your design methods like setText() already mentioned in code. In this way you can create layouts and add ImageView, TexView etc.

Here JSONObject and JSONArray class is used to parse JSON data.

The following parameters received as a JSON parsing :

  1. ‘hotstar’–  Main Array which differentiate category for example if you are creating other category of this Activity class then it will be categorised.
  2. ‘link’–  This parameter contains the string URL. It is very hard process to send image in JSON and it will slow down your server if you sending the direct image in JSON format. So , It is good practice to send only URL and when app receives this URL it loads the image in ImageView. Here Bitmap class used to show image from URL in ImageView. (See in above code line no. 149)
  3. ‘redirectlink’– Here in code ImageView will act like a HTML image link. Intent Action used to do this function. When user click on ImageView then it will ask user to open link in Browser or Hotstar.
  4. ‘name’– Description of clickable image shown by TextView exactly below of ImageView.

These all parameters added to form single layout dynamically in for loop.

You can show your existing GridView layout from XML file to java file :

Each single data including above above JSON strings with images added in GridLayout. Here ScrollView used to show dynamic data. See below movies_main.xml.

movies_main.xml:

Movies.php:

When server receive POST request from app then it send data in JSON format. Here using PHP I have send JSON data to app client using HTTP POST method. The POST parameter ‘category’ receive on server side. Then using PHP PDO method it fetches all data from MySQL database and encodes this data in JSON format. You can see detail explanation about PHP PDO(PHP Data Objects) here with android connection.

connectandroid.php:

Result on app:

  1. Simple GridLayout of data :
    Dynamic Data Inside Layout Android

    2. When User Click any ImageView then it will show like this:

    Dynamic Data Inside Layout Android After User Click On ImageView

     

If You have any query regarding this post feel free to comment !

Leave a Reply

Your email address will not be published. Required fields are marked *

2 comments

  1. Hi,
    Thanks for the info, But How do I get to know the data which I have is also available on Hotstar?
    And how did you get the data to link it to Hotstar.?
    is there any json API from Hotstar?

    1. Hi bishwajith bn,
      This post is about to ‘BUILD’ app like HOTSTAR. It is not related to get direct data from hotstar. I have explained only how to build movies category app like Hotstar, Sonyliv- LIVE, OZEE etc just to clear concept of dynamic layout and JSON parsing. If you want​ to show data from Hotstar app then you have to contact developers of Hotstar. Some apps do the same process. For example , OZEE app gets data from Ditto TV app. If you want to build something big or do business from your app then only contact to Hotstar.

      Thanks,
      Ajinkya