Skip to content

Instantly share code, notes, and snippets.

@bikashthapa01
Last active December 12, 2021 14:30
Show Gist options
  • Save bikashthapa01/d2054fd5f26081d3f02bcf13d90b6c66 to your computer and use it in GitHub Desktop.
Save bikashthapa01/d2054fd5f26081d3f02bcf13d90b6c66 to your computer and use it in GitHub Desktop.
Android Studio - Java Sample code that download and read xls file from URL and Display in TextView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="2dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/recyclerView"
app:layout_constraintEnd_toEndOf="@+id/recyclerView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/recyclerView" />
<TextView
android:id="@+id/wait"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Please Wait"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
package net.smallacademy.excelreader;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<String> titles,descriptions,images;
private LayoutInflater inflater;
Adapter(Context context, List<String> titles,List<String> descriptions,List<String> images){
Log.d("data", "titles -> "+titles);
this.titles = titles;
this.descriptions = descriptions;
this.images = images;
this.inflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String title = titles.get(position);
String desc = descriptions.get(position);
Picasso.get().load(images.get(position)).placeholder(R.drawable.placeholder).error(R.drawable.placeholder)
//.resize(75,75)
.fit()
.centerCrop()
.into(holder.thumbnail);
holder.title.setText(title);
holder.desc.setText(desc);
}
@Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title,desc;
ImageView thumbnail;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
desc = itemView.findViewById(R.id.desc);
thumbnail = itemView.findViewById(R.id.cardImg);
}
}
}
dependencies {
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
implementation 'com.loopj.android:android-async-http:1.4.10'
implementation 'com.squareup.picasso:picasso:2.71828'
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:backgroundTint="?attr/colorButtonNormal"
android:padding="5dp">
<androidx.cardview.widget.CardView
android:layout_width="409dp"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/cardImg"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.03"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Sample Title"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/cardImg"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/desc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Sample Description for the given title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/cardImg"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
package net.smallacademy.excelreader;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cz.msebera.android.httpclient.Header;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
public class MainActivity extends AppCompatActivity {
Workbook workbook;
AsyncHttpClient asyncHttpClient;
List<String> storyTitle,storyContent,thumbImages;
RecyclerView recyclerView;
Adapter adapter;
ProgressBar progressBar;
TextView wait;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
progressBar = findViewById(R.id.progressBar);
wait = findViewById(R.id.wait);
String URL = "https://bikashthapa01.github.io/excel-reader-android-app/story.xls";
//String apiURL = "https://bikashthapa01.github.io/excel-reader-android-app/";
storyTitle = new ArrayList<>();
storyContent = new ArrayList<>();
thumbImages = new ArrayList<>();
// // checking if the excel file has new content
//
// try {
// URL mURL = new URL(apiURL);
// HttpsURLConnection httpsURLConnection = (HttpsURLConnection) mURL.openConnection();
// InputStream inputStream = new BufferedInputStream(httpsURLConnection.getInputStream());
// // getting network os exception error
// } catch (MalformedURLException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get(URL, new FileAsyncHttpResponseHandler(this) {
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) {
Toast.makeText(MainActivity.this, "Error in Downloading Excel File", Toast.LENGTH_SHORT).show();
wait.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
@Override
public void onSuccess(int statusCode, Header[] headers, File file) {
WorkbookSettings ws = new WorkbookSettings();
ws.setGCDisabled(true);
if(file != null){
//text.setText("Success, DO something with the file.");
wait.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
try {
workbook = Workbook.getWorkbook(file);
Sheet sheet = workbook.getSheet(0);
//Cell[] row = sheet.getRow(1);
//text.setText(row[0].getContents());
for(int i = 0;i< sheet.getRows();i++){
Cell[] row = sheet.getRow(i);
storyTitle.add(row[0].getContents());
storyContent.add( row[1].getContents());
thumbImages.add(row[2].getContents());
}
showData();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
}
});
}
private void showData() {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter(this,storyTitle,storyContent,thumbImages);
recyclerView.setAdapter(adapter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment