Skip to main content

 Rest API Data Get  (Sportmonks) and Search Option


step 1: data get in Sportmonks uses RestApi 
step 1: dependencies add   build.gradle(:app)

apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "ruhulbdapp.com.blogspot"
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.material:material:1.4.0-beta01'

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.2.0"
// For control over item selection of both touch and mouse driven selection
//implementation "androidx.recyclerview:recyclerview-selection:1.1.0"

}
step 2: Make sure Internet Permission and intent-filter action search
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ruhulbdapp.com.blogspot">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Show_item_Activity"></activity>
<activity android:name=".MainActivity">

<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
step 3: make model class same as Response Restapi
package ruhulbdapp.com.blogspot.model;

public class Continents {
private String resource;
private int id;
private String name;
private String updated_at;

public Continents(String resource, int id, String name, String updated_at) {
this.resource = resource;
this.id = id;
this.name = name;
this.updated_at = updated_at;
}

public String getResource() {
return resource;
}

public void setResource(String resource) {
this.resource = resource;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUpdated_at() {
return updated_at;
}

public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}



public Continents() {
}




}

step 4: make Constant Class for dynamic object handle
package ruhulbdapp.com.blogspot.utilities;

public class Constant {

public static final String api_token="ZA1heZn1JlBuA6y5zgTmxTpLztinjZRlETwlebbrBsWsp";
public static final String base_url="https://cricket.sportmonks.com/api/v2.0/";
}

step 5: make Interface for  RetrofitClint 
package ruhulbdapp.com.blogspot;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface RetrofitClient {

@GET("continents")
Call<OurMainDataClass> getData(@Query("api_token") String token);

}

step 5: make a Main Object Class like as Response Data 
package ruhulbdapp.com.blogspot;

import java.util.List;

import ruhulbdapp.com.blogspot.model.Continents;

public class OurMainDataClass {

public List<Continents> data;

public List<Continents> getData() {
return data;
}

public void setData(List<Continents> data) {
this.data = data;
}

public OurMainDataClass(List<Continents> data) {
this.data = data;
}
}

step 6: layout design 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:orientation="vertical"
tools:context=".MainActivity">



<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rec_list_id"
android:layout_width="match_parent"
android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>


</RelativeLayout>

step 7: layout design for item show  activity_show_item.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_centerInParent="true"
android:gravity="center"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Show_item_Activity">

<com.google.android.material.card.MaterialCardView
android:layout_margin="10dp"
android:elevation="15dp"
app:cardCornerRadius="10dp"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="250dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:textColor="#000"
android:textStyle="bold"
android:id="@+id/resource_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:textColor="#000"
android:id="@+id/continent_id_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resource_id" />

<TextView
android:textColor="#000"
android:id="@+id/name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/continent_id_id" />

<TextView
android:textColor="#000"
android:id="@+id/updated_at_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>


</RelativeLayout>
step 8: layout design for adapter   continent_show_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="7dp"
android:elevation="15dp"
android:defaultFocusHighlightEnabled="true"
app:cardCornerRadius="2dp"
android:clickable="true"
android:outlineAmbientShadowColor="#0288D1"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:textColor="#000"
android:textStyle="bold"
android:id="@+id/resource_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:textColor="#000"
android:id="@+id/continent_id_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="28dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resource_id" />

<TextView
android:textColor="#000"
android:id="@+id/name_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/continent_id_id" />

<TextView
android:textColor="#000"
android:id="@+id/updated_at_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>



</com.google.android.material.card.MaterialCardView>
step 9: create menu for search_meu.xml 
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
android:title=""
android:id="@+id/action_settings"
android:icon="@drawable/ic_search_black_24dp"
android:orderInCategory="100"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"
/>

</menu>
step 10: ok goto MainActivity.java
package ruhulbdapp.com.blogspot;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import ruhulbdapp.com.blogspot.adapter.Continents_Adapter;
import ruhulbdapp.com.blogspot.model.Continents;
import ruhulbdapp.com.blogspot.utilities.Constant;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements Continents_Adapter.In_itemsellect {
private RetrofitClient retrofitClient;
private List<Continents> allcontinentslist;
private RecyclerView recyclerView;
public Continents_Adapter continents_adapter;
public SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView=findViewById(R.id.rec_list_id);

Retrofit retrofit = new Retrofit
.Builder()
.baseUrl(Constant.base_url)
.addConverterFactory(GsonConverterFactory.create())
.build();


//getallcontinents(retrofit);
allcontinentslist=new ArrayList<>();

retrofitClient = retrofit.create(RetrofitClient.class);

Call<OurMainDataClass> call = retrofitClient.getData(Constant.api_token);

call.enqueue(new Callback<OurMainDataClass>() {
@Override
public void onResponse(Call<OurMainDataClass> call, Response<OurMainDataClass> response) {

if (response.isSuccessful())
{
allcontinentslist=response.body().getData();

continents_adapter=new Continents_Adapter(MainActivity.this,MainActivity.this,allcontinentslist);
// recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(continents_adapter);
continents_adapter.notifyDataSetChanged();

for (Continents continents:allcontinentslist)
{
Log.d("resource",continents.getResource());
Log.d("id",String.valueOf(continents.getId()));
Log.d("name",continents.getName());
Log.d("updated_at",continents.getUpdated_at());

}


}

}

@Override
public void onFailure(Call<OurMainDataClass> call, Throwable t) {

Log.d("failed","not data fetch");
}
});




}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

int id=item.getItemId();
if (id==R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}

private void getallcontinents(Retrofit retrofit)
{
retrofitClient = retrofit.create(RetrofitClient.class);

Call<OurMainDataClass> call = retrofitClient.getData(Constant.api_token);

call.enqueue(new Callback<OurMainDataClass>() {
@Override
public void onResponse(Call<OurMainDataClass> call, Response<OurMainDataClass> response) {

if (response.isSuccessful())
{
allcontinentslist=response.body().getData();

for (Continents continents:allcontinentslist)
{
Log.d("resource",continents.getResource());
Log.d("id",String.valueOf(continents.getId()));
Log.d("name",continents.getName());
Log.d("updated_at",continents.getUpdated_at());
}

}

}

@Override
public void onFailure(Call<OurMainDataClass> call, Throwable t) {

Log.d("failed","not data fetch");
}
});
}

@Override
public void sellect(Continents continents) {

Intent intent=new Intent(MainActivity.this,Show_item_Activity.class);
intent.putExtra("resource", continents.getResource());
intent.putExtra("id", String.valueOf(continents.getId()));
intent.putExtra("Continent_name", continents.getName());
intent.putExtra("updated_at", continents.getUpdated_at());
startActivity(intent);

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.serch_menu, menu);

// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_settings).getActionView();
assert searchManager != null;
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint("Enter serch ..");

searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);


searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String query) {

Toast.makeText(MainActivity.this, "serch...", Toast.LENGTH_SHORT).show();
continents_adapter.getFilter().filter(query);


//fetchdata();
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(MainActivity.this, "wait...", Toast.LENGTH_SHORT).show();
continents_adapter.getFilter().filter(newText);
//fetchdata();

return false;
}
});






return true;
}
}
step 11: adapter class
package ruhulbdapp.com.blogspot.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import ruhulbdapp.com.blogspot.R;
import ruhulbdapp.com.blogspot.model.Continents;

public class Continents_Adapter extends RecyclerView.Adapter<Continents_Adapter.ViewHolder> implements Filterable {

private In_itemsellect in_itemsellect;
private Context context;
private List<Continents> allcontinentslist;
public List<Continents> filterlist;



public Continents_Adapter(In_itemsellect in_itemsellect, Context context, List<Continents> allcontinentslist) {
this.in_itemsellect = in_itemsellect;
this.context = context;
this.allcontinentslist = allcontinentslist;
this.filterlist=allcontinentslist;
}



public Continents_Adapter() {
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view=LayoutInflater.from(context).inflate(R.layout.continents_item_design,parent,false);
return new ViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

Continents continents=allcontinentslist.get(position);

holder.resource.setText("resource : "+allcontinentslist.get(position).getResource());
holder.name.setText("continents name : "+continents.getName());
holder.id.setText("Continents id: "+String.valueOf(continents.getId()));
holder.updated_at.setText(continents.getUpdated_at());


Log.d("res",continents.getResource());
}

@Override
public int getItemCount() {

return filterlist.size();


}

@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
filterlist = allcontinentslist;
} else {
List<Continents> filteredList = new ArrayList<>();
for (Continents item : allcontinentslist) {

// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (item.getName().toLowerCase().contains(charString.toLowerCase()))
{
filteredList.add(item);
}
}

filterlist = filteredList;
}

FilterResults filterResults = new FilterResults();
filterResults.values = filterlist;
return filterResults;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filterlist = (ArrayList<Continents>) filterResults.values;
notifyDataSetChanged();
}
};
}

public interface In_itemsellect{
void sellect(Continents continents);
}

public class ViewHolder extends RecyclerView.ViewHolder {

public TextView resource,name,id,updated_at;
public ViewHolder(@NonNull View itemView) {
super(itemView);

resource=itemView.findViewById(R.id.resource_id);
name=itemView.findViewById(R.id.name_id);
id=itemView.findViewById(R.id.continent_id_id);
updated_at=itemView.findViewById(R.id.updated_at_id);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
in_itemsellect.sellect(allcontinentslist.get(getAdapterPosition()));
//Toast.makeText(context, "sellect : "+String.valueOf(allcontinentslist.get(getAdapterPosition())), Toast.LENGTH_SHORT).show();
}
});
}
}
}
step 11: and create showitem 
package ruhulbdapp.com.blogspot;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Show_item_Activity extends AppCompatActivity {

public TextView resource,name,id,updated_at;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_item_);

resource=findViewById(R.id.resource_id);
name=findViewById(R.id.name_id);
id=findViewById(R.id.continent_id_id);
updated_at=findViewById(R.id.updated_at_id);


try {
Intent intent=getIntent();
resource.setText("resource: "+intent.getStringExtra("resource").toString());
name.setText("id: "+intent.getStringExtra("id").toString());
id.setText("Continent_name: "+intent.getStringExtra("Continent_name").toString());
updated_at.setText("updated_at: "+intent.getStringExtra("updated_at").toString());

}catch (Exception e)
{

}



}
}

Comments

Popular posts from this blog

 var val  deference in kotlin  fun main () { var Android_Programing_language : String = "java" println ( "android official programing language: " + Android_Programing_language ) Android_Programing_language = "Kotline" println ( "2019 google announced android official programing language: " + Android_Programing_language ) val Android_MultiPlatform_language : String = "Dart" println ( "multiple device run : " + Android_MultiPlatform_language ) /* Android_MultiPlatform_language="" /* note: var--------------valu changable note: val--------------valu not changable (final) */ */ }
 Firebase Cloud Messaging With  push notification with Image  public class ImageDownload extends AsyncTask < String , Void , Bitmap >{ @Override protected Bitmap doInBackground ( String ... strings ) { InputStream inputStream ; try { URL url = new URL( strings [ 0 ]) ; try { HttpURLConnection connection = ( HttpURLConnection ) url .openConnection() ; connection .connect() ; inputStream = connection .getInputStream() ; return BitmapFactory . decodeStream ( inputStream ) ; } catch ( IOException e ) { e .printStackTrace() ; } } catch ( MalformedURLException e ) { e .printStackTrace() ; } return null; } @Override protected void onPostExecute ( Bitmap bitmap ) { ShowNotification( bitmap ) ; } }
  Kotlin Function function means use big problems divided by submodule and easily problems solved.   It makes reusability of code and makes the program more manageable There are two types of functions: Standard library function User-defined function Standard Library Function Kotlin Standard library function is built-in library functions import java.time.LocalDateTime fun main (args: Array < String >) { val current = LocalDateTime.now() println( "Current Date and Time is: $current " ) } User-defined Function A user-defined is created by the user. User-defined function takes the parameter(s), perform an action and return the result of that action as a value. fun  main(args: Array<String>){       sum()       print( "code after sum" )   }   fun  sum(){       var num1 = 5        var...