Make an interview preparation Android Application

This Application will be an Android Application which will provide the user with the question and answers which can be asked in an interview.
We will get the question and answers using a JSON API. This benefits us as we can change the data in it without upgrading the application.
Check out the full code here: https://github.com/Himanshu-Choudhary-07/Android-Task
Step 1: Make a JSON API. You can use this website: http://myjson.com/
In this application, our JSON API looks like this:
{
"questions":
[
{
"question": "What are Binary trees?",
"Answer": "A Binary Tree is a type of data structure that has two nodes: A left node and a right node. In programming, binary trees are actually an extension of the linked list structures."
},
{
"question": "What is a Stack?",
"Answer": "A stack is a data structure in which only the top element can be accessed."
},
{
"question": "What is a Stack?",
"Answer": "A stack is a data structure in which only the top element can be accessed."
},
{
"question": "How many minimum numbers of queues are needed to implement the priority queue?",
"Answer": "Two queues"
},
{
"question": "When should you use binary search engine?",
"Answer": "A binary search algorithm is used to search a list when the elements are already in order or sorted."
},
{
"question": "What is the difference between NULL and VOID?",
"Answer": "Null is actually a value, whereas Void is a data type identifier."
}
]
}
Step 2:
We have to create a layout which will be the basic model for every page. The code will be:\
slide_layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView
android:id="@+id/questionNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp"
android:text="Question Number" android:textColor="@color/grey"
android:fontFamily="sans-serif-condensed" android:textStyle="bold"
android:textSize="20dp"
android:gravity="center"/> <TextView
android:id="@+id/question" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp"
android:text="Question" android:layout_below="@id/questionNumber" android:fontFamily="sans-serif-condensed" android:textSize="20dp"
android:textColor="@color/sign_up" android:gravity="center"/> <ImageView
android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="80dp"
android:id="@+id/image"
android:src="@drawable/ds" android:layout_below="@id/question"/> <TextView
android:id="@+id/answer"
android:layout_below="@id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" a
ndroid:text="Answer "
android:fontFamily="sans-serif-condensed" android:textSize="20dp"
android:gravity="center"/></RelativeLayout>
Step 3: We will create an activity which will display the different questions on different pages and we change these question with a swipe. The code will be:
This the XML file:
activity_interview.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" tools:context=".InterviewActivity"> <android.support.v4.view.ViewPager android:id="@+id/slideViewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/previousBtn"> </android.support.v4.view.ViewPager> <LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:gravity="center"
android:id="@+id/dotsLayout" android:orientation="horizontal"
android:padding="20dp"> </LinearLayout> <Button
android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/previousBtn" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_alignParentLeft="true" android:text="PREVIOUS"
android:visibility="invisible"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed" android:background="@color/white" android:textColor="@color/sign_in" android:layout_margin="7dp"/> <Button
android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/nextBtn" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true"
android:text="NEXT"
android:textStyle="bold"
android:fontFamily="sans-serif-condensed"
android:background="@color/white" android:textColor="@color/crimson" android:layout_margin="7dp"/></RelativeLayout>
Step 4: Now, we will write the java code:
InterviewActivity.java
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;import com.thefinestartist.finestwebview.FinestWebView;public class InterviewActivity extends AppCompatActivity { private ViewPager mSlideViewPager;
private LinearLayout mDotLayout; private TextView[] mDots; private SlideAdapter slideAdapter; private Button mNext;
private Button mPrevious; private int mCurrentPage; String[] ques = new String[10];
String[] ans = new String[10]; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interview); ques = getIntent().getStringArrayExtra("hello");
ans = getIntent().getStringArrayExtra("hi"); mNext = (Button) findViewById(R.id.nextBtn);
mPrevious = (Button) findViewById(R.id.previousBtn); mSlideViewPager = (ViewPager) findViewById(R.id.slideViewPager);
mDotLayout = (LinearLayout) findViewById(R.id.dotsLayout); slideAdapter = new SlideAdapter(this);
mSlideViewPager.setAdapter(slideAdapter); addDots(0); mSlideViewPager.addOnPageChangeListener(viewListener); mNext.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
if (mCurrentPage < mDots.length-1) {
mSlideViewPager.setCurrentItem(mCurrentPage + 1);
}else {
Intent mainIntent = new Intent(InterviewActivity.this,MainActivity.class);
startActivity(mainIntent);
finish();
}
}
}); mPrevious.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
mSlideViewPager.setCurrentItem(mCurrentPage - 1);
}
});
}
public void addDots(int j){
mDots = new TextView[5];
mDotLayout.removeAllViews(); for(int i = 0; i<mDots.length;i++){
mDots[i] = new TextView(this);
mDots[i].setText(Html.fromHtml("•"));
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.grey)); mDotLayout.addView(mDots[i]);
} if (mDots.length > 0){
mDots[j].setTextColor(getResources().getColor(R.color.crimson));
}
} ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) {
addDots(position);
mCurrentPage = position; if(position == 0){
mNext.setEnabled(true);
mPrevious.setEnabled(false);
mPrevious.setVisibility(View.INVISIBLE); mNext.setText("NEXT");
mPrevious.setText("");
}else if (position == mDots.length-1){
mNext.setEnabled(true);
mPrevious.setEnabled(true);
mPrevious.setVisibility(View.VISIBLE); mNext.setText("FINISH");
mPrevious.setText("BACK");
}else{
mNext.setEnabled(true);
mPrevious.setEnabled(true);
mPrevious.setVisibility(View.VISIBLE); mNext.setText("NEXT");
mPrevious.setText("BACK");
}
} @Override public void onPageScrollStateChanged(int state) { }
};
public class SlideAdapter extends PagerAdapter { Context context;
LayoutInflater layoutInflater; MainActivity main = new MainActivity(); public SlideAdapter(Context context){
this.context = context;
} //Array public int[] slide_image = {
R.drawable.ds,
R.drawable.ds2,
R.drawable.ds3,
R.drawable.ds4,
R.drawable.ds }; public String[] slide_question_number = {
"Question 1",
"Question 2",
"Question 3",
"Question 4",
"Question 5" };
public String[] slide_questions = ques; public String[] slide_answers = ans;
@Override public int getCount() {
return slide_question_number.length;
} @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == (RelativeLayout) o;
} @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout,container,false); TextView mQuestionNumber= (TextView) view.findViewById(R.id.questionNumber);
TextView mQuestion= (TextView) view.findViewById(R.id.question);
TextView mAnswer= (TextView) view.findViewById(R.id.answer);
ImageView mAdvertiseImage= (ImageView) view.findViewById(R.id.image); mAdvertiseImage.setImageResource(slide_image[position]);
mQuestionNumber.setText(slide_question_number[position]);
mQuestion.setText(slide_questions[position]);
mAnswer.setText(slide_answers[position]); mAdvertiseImage.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) { new FinestWebView.Builder(InterviewActivity.this)
.show("https://medium.com/@himanshu017");
}
}); container.addView(view); return view;
} @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object o) {
container.removeView((RelativeLayout)o);
}
}}
Step 5: The main activity will be getting the data from the API. The code will be:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/relative"
android:background="@color/white" tools:context=".MainActivity"> <TextView
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="157dp"
android:fontFamily="sans-serif-condensed" android:gravity="center"
android:padding="20dp"
android:text="Data Structure Questions" android:textColor="@color/crimson"
android:textSize="28dp"
android:textStyle="bold" /> <Button
android:id="@+id/started"
android:layout_width="150dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="86dp" android:background="@color/sign_in" android:fontFamily="sans-serif-condensed"
android:text="GET STARTED"
android:textColor="@color/white" /> <TextView
android:id="@+id/text123" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"
android:padding="10dp" android:layout_alignParentBottom="true" android:textSize="14dp"
android:layout_marginBottom="20dp" android:textColor="@color/grey"
android:fontFamily="sans-serif-condensed"
android:text="It take 1-2 seconds to get data from API. So, please press after 2 Seconds."/></RelativeLayout>
Step 6: Now, the last step. The java code:
MainActivity.java
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import java.util.ArrayList;import dmax.dialog.SpotsDialog;
public class MainActivity extends AppCompatActivity { public String[] question_array = new String[10];
public String[] answer_array = new String[10]; private RequestQueue mQueue; public ArrayList<String> mlist = new ArrayList<>(); private RelativeLayout mLayout; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (RelativeLayout) findViewById(R.id.relative); Button buttonParse = findViewById(R.id.started); mQueue = Volley.newRequestQueue(this); buttonParse.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
jsonParse();
}
});
} private void jsonParse() { String url = "https://api.myjson.com/bins/vu2vi";//This will be the url of your API JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override public void onResponse(JSONObject response) {
ProgressDialog progressDoalog = new ProgressDialog(MainActivity.this);
progressDoalog.setMax(100);
progressDoalog.setMessage("Its loading....");
progressDoalog.show();
try {
JSONArray jsonArray = response.getJSONArray("questions");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i); String hm = employee.getString("question");
question_array[i] = employee.getString("question"); answer_array[i] = employee.getString("Answer");
Toast.makeText(MainActivity.this,hm,Toast.LENGTH_LONG).show();
progressDoalog.dismiss();
}
Intent next = new Intent(MainActivity.this,InterviewActivity.class);
next.putExtra("hello",question_array);
next.putExtra("hi",answer_array);
startActivity(next); } catch (JSONException e) {
e.printStackTrace();
Snackbar.make(mLayout,e.getMessage(),Snackbar.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Snackbar.make(mLayout,error.getMessage(),Snackbar.LENGTH_LONG).show();
}
}); mQueue.add(request);
} public String[] question_return(){
return question_array;
} public String[] answer_return(){
return answer_array;
} @Override protected void onStart() {
super.onStart(); }
}
The Screenshots of the following applications are provided below:





