지난 포스트에 이어서 작업을 진행합니다.
1. 메뉴 아이콘 생성
drawable에 폴더 우클릭 -> New -> Vector Asset을 클릭하여 Asset Studio를 실행해 줍니다.
아래와 같은 Asset Studio에서 Clip Art 그림을 클릭하면 Android에서 제공해 주는 icon을 찾아서 사용할 수 있습니다.
Select Icon 화면에서 메뉴에 app을 입력 후 맨 오른쪽 아이콘을 선택하고 OK 버튼을 눌러서 app icon을 불러옵니다 다시 Asset Studio로 돌아와서 Name을 ic_app이라고 입력한 후 Next -> Finish 버튼을 눌러 아이콘을 만들어 줍니다.
위 과정을 반복해서 chart, money, build, arrow back icon을 생성해 줍니다. 이제 메뉴에서 사용할 아이콘 작업은 끝났습니다. 메뉴는 activy_dayprice Layout을 작성하면서 함께 작업합니다.
2. DayPriceActivity 및 Layout 작성
1) activity_dayprice Layout
아래 코드를 넣어줍니다. 코드 내용은 MaterialToolbar와 RecyclerView를 생성하는 내용입니다. Toolbar에서 top_app_bar_menu를 사용하도록 합니다.
<?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:id="@+id/dayprice"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DayPriceActivity">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:title="MegaBomb"
app:navigationIcon="@drawable/ic_back"
app:menu="@menu/top_app_bar_menu"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/dayPriceRV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="65dp"
app:layout_constraintStart_toStartOf="@id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
아직 메뉴가 만들어지지 않아서 오류를 나타냅니다. 빨간색 라인에 커저를 위치시키고 경고 아이콘을 눌러 Create menu resource file을 선택해 줍니다.
New Resource File Dialog에서 내용을 변경 없이 OK를 버튼을 누르고 나오면 아래와 같이 메뉴 폴더, 메뉴 Resource 파일이 자동으로 생성됩니다.
top_app_bar_menu Layout에 아래코드를 넣어줍니다. 그러면 이미 만들어준 아이콘을 로드하여 메뉴를 구성해 줍니다.
이로써 메뉴구성작업과 동시에 activity_dayprice Layout 작업을 완료하였습니다.
<?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:icon="@drawable/ic_chart"
app:showAsAction="always"
android:title="Chart"/>
<item
android:icon="@drawable/ic_account"
app:showAsAction="always"
android:title="Account"/>
<item
android:icon="@drawable/ic_build"
app:showAsAction="always"
android:title="Setting"/>
</menu>
이제 DayPriceActivity Class의 코드를 구현합니다. 이전에 Empty Views로 만들어진 상태입니다. 여기를 코드를 입혀줍니다.
3. DayPriceActivity Class
DayPriceActivity Class 선언부에 아래 변수들을 선언해 줍니다.
private static final String TAG = "DayPriceActivity";
List<DayPrice> dayPriceList = new ArrayList<>();
DayPriceAdapter adapter;
RecyclerView recyclerView;
LinearLayoutManager layoutManager;
MaterialToolbar toolbar;
DayPriceActivity Class에서 처리할 기능은 아래와 같습니다.
- activity_dayprice View 및 Adapter 매핑
- Main화면 복귀를 위한 toolbar Listener 추가
- 메인에서 넘어온 종목코드를 getExtra로 받은 후 일자별 주가 Request
1) activity_dayprice View 및 Adapter 매핑
onCreate Method 부분에 다음 코드를 추가하여 View Control 및 기 생성한 DayPriceAdapter를 인식시켜 줍니다.
recyclerView = findViewById(R.id.dayPriceRV);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new DayPriceAdapter(dayPriceList);
recyclerView.setAdapter(adapter);
2) Main화면 복귀를 위한 toolbar Listener 추가
toolbar의 Back 화살표를 누르면 dayprice activity를 종료하도록 합니다. 그러면 Manifest에서 DayPriceActivity를 Main의 Child로 정의해 줬기 때문에 Main으로 복귀합니다.
toolbar = findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
3) 메인에서 넘어온 종목코드를 getExtra로 받은 후 일자별 주가 Request
Main에서 putExtra로 넘겨준 종목코드를 받아서 fetchDayPrice를 호출합니다.
String stockCode = "StockCode not Set";
Bundle extras = getIntent().getExtras();
if(extras != null) {
stockCode = extras.getString("stockCode");
}
fetchDayPrice(stockCode);
fetchDayPrice Function의 코드는 아래와 같은데 Query Map fid_input_iscd를 Main에서 넘겨받은 stockCode로 설정해 주고 getDaylyPrice를 요청해서 Response를 받습니다.
private void fetchDayPrice(String stockCode) {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("authorization","Bearer 발급받은 Access Token");
headers.put("appKey", "나의 appKey");
headers.put("appSecret","나의 appSecret");
headers.put("tr_id","FHKST01010400");
Map<String, String> map = new HashMap<>();
map.put("fid_cond_mrkt_div_code","J");
map.put("fid_input_iscd",stockCode);
map.put("fid_org_adj_prc","0000000001");
map.put("fid_period_div_code","D");
RetrofitClient.getRetrofitClient().getDaylyPrice(headers,map).enqueue(new Callback<DayPriceRoot>() {
@Override
public void onResponse(Call<DayPriceRoot> call, Response<DayPriceRoot> response) {
Log.d(TAG, "ON Response : Server Response: " + response.toString());
Log.d(TAG, "ON Response : received Information: " + response.body().toString());
dayPriceList.addAll(response.body().getOutput());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<DayPriceRoot> call, Throwable throwable) {
Log.e(TAG, "Something went wrong " + throwable.getMessage());
}
});
}
이제 activity_mail Layout의 toolbar를 material toolbar로 바꿔주면 전체 작업이 완료되었습니다. 프로그램을 Run시켜 결과를 확인합니다.
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="MegaBomb"
app:navigationIcon="@drawable/ic_app"
app:menu="@menu/top_app_bar_menu"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="65dp"
app:layout_constraintStart_toStartOf="@id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>