How to Create Pie Chart In Android Studio[2020]

Hi, In this post we are going to learn, How we can create a pie chart in the android studio. The pie chart is very important for data analysis because it is easy to understand.

We can create a pie chart by using MP Android chart dependency. MP Android chart is an easy and powerful way to design many types of charts.

Without wasting your time, we are going to explain to you how to make charts.

Create Pie Chart In Android Studio

Step 1: Adding Dependency

The first things you need to do is to add the dependency.

Dependencies allow us to include an external library or local jar files or other library modules in Android project

Go to Android then build.gradle(Module: app) and add this inside dependency

dependencies {
 implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

Now add following line of code in your repository in build.gradle(Project: App Name)

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
        
    }
}

Now sync your project.

Step 2: Adding Pie Chart in .xml

After we add dependency, our second step is to add the pie chart in .xml

<com.github.mikephil.charting.charts.PieChart
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pieChart"/>

Step 3: Adding values in Pie Chart

After adding pie chart in xml we need to pass data in Pie chart, we do this in java file by using following code

public class MainActivity extends AppCompatActivity {

PieChart pieChart;
PieData pieData;
List<PieEntry> pieEntryList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pieChart = findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);
        pieEntryList.add(new PieEntry(10,"India"));
        pieEntryList.add(new PieEntry(5,"US"));
        pieEntryList.add(new PieEntry(7,"UK"));
        pieEntryList.add(new PieEntry(3,"NZ"));
        PieDataSet pieDataSet = new PieDataSet(pieEntryList,"country");
        pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
}

Now, I am going to explain each line of code. First, we create a reference variable of the pie chart, pie data. We also create a list of pie Entry by following line of code

PieChart pieChart;
PieData pieData;
List<PieEntry> pieEntryList = new ArrayList<>();

Pie chart uses pie data to represent values, and Pie Data uses a list of pie entries to represent values.

After this inside onCreate() we connect Pie chart to its xml view. We set Pie chart to use percent values to represent data by these line of code

        pieChart = findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);

Now, we add pieEntry objects in pieEntry List and then add it in the Pie Data set and then we add pie data set to pie data by given code.

Pie entry takes two arguments in the constructor, first represents the value, and the second represents the name.

       pieEntryList.add(new PieEntry(10,"India"));
        pieEntryList.add(new PieEntry(5,"US"));
        pieEntryList.add(new PieEntry(7,"UK"));
        pieEntryList.add(new PieEntry(3,"NZ"));
        PieDataSet pieDataSet = new PieDataSet(pieEntryList,"country");
        pieData = new PieData(pieDataSet);

After this, we only need to connect it to the pie chart and we do this easily by given code.

setData() sets the data and invalidate() is used if we update our values then it automatically changes the values in the pie chart

       pieChart.setData(pieData);
        pieChart.invalidate();

If you run your code you see something like this, yes! I know what are you thinking.

pie chart in android studio
pie chart in android studio

We can change the color and UI in the next heading

Step 4: Customizing Pie chart

Changing Colours

We are going to change colour of pie chart, to do so we just add the setColor() method in pie data set like this

  pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
pie chart in android studio
pie chart in android studio

Complete code

public class MainActivity extends AppCompatActivity {
PieChart pieChart;
PieData pieData;
List<PieEntry> pieEntryList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pieChart = findViewById(R.id.pieChart);
        pieChart.setUsePercentValues(true);
        pieEntryList.add(new PieEntry(10,"India"));
        pieEntryList.add(new PieEntry(5,"US"));
        pieEntryList.add(new PieEntry(7,"UK"));
        pieEntryList.add(new PieEntry(3,"NZ"));
        PieDataSet pieDataSet = new PieDataSet(pieEntryList,"country");
        pieDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
        pieData = new PieData(pieDataSet);
        pieChart.setData(pieData);
        pieChart.invalidate();
    }
}

Removing hole in Pie chart

If you want to remove the hole in pie chart, you need to use this method

 pieChart.setDrawHoleEnabled(false);
pie chart in android studio
pie chart in android studio

What it does it, It removes your hole radius, also If you want to change your hole radius you can do this by the following code

pieChart.setHoleRadius(3);

Change Description Of Pie Chart

To change the description of pie chart, you need to add simply this line of code

pieChart.getDescription().setText("Country");
description label in android studio
description label in android studio

Conclusion:

I Hope, you learn something new in this post. You can refer docs if you want some other feature in the pie chart or simply comment on your doubts.

Read about toast, back button in android studio here.

Tagged : /

9 Best Offline Football Game For Android 2020

Football has more number of a follower than cricket and also the most watchable sports. In this post, we are going to introduce the 9 best football game for android which can be played offline in 2020, and we update our content as we see some latest released game which is not introduced in our list.

So, To bring the list of best offline football games we spend more than 3 hours doing research and I bet you loved all this.

Best Offline Football game for android

These are our top 11 picker’s

1. World Soccer League

world soccer league
best football offline game

The first game that is on our list is World Soccer League. This game comes with high-quality graphics. This game has many features, some of them are 60 national teams, 60 clubs, and a total of 2000 players.
It also has a total of 4 modes provided: Exhibition, Cup, League, and Training.
and also provide you splendid dribble and thrilling shooting!

In addition, you can save and keep all the images or videos of any moment.
Achievements and Global Activity are provided, support 15 languages, and also tablet devices.

Game Size – 46MB and Download Link

2. Football Strike: Multiplayer Soccer

football soccer
multiplayer soccer

You’ve never played a football game like this before. One of the best things about this game is that it provides you multiplayer ground. You can customize your striker and goalkeeper with tons of unlockable items and show off your style or represent your team’s colors!

Some of the key features of this game are you can play multiplayer battles against your friend and provides you a fast gameplay

Game Size – 50MB and Download Link

3. Soccer! Hero

soccer hero
soccer hero

This game is available free on play store and it is created by Free games private limited and also has a total download more than 10M. One of the best parts of this game is that this game has more than 700 levels and as you are reaching higher levels you achieve a status of a pro player. You can customize your player and also rank up in leaderboard if you play well.

Some of the best Feature of this game is that it has stunning 3d graphics and more than 700 levels. This game is easy to play but hard to master. You can also take part in regular events and win trophies. Connection with Facebook is also a good feature where you connect with your friend and compete with each other.

Game Size – 96MB Download Link

4. Ultimate Soccer Football

ultimate soccer
ultimate soccer

One of the best realistic games, this game provides the above-average experience of playing the game. The control of this game consists of a joystick same as in pubg and 2 fire buttons named as pass and goal. The graphics of this game is so realistic and also provide you a smoother and faster play.

One of the best features of this game is that it provides you a high 3d graphics with the best sound effect which attracts you toward the game. also, there are many types of modes available such as Career mode, World Cup mode & Friendly mode

Game Size – Varies with device and Download Link

5. Real Football

real football
real football

This game is designed by the Gameloft and one of the best parts of this game is it is of only 31MB in size. It is a free simulation sports game where you’ll be able to win a league or cup championship, play with world champions, be a manager of a fantasy team, and more.

The best feature of this games includes, 3D stadiums, polished shadows, detailed textures, and spectators and also it has 3D stadiums, polished shadows, detailed textures, and spectators. This game is designed for smarter players so that they can feel realistic and challenging experience!

Game Size 31MB and Download Link

6. Soccer Cup 2020

soccer cup 2020
Soccer cup

This game is totally awesome. Once you try to play this football games then you won’t stop, it’s all because of the real football feeling. You will find not only a soccer games but also many other game modes. There are many football games, but none of them are like this one. Take total control of every action on the pitch with free soccer games in a way that only we can provide!

Some of the best quality of this game is It comes with different mode season, tournament, practice and career mode. This game is most realistic game and provide you high feel of real ground.

Game Size 58MB and Download Link

7. Score Match: PvP Soccer

score match

With millions of players worldwide this game is a unique soccer multiplayer game, You become captain your team on the road to GLORY, recruit your friends and rise to the top in a series of challenging Arenas and weekly Events.

you can play your own way like dribble, tackle, pass and also shoot against opponents in real-time online matches against all over the world.

Some of the best features of this game are this game provides you real-time fast-paced 1 on 1 matches against a player from all over the world. This game is an Addictive, easy, and fun Score!. You can earn a package to unlock new player types and upgrade players to reach their potential and showcase their skills. You can also take part in regular seasons and events to win unrivaled rewards

Game Size 86MB and Download Link

8. Dream League Soccer 2020

dream league 2020

Dream League Soccer 2020 puts you in the heart of the action with a fresh look and brand new features! Build your dream team from over 3,500 FIFPro™ licensed players and take to the field against the world’s best soccer clubs! Rise through 8 divisions whilst enjoying full 3D motion-captured player moves, immersive in-game commentary, team customizations, and much more. The beautiful game has never been so good!

In this game you can develop your dream team from over 3,500 licensed players. This game is full 3D motion-captured kicks, tackles, celebrations and goalkeeper saves gives unmatched realism you can Build your soccer empire from your own Stadium to Medical, Commercial and Training facilities also use Coaches to develop your players technical and physical abilities

Game Size 336 Mb and Download Link

9. Football eLegends – Dream Off! Soccer

Game by Abstron private limited completely entertain you and take the football leagues to the next level. You can find better grounds, players, uniforms, and opponents. This game also gives you the feature of managing your own team. It also provides good controlling options to control your player.

This game will also give you 4 mode Quick Match, International Cup, Penalty Kick, Challenges. The quick match will allow the user to learn the basics of football, International cup winner will go through tough challenges given by 15 other teams. Penalty kicks will test your abilities under high pressure. Challenges mode is the last one and much difficult than rest all

Game Size 25Mb and Download Link

Conclusion

So, you read above 9 best offline football games for android and I assume you pick one of them according to feature listed above in each game. According to me, I choose Soccer cup 2020.

Tree in Data Structure

We are already aware of data structures like an array, linked list, stack, queue, etc. But can you guess what is common between all the above data structures ..and why we introduce tree in data structure?

Yes, you guessed it right. All the data structures are LINEAR data structures. Why is it called linear..? Because you traverse it one after another and only one element can be directly accessed while traversing.

But there are many real-life things which we can’t represent linearly or I would say that we can represent them linearly but it will bear you the high cost.

Let us discuss some of the examples,

Computer Hard Drives

We almost use computers or laptops every day. When we go to my computer section, we see that there are four sections of our hard drives. C drive, E drive, F drive, and H drive.

Now let think of a situation,

Let consider the structure of a hard drive like shown in the picture,

drive in computer

Let’s say we want to access a file in Drive F.

While traversing linearly, we have to access it through Drive C and then Drive E, which means first I have to go through all the files of Drive C and then through all the files of Drive E, and then we can finally access any file in Drive F.

Just Think of this situation, your life would have been pathetic.
Thanks to tree data structure.

tree in data structire

This representation looks fine as we have access to all three other drivers from my computer, and this is where tree data structures come into play.

Binary Search Tree in Data Structure

In Linear Data Structures, such as Linked List, Searching takes O(n) time complexity. In Arrays inserting and deleting a data takes O(n) time complexity.

Although O(n) looks good but thinks of real-world examples where we deal with millions of records, here O(n) may not be sufficient for inserting, deleting, or searching data.

Here also trees play a vital role. Binary Search Tree is an application of Tree where insertion, deletion, and searching all takes O(log n) time complexity.

binary tree in data structure

This is a Binary Search Tree, we will discuss it later but it is an application of Tree Data Structure.

There are millions of applications of Tree Data Structures in the real world. Let us now do some technical stuff.

Technical Definition of Tree

In a programming language, the tree is defined as a collection of nodes and edges. What is a node..?

Node is an entity which represents a tree, nodes are connected by edges and this overall combination forms a network which is known as a tree.

binary tee in data structure

Types of Trees in Data Structure

Binary Tree in Data structure :

A tree where each node can have at most two children is called a binary tree. A binary tree can have at most (2^h – 1) numbers of nodes. The height of a tree is represented by ‘h’.

The height of a tree is the number of levels in a tree. In the given tree, ‘h’ is 3.

N-ary Tree: A tree where each node can have n children is called an N-ary tree.

Some Technical Terms

Root: Topmost Element of the tree is called the root of the tree.
Parent: The node which has a branch from it to any other node is called a parent node.
Child: The node which is a descendant of some node is called a child node.
Siblings: Nodes that belong to the same parent are siblings to each other.

Leaf Node: Nodes having no children is called a leaf node.
Internal Node: Nodes with at least one child are internal nodes.

Let us take an example,

tree in data structure

In the above example,

(i) A is the root of the tree.
(ii) B is the parent of D and E.
(iii) C is a child of A.
(iv) B and C are siblings.

(v) D, E, G are leaf nodes.
(vi) A, B, C, and F are internal nodes.

Binary Tree

As we have discussed earlier, the binary tree is a tree where each node can have at most 2 children. The two children are referred to as the left child and right child.

Now, you must be wondering how we can represent this tree data structure in our code..? Yes, this is an obvious question every beginner thinks of. There are two ways of representing a binary tree data structure. One is by using an array and the other is by using a doubly-linked list.

Representation of Binary Tree using an array

We will initially store the root in the 0th index. Since we know that a node in a binary tree can have at most two children. So we have to give two continuous slots for every root to store his left and right child.

So, we will store the left child of the root in ((2*i) + 1)th index and store the right child of the root in ((2*i) + 2)th index.

Let us consider an example,

Disadvantages of using Array Representation

  1. In array representation, we have to predefined the height of the tree so that maximum of  (2^h-1) space can be allocated in the array for the binary tree representation. We have to fixed the size of the array initially which doesn’t allow us to grow our tree beyond.
  1. In array, memory wastage is a big issue. In the second example, we saw that the 4th and 6th index were unused. Just think of a large tree with millions of nodes, in that case, memory wastage will be a serious problem.

Representation of Binary Tree using Doubly Linked List

We are using a doubly-linked list because we require two pointers, one for the left child and another for the right.

struct Node
{
int data;
struct Node *left;
struct Node *right;
};

Types of Binary Trees in Data Structure

Perfect Binary Tree

It is a binary tree in which all levels are completely filled. It means every level must contain (2^l) nodes (level starting from 1).

A Perfect Binary Tree of height ‘h’ has 2h – 1 node.

perfect binary tree

Complete Binary Tree :

It is a binary tree in which all levels are completely filled except possibly the last level and keys at last level must have nodes as left as possible.

complete binary in tree data structure

Every perfect Binary Tree is a complete tree but vice versa is not true.

That’s All Folks


Tagged : / / /