package com.codingrakitan.homegrab;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.androidnetworking.widget.ANImageView;
public class MainActivity extends AppCompatActivity {
private LayoutInflater inf;
private LinearLayout root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = findViewById(R.id.root);
inf = getLayoutInflater();
String[] s = getResources().getStringArray(R.array.judul);
String[] s2 = getResources().getStringArray(R.array.des);
String[] gm = getResources().getStringArray(R.array.gambar);
int no = 0;
LinearLayout ll = new LinearLayout(this);
for (int i =0;i<s.length;i++){
no++;
if (no==1){
LinearLayout ly = BuatLinearLayout();
ll = ly;
}
if (no==2){
no =0;
}
BuatCard(s[i], s2[i], ll, gm[i], no);
}
}
private LinearLayout BuatLinearLayout() {
LinearLayout ly = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ly.setLayoutParams(lp);
ly.setOrientation(LinearLayout.HORIZONTAL);
root.addView(ly);
return ly;
}
private void BuatCard(String judul, String des, LinearLayout ly, String url_gm, int no) {
View v = inf.inflate(R.layout.card, null);
TextView j = v.findViewById(R.id.textView);
TextView j2 = v.findViewById(R.id.textView2);
ANImageView image = v.findViewById(R.id.gm);
Typeface typeface = Typeface.createFromAsset(getAssets(), "font/arial.ttf");
j.setTypeface(typeface);
j.setText(judul);
j2.setText(des);
image.setImageUrl(url_gm);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT,
1.0f);
if(no==1){
lp.setMargins(30, 20,20, 30);
}else{
lp.setMargins(20, 20,30, 30);
}
v.setLayoutParams(lp);
ly.addView(v);
}
}