Skip to content

Commit 2ae5daf

Browse files
author
chimemoo
committed
update news app
1 parent fc343a5 commit 2ae5daf

File tree

12 files changed

+149
-42
lines changed

12 files changed

+149
-42
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ This is my learning journey of android, i will add new app and some note in this
3333
<img src="assets-markdown/onboard-app3.png" alt="drawing" width="200"/>
3434
- I learn how to using Intro App library for making onboard on application
3535
- I Learn how to making simple thread
36-
8. News App (On Progress) <br>
36+
8. News App (On Progress) <br>
37+
<img src="assets-markdown/news-app1.png" alt="drawing" width="200"/>
38+
<img src="assets-markdown/news-app2.png" alt="drawing" width="200"/>
39+
- I Learn how to use CardView
40+
- Implementing Interceptor for logging
41+
- Using Shimmer Layout

assets-markdown/news-app.png

-597 KB
Binary file not shown.

assets-markdown/news-app1.png

28.7 KB
Loading

assets-markdown/news-app2.png

951 KB
Loading

news-app/app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ dependencies {
4747

4848
// Swipe Refresh Layout
4949
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
50+
51+
// Shimmer Effect
52+
implementation 'com.facebook.shimmer:shimmer:0.5.0'
5053
}

news-app/app/src/main/java/com/chimemoo/newsapp/MainActivity.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import androidx.appcompat.app.AppCompatActivity;
44
import androidx.recyclerview.widget.LinearLayoutManager;
55
import androidx.recyclerview.widget.RecyclerView;
6+
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
67

78
import android.os.Bundle;
89
import android.util.Log;
10+
import android.view.View;
11+
12+
import com.facebook.shimmer.ShimmerFrameLayout;
913

1014
import java.util.List;
1115

@@ -18,6 +22,9 @@ public class MainActivity extends AppCompatActivity {
1822
NewsService newsService;
1923
List<NewsModel> listNews;
2024
RecyclerView rvNews;
25+
NewsAdapter newsAdapter;
26+
ShimmerFrameLayout shimmerNews;
27+
SwipeRefreshLayout swipeRefreshLayout;
2128

2229

2330
@Override
@@ -27,9 +34,22 @@ protected void onCreate(Bundle savedInstanceState) {
2734

2835
newsService = API.getHeadlineNews().create(NewsService.class);
2936

37+
swipeRefreshLayout = findViewById(R.id.swipeRefresh);
38+
shimmerNews = findViewById(R.id.shimmer_news);
3039
rvNews = findViewById(R.id.rv_news);
3140
rvNews.setLayoutManager(new LinearLayoutManager(this));
3241

42+
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
43+
@Override
44+
public void onRefresh() {
45+
shimmerNews.setVisibility(View.VISIBLE);
46+
shimmerNews.startShimmer();
47+
listNews.clear();
48+
rvNews.removeAllViewsInLayout();
49+
refresh();
50+
}
51+
});
52+
3353
refresh();
3454

3555
}
@@ -39,17 +59,33 @@ public void refresh() {
3959
newsCall.enqueue(new Callback<NewsResponseModel>() {
4060
@Override
4161
public void onResponse(Call<NewsResponseModel> call, Response<NewsResponseModel> response) {
62+
shimmerNews.stopShimmer();
63+
shimmerNews.setVisibility(View.GONE);
4264
NewsResponseModel newsResponse = response.body();
4365
listNews = newsResponse.getArticles();
44-
NewsAdapter newsAdapter = new NewsAdapter(listNews);
66+
newsAdapter = new NewsAdapter(listNews);
4567
rvNews.setAdapter(newsAdapter);
46-
Log.d("RESPONSE", "data : " + response.body().toString());
68+
newsAdapter.notifyDataSetChanged();
69+
swipeRefreshLayout.setRefreshing(false);
4770
}
4871

4972
@Override
5073
public void onFailure(Call<NewsResponseModel> call, Throwable t) {
5174
Log.d("RESPONSE", "GAGAL");
75+
swipeRefreshLayout.setRefreshing(false);
5276
}
5377
});
5478
}
79+
80+
@Override
81+
protected void onResume() {
82+
super.onResume();
83+
shimmerNews.startShimmer();
84+
}
85+
86+
@Override
87+
protected void onPause() {
88+
super.onPause();
89+
shimmerNews.stopShimmer();
90+
}
5591
}

news-app/app/src/main/java/com/chimemoo/newsapp/NewsAdapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public void onBindViewHolder(@NonNull NewsAdapter.ListViewHolder holder, int pos
3434

3535
Glide.with(holder.itemView.getContext())
3636
.load(news.getUrlToImage())
37-
.apply(new RequestOptions().override(200, 50))
37+
.apply(new RequestOptions().override(800, 400))
38+
.fitCenter()
3839
.into(holder.ivNews);
3940

4041
holder.tvTitle.setText(news.getTitle());

news-app/app/src/main/java/com/chimemoo/newsapp/NewsService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public interface NewsService {
77

8-
@GET("v2/top-headlines?country=us&apiKey=6f711c0871524df9bd47c6333402abca")
8+
@GET("v2/top-headlines?country=id&apiKey=6f711c0871524df9bd47c6333402abca")
99
Call<NewsResponseModel> newsHeadline();
1010

1111
}

news-app/app/src/main/res/layout/activity_main.xml

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,40 @@
77
android:padding="10dp"
88
tools:context=".MainActivity">
99

10-
<androidx.recyclerview.widget.RecyclerView
11-
android:id="@+id/rv_news"
10+
<com.facebook.shimmer.ShimmerFrameLayout
11+
android:id="@+id/shimmer_news"
1212
android:layout_width="match_parent"
13-
android:layout_height="wrap_content"
14-
android:layout_marginHorizontal="10dp"
15-
tools:listitem="@layout/item_news"
16-
app:layout_constraintStart_toStartOf="parent"
17-
app:layout_constraintTop_toTopOf="parent" />
13+
android:layout_height="match_parent"
14+
android:orientation="vertical">
15+
16+
<LinearLayout
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:orientation="vertical">
20+
21+
<include layout="@layout/item_news_shimmer_placeholder"/>
22+
<include layout="@layout/item_news_shimmer_placeholder"/>
23+
<include layout="@layout/item_news_shimmer_placeholder"/>
24+
<include layout="@layout/item_news_shimmer_placeholder"/>
25+
<include layout="@layout/item_news_shimmer_placeholder"/>
26+
<include layout="@layout/item_news_shimmer_placeholder"/>
27+
28+
</LinearLayout>
29+
</com.facebook.shimmer.ShimmerFrameLayout>
30+
31+
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
32+
android:id="@+id/swipeRefresh"
33+
android:layout_width="match_parent"
34+
android:layout_height="match_parent">
35+
36+
<androidx.recyclerview.widget.RecyclerView
37+
android:id="@+id/rv_news"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
tools:listitem="@layout/item_news"
41+
app:layout_constraintStart_toStartOf="parent"
42+
app:layout_constraintTop_toTopOf="parent" />
43+
44+
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
1845

1946
</androidx.constraintlayout.widget.ConstraintLayout>

news-app/app/src/main/res/layout/item_news.xml

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,37 @@
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
android:orientation="vertical"
55
android:layout_width="match_parent"
6-
android:layout_height="wrap_content">
6+
android:layout_height="wrap_content"
7+
android:layout_margin="10dp">
78

8-
<androidx.cardview.widget.CardView
9+
<LinearLayout
10+
android:padding="10dp"
911
android:layout_width="match_parent"
1012
android:layout_height="wrap_content"
11-
android:layout_marginBottom="10dp">
12-
<LinearLayout
13-
android:padding="10dp"
13+
android:orientation="vertical">
14+
<ImageView
15+
android:id="@+id/iv_news"
1416
android:layout_width="match_parent"
15-
android:layout_height="wrap_content"
16-
android:orientation="vertical">
17-
<ImageView
18-
android:id="@+id/iv_news"
19-
android:layout_width="match_parent"
20-
android:layout_height="200dp"
21-
android:background="#ddd"/>
22-
23-
<TextView
24-
android:id="@+id/tv_title"
25-
android:layout_width="match_parent"
26-
android:layout_height="wrap_content"
27-
android:layout_marginTop="5dp"
28-
android:textSize="24sp"
29-
android:textColor="@color/black"
30-
android:text="TITLE"/>
17+
android:layout_height="200dp"
18+
android:background="#ddd"
19+
android:scaleType="fitXY"/>
3120

32-
<TextView
33-
android:id="@+id/tv_content"
34-
android:layout_width="match_parent"
35-
android:layout_height="wrap_content"
36-
android:layout_marginTop="5dp"
37-
android:textSize="14sp"
38-
android:text="Lorem Ipsum"/>
39-
</LinearLayout>
21+
<TextView
22+
android:id="@+id/tv_title"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
25+
android:layout_marginTop="5dp"
26+
android:textSize="24sp"
27+
android:textColor="@color/black"
28+
android:text="TITLE"/>
4029

41-
</androidx.cardview.widget.CardView>
30+
<TextView
31+
android:id="@+id/tv_content"
32+
android:layout_width="match_parent"
33+
android:layout_height="wrap_content"
34+
android:layout_marginTop="5dp"
35+
android:textSize="14sp"
36+
android:text="Lorem Ipsum"/>
37+
</LinearLayout>
4238

4339
</androidx.cardview.widget.CardView>

0 commit comments

Comments
 (0)