JSONParsing & ListView 257

 Build Gradle

implementation libs.volley
implementation libs.picasso

Main Activity XML 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#FFFFFF"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_margin="10dp"
>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:visibility="gone"
/>


<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >

</ListView>


</LinearLayout>


</LinearLayout>


Main Activity JAVA

package com.rakibshah.server;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

TextView textView;
ProgressBar progressBar;
ListView listView;

ArrayList <HashMap<String, String>> arrayList = new ArrayList<>();
HashMap <String, String> hashMap;

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

progressBar = findViewById(R.id.progressBar);
listView = findViewById(R.id.listView);
textView = findViewById(R.id.textView);

String url = "https://asoshikhi.com/Drive/job.json";

JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
progressBar.setVisibility(View.GONE);

try {

for(int x=0; x<response.length(); x++){

JSONObject jsonObject = response.getJSONObject(x);
String title = jsonObject.getString("title");
String video_id = jsonObject.getString("video_id");


hashMap = new HashMap<>();
hashMap.put("title" , title);
hashMap.put("video_id" , video_id);
arrayList.add(hashMap);

}


MyAdapter myAdapter = new MyAdapter();
listView.setAdapter(myAdapter);



} catch (JSONException e) {
throw new RuntimeException(e);
}

}
}, new Response.ErrorListener() {
@Override

public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
textView.setText(""+error.getMessage());
textView.setVisibility(View.VISIBLE);

}
});

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(arrayRequest);

}

private class MyAdapter extends BaseAdapter{

@Override
public int getCount() {
return arrayList.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater layoutInflater = getLayoutInflater();
View myView = layoutInflater.inflate(R.layout.item, null);

TextView tvTitle = myView.findViewById(R.id.tvTitle);
ImageView imageThumb = myView.findViewById(R.id.imageThumb);

HashMap<String, String> hashMap = arrayList.get(position);
String title = hashMap.get("title");
String video_id = hashMap.get("video_id");


String image_url = "https://img.youtube.com/vi/"+video_id+"/0.jpg";

tvTitle.setText(title);
imageThumb.setImageResource(R.drawable.img);
Picasso.get().load(image_url).placeholder(R.drawable.img).into(imageThumb);


return myView;
}
}


}


Item.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
>


<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title Here"
android:textSize="25sp"
android:textStyle="bold"
/>

<ImageView
android:id="@+id/imageThumb"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/img"
android:scaleType="centerCrop"
/>
</LinearLayout>

1st & 2nd Fragmeant

 Main Activity XML



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:layout_margin="10dp"
>

<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Fragment"
android:textSize="20sp"
android:textStyle="bold"
/>

<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Fragment"
android:textSize="20sp"
android:textStyle="bold"
/>

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Third Fragment"
android:textSize="20sp"
android:textStyle="bold"
/>


<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>


</LinearLayout>


 Main Activity JAVA

package com.rakibshah.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.FirstFragment;
import com.SecondFragment;

public class MainActivity extends AppCompatActivity {

Button button1, button2, button3;

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

button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentManager fManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout , new FirstFragment());
fragmentTransaction.commit();

}
});

button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout, new SecondFragment());
fragmentTransaction.commit();

}
});

}
}

Create Fragment File

Com > NEW > Fragment > Fragment(Blank)


First Fragment XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.FirstFragment"
android:background="#88B6BC"
android:orientation="vertical"
android:padding="15dp"
>


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1st Fragment"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
/>

</LinearLayout>


First Fragment JAVA

package com;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rakibshah.fragment.R;

public class FirstFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_first, container, false);

return myView;
}
}


 Second Fragment XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#DD4D4D"
tools:context="com.SecondFragment">



<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2nd Fragmeant"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_gravity="center"
/>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>


Second Fragment JAVA

package com;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.rakibshah.fragment.R;


public class SecondFragment extends Fragment {

Button button;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_second, container, false);

button = myView.findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(getContext(), "Hello", Toast.LENGTH_LONG).show();

}
});
        return myView;
        }
   }




Fragment 1st

 Fragment 1st


XML


Create a Fragment Activity

Com > New > Fragment > Fragment (Blank)


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="500dp"
android:background="#2196F3"
>

</FrameLayout>


</LinearLayout>


JAVA

package com.rakibshah.fragment;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.FirstFragment;

public class MainActivity extends AppCompatActivity {

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

FragmentManager fManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fManager.beginTransaction();
fragmentTransaction.add(R.id.frameLayout , new FirstFragment());
fragmentTransaction.commit();

}
}

Fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.FirstFragment"
android:background="#88B6BC"
android:orientation="vertical"
android:padding="15dp"
>


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1st Fragment"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
/>

</LinearLayout>



Fragment JAVA

package com;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rakibshah.fragment.R;

public class FirstFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_first, container, false);

return myView;
}
}

JSON Array Request 255

 JSON Server Code

[

{

"title" :"Rakib Shah",

"video_id" : "xhUPXIFiAKk"

},

{

"title" :"Abid Shah",

"video_id" : "rrj9yiHrKc4"

},

{

"title" :"Prova Akter",

"video_id" : "e47QmqVMhGc"

},

{

"title" :"Abid Shah",

"video_id" : "rrj9yiHrKc4"

},

{

"title" :"Prova Akter",

"video_id" : "e47QmqVMhGc"

}

]


Build Gradle

implementation 'com.android.volley:volley:1.2.1'

Manifest Internet Permission
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="20dp"
>


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="10sp"
/>

</LinearLayout>

JAVA

package com.rakibshah.drivinglicense;

import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

TextView textView;

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

textView = findViewById(R.id.textView);

String url = "https://asoshikhi.com/Drive/job.json";

JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {

try {


for(int x=0; x<response.length(); x++){

JSONObject jsonObject = response.getJSONObject(x);
String title = jsonObject.getString("title");
String video_id = jsonObject.getString("video_id");
textView.append(x+"."+title+"\n"+video_id+"\n\n");

}

} catch (JSONException e) {
throw new RuntimeException(e);
}

}
}, new Response.ErrorListener() {
@Override

public void onErrorResponse(VolleyError error) {

textView.setText(""+error.getMessage());

}
});

RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(arrayRequest);

}
}

JSON Parsing in Android 253

 XML


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#FFFFFF"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
>

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#E91E63"
android:backgroundTint="#3F51B5"
android:layout_marginTop="50dp"

/>



<TextView
android:id="@+id/tName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/button"
android:layout_marginTop="10dp"
/>

<TextView
android:id="@+id/Tl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/tName"
android:layout_marginTop="10dp"
android:background="#FFC107"
/>

<TextView
android:id="@+id/tMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mobile"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/Tl1"
android:layout_marginTop="10dp"
/>

<TextView
android:id="@+id/Tl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/tMobile"
android:layout_marginTop="10dp"
android:background="#FFC107"
/>

<TextView
android:id="@+id/tEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mobile"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/Tl2"
android:layout_marginTop="10dp"
/>

<TextView
android:id="@+id/Tl3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/tEmail"
android:layout_marginTop="10dp"
android:background="#FFC107"
/>


<TextView
android:id="@+id/tAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mobile"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/Tl3"
android:layout_marginTop="10dp"
/>

<TextView
android:id="@+id/Tl4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_below="@+id/tAddress"
android:layout_marginTop="10dp"
android:background="#FFC107"
/>

</LinearLayout>


</RelativeLayout>

JAVA

package com.rakibshah.server;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends AppCompatActivity {

Button button;
TextView tName,Tl1,tMobile,Tl2,tEmail,Tl3,tAddress,Tl4;

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

button = findViewById(R.id.button);
tName = findViewById(R.id.tName);
Tl1 = findViewById(R.id.Tl1);
tMobile = findViewById(R.id.tMobile);
Tl2 = findViewById(R.id.Tl2);
tEmail = findViewById(R.id.tEmail);
Tl3 = findViewById(R.id.Tl3);
tAddress = findViewById(R.id.tAddress);
Tl4 = findViewById(R.id.Tl4);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String url = "https://asoshikhi.com/Drive/job.json";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
button.setText("Response is: " + response.substring(0,500));
Log.d("serverRes",response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
button.setText("That didn't work!");
}
});

// Add the request to the RequestQueue.
queue.add(stringRequest);

}
});



}
}

MANIFEST

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

BUILS GRADLE
implementation libs.volley