Creating Simple Horizontal graph in android using android Weight property without any External Libraries

Hi Coders ,



This a simple example for how to make simple graph in android without using any external library



this a small part of the my xml file wich is used make the graph part,Hope you guys can do the rest..

the out put of the graph is given at the bottom..







            



            

        








Initially i set the weight of each text view to 50 dp


this my java code  friend there might me some extra codes because its part of my project, so i cant show the entire project ...

 The libary name Ion is used for getting the json data from the web,and according to that value graph is set


TextView c1gtexy=(TextView )FindViewById(R.id.country1_bar);
TextView c2gtexy=(TextView )FindViewById(R.id.country2_bar);


TextView c1gtexy=(TextView )FindViewById(R.id.country1_bar);
TextView c2gtexy=(TextView )FindViewById(R.id.country2_bar);


public void getPredictions() {



  if (new NetworkData().isNetworkAvailable(this)) {

   Ion.with(getApplicationContext())

     .load("http://www.someweb.com/index.php")

     .addQuery("type", "1").addQuery("matchid", pooler[0])

     .asJsonArray().setCallback(new FutureCallback() {



      @Override

      public void onCompleted(Exception arg0, JsonArray arg1) {

       JsonArray jar = arg1.getAsJsonArray();



       if (jar.size() > 0) {

        JsonElement elem = jar.get(0);

        JsonObject obj = elem.getAsJsonObject();



        int team1Count = obj.get("team_1").getAsInt();

        int team2Count = obj.get("team_2").getAsInt();

        double totalCount = team1Count + team2Count;



        V1width = (float) ((team1Count / totalCount) * 100);

        V2width = (float) ((team2Count / totalCount) * 100);

        graphAdjuster(V1width, V2width);



        mpd.dismissProgress();

       }

      }

     });

  } else {



   String[] score = scores.getString(

     getIntent().getExtras().getString("pool"), "").split(" ");



   System.out.println((float) Integer.parseInt(score[0])+"     "+ (float) Integer.parseInt(score[1]));

   

   graphAdjuster((float) Integer.parseInt(score[0]), (float) Integer.parseInt(score[1]));

   mpd.dismissProgress();

   Toast.makeText(getBaseContext(),

     "No data connection ,Statitics not available",

     Toast.LENGTH_SHORT).show();

  }

 }



//This function is important this is the function which changes our graph.



 public void graphAdjuster(float a, float b) {



  LayoutParams l1 = new LayoutParams(LayoutParams.WRAP_CONTENT,

    LayoutParams.MATCH_PARENT, a);

  LayoutParams l2 = new LayoutParams(LayoutParams.WRAP_CONTENT,

    LayoutParams.MATCH_PARENT, b);

  ed = scores.edit();




  c1gtext.setText((int) a + " %");

  c1gtext.setLayoutParams(l1);

  c2gtext.setText((100 - (int) a) + " %");

  ed.putString(getIntent().getExtras().getString("pool"), (int) a

    + " " + (100 - (int) a));

  c2gtext.setLayoutParams(l2);

  ed.commit();

 }




friend if u have any clarification please free feel to ask also if i did make any mistake please also mention it








Comments

Popular posts from this blog

Adding Markers in Google Map and Focusing Camera when map loaded