Monday, 4 May 2020

custom Log

import android.content.Context;
import android.widget.Toast;

public class Message {
    public static void message(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }
}
Message.message(context,""+xyz);

Sunday, 3 May 2020

loadJSONFromAsset Folder android

//users_list.json
{
  "users": [
    {
      "id": "1087",
      "name": "Abhishek Saini",
      "email": "saini.abhishek@gmail.com",
      "gender": "male",
      "contact": {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
      }
    },
    {
      "id": "1088",
      "name": "Gourav",
      "email": "gourav9188@gmail.com",
      "gender": "male",
      "contact": {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
      }
    },
    {
      "id": "1089",
      "name": "Akshay",
      "email": "akshay@gmail.com",
      "gender": "male",
      "contact": {
        "mobile": "+91 0000000000",
        "home": "00 000000",
        "office": "00 000000"
      }
    }
  ]
}



private String loadJSONFromAsset() {
    String json = null;
    try {
        InputStream is = getAssets().open("users_list.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer,"UTF-8");

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

    return json;
}

//
ArrayList<String> arrayList = new ArrayList();
ArrayList<String> arrayMobileList = new ArrayList();


try {
    JSONObject jobj = new JSONObject(loadJSONFromAsset());
    JSONArray jsonArray = jobj.getJSONArray("users");
    for(int i=0; i<jsonArray.length(); i++){
            JSONObject jo = jsonArray.getJSONObject(i);
            String name = jo.getString("name");
            arrayList.add(name);
            JSONObject contact = jo.getJSONObject("contact");
            String mobile = contact.getString("mobile");
            arrayMobileList.add(mobile);
    }
    Log.e("names",""+arrayList);


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


//
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);

CustomAdspter ca = new CustomAdspter(this, arrayList, arrayMobileList);
recyclerView.setAdapter(ca);
//

public class CustomAdspter extends RecyclerView.Adapter<MyHolder> {

    ArrayList<String> arrayListNames;
    ArrayList<String> arrayMobileListNames;
    Context context;
    public CustomAdspter(Context ctx, ArrayList<String> arrayList,ArrayList<String> arrayMobileList) {
        this.context= ctx;
        this.arrayListNames =arrayList;
        this.arrayMobileListNames =arrayMobileList;
    }

    @NonNull    @Override    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent,false);
        MyHolder myHolder = new MyHolder(v);
        return myHolder;
    }

    @Override    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
    holder.name.setText(arrayListNames.get(position));
    holder.mobile.setText(arrayMobileListNames.get(position));
    }

    @Override    public int getItemCount() {
        return arrayListNames.size();
    }
}
class MyHolder extends RecyclerView.ViewHolder {
    TextView name, mobile;
    public MyHolder(@NonNull View itemView) {
        super(itemView);
        name = (TextView) itemView.findViewById(R.id.name);
        mobile = (TextView) itemView.findViewById(R.id.mobile);
    }

}

Saturday, 2 May 2020

Json Parsing Android

  String JSON_STRING_OBJECT = "{\"employee\":{\"name\":\"XYZ\",\"salary\":4000}}";

try {
  JsonObject jo = new JsonObject(JSON_STRING_OBJECT);

//Parsing
  JsonObject empObj = jo.getJsonObject("employee");

  String empName = empObj.getString("name");
  String empSal = empObj.getString("salary");

//set text 
  TextView tv = findViewById(R.id.name);
  tv.setText(empName+""+ empSal);

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