Thursday, 25 August 2022

Max value in array


 public class HelloWorld{


     public static void main(String []args){

        System.out.println("Hello, World!");

        int arry[] = new int[]{1,2,33,33,6,6};

        int max = arry[0];

          for(int i=1; i<arry.length;i++){

                 if(arry[i]> max){

                     max = arry[i];

                 }

            

         }

          System.out.println("Hello, World!"+max);

     }

}

Wednesday, 24 August 2022

 /* palindrome Number */

public class HelloWorld{


     public static void main(String []args){

        System.out.println("Hello, World!");

        int rem, sum =0, temp;

        int num=151;

        temp=num;

        while(num>0){

            rem = num%10;

            sum =(sum*10)+rem;

            num =num/10;

            

        }

        if (temp == sum){

              System.out.println("palindrome");

        }

        else{

              System.out.println("Hello, not");

        }

     }

}

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();
}

Thursday, 12 December 2019

Null Safety


!! Not-null Assertion //use it when you are sure the value is notnull
//throws nullpointerexception if the value is found to be null

?.let{} safe call with let

?. Safe call operator
Val name: String = null
By default kotlin not support nullable.
If we want to assign null we can use -> Val name: String? = null

?.let{} safe call with let
//1. returns the length if the name is not null else return null
//2. Name?.let{//It Executes the block only if the name is notnull
Println("")
}
?: Elvis
// when we have a nullable reference name, we can say "is name is not null",use it ,
//otherwise use some non nullable value
  1. Val nameLen=if(name!=null)
Name.length
Else
-1
Or
Val leng = name?.length?: -1

Wednesday, 4 December 2019

Difference between DP , SP ,DPI and Pixel


DPI- Dots per Inch
DP - Density independent pixels—>it scales as per device screen density—>used to define layouts or views dimension—>pixel unit I.e independent of density of the device screen
Px- Pixel

SP: scale independent pixel—>used only for textview size—>it scales as per device screen density and user setting preferences.

px= dp*(dpi/160)

1dp equals to how many pixel for mdpi
Here mdpi = 160 pixel
         hdpi=  240 pixel
        Xhdpi = 320 pixel
xxhdpi=480 pixel


Px = 1*160/160

Px =1dp

For hdpi=1px=1*240/160=1.5px—>scale factor

Screen size measured in diagonally- 5.0’ screen
Screen resolution 720*1280 pixels

Screen density measured in DPI(Dots per Inch)
        mdpi = 160 pixel
         hdpi=  240 pixel
        Xhdpi = 320 pixel
xxhdpi=480 pixel