so I want to have a X number of TextView which is programmatically created and added to its parrent layout, and set each textView with different text depends on its id.
I already manage to created and added X number of TextView into its parrent Layout ,although I'm not sure do I manage to give each of them different id which I created manually with simple string manipulation and store them inside a HashMap.
next step, I try to get each TextView with findViewById()
using the id from HashMap and setText each TextView but I have no idea why my TextView is still blank. I already try to hardcode the text directly when the TextView is created but it still give me blank view, but this seems only happen with TextView, I also tried add X number ImageView programmatically with the same way I add the TextView, and setImageResourse depends on each id, and it works
honestly, I dont know where my mistake and I hope you can point me in the right direction .Thanks
I'm using minimal SDK Jelly Bean 4.2.2(API 17)
here's my onCreate()
int allEventNumber,imageId,textNameId,textDateId,textTappedId = -1;
LinearLayout layoutUtama,eventLayout,detailLayout;
ImageView thumbnailImageView;
TextView textViewName,textViewDate,textViewTapped;
String imageIdKey,textNameIdKey,textDateIdKey,textTappedIdKey,textDate,textTapped,textName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
HashMap<String,Integer> keyId = new HashMap<String,Integer>();
keyId = initAll(keyId);
fillAllById(allEventNumber,keyId);
}
initAll(HashMap)
public HashMap<String,Integer> initAll(HashMap<String,Integer> hMap){
allEventNumber = 6; //hardcode
layoutUtama = (LinearLayout) findViewById(R.id.layout_utama);
hMap = insertLayout(hMap,allEventNumber);
return hMap;
}
insertLayout(HashMap, int)
public HashMap<String,Integer> insertLayout(HashMap<String ,Integer> hMap, int jumlahEvent){
//
for (int i =1;i<= jumlahEvent;i++){
eventLayout = new LinearLayout(this);
eventLayout.setOrientation(LinearLayout.HORIZONTAL);
eventLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT));
thumbnailImageView = new ImageView(this);
imageIdKey = "key_image_view_" + i;
imageId = View.generateViewId();
hMap.put(imageIdKey,imageId);
thumbnailImageView.setId(imageId);
thumbnailImageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
detailLayout = new LinearLayout(this);
detailLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15,0,0,0);
detailLayout.setLayoutParams(layoutParams);
detailLayout.setGravity(Gravity.CENTER);
textViewName = new TextView(this);
textNameIdKey = "key_text_view_name_" + i;
textNameId = View.generateViewId();
hMap.put(textNameIdKey,textNameId);
textViewName.setId(textNameId);
textViewName.setGravity(Gravity.LEFT);
textViewName.setTextSize(R.dimen.event_name_text_size);
textViewName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
detailLayout.addView(textViewName);
eventLayout.addView(thumbnailImageView);
eventLayout.addView(detailLayout);
layoutUtama.addView(eventLayout);
}
return hMap;
}
fillAllById(int,HashMap)
public void fillAllById(int allEventNumber,HashMap<String,Integer> hMap){
for (int i=1;i<=allEventNumber;i++){
imageIdKey = "key_image_view_" + i;
textNameIdKey = "key_text_view_name_" + i;
imageId = hMap.get(imageIdKey);
textNameId = hMap.get(textNameIdKey);
thumbnailImageView = (ImageView) findViewById(imageId);
textViewName = (TextView) findViewById(textNameId);
thumbnailImageView.setImageResource(R.drawable.thumbnail_event3);
textName = "Name Event";
textViewName.setText(textName);
}
}
and my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.example.hansel.tapbandung_v00.Event">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<LinearLayout
android:id="@+id/layout_utama"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp">
</LinearLayout>
</ScrollView>
</RelativeLayout>