Android : how to update the dynamic created textview backgroud using getTag()

on Wednesday, December 10, 2014


I have created a textView dynamically based on number of fields getting from server. below textview i have one hidden view, on click of that specific textview , wants to show the view.


below is code, but not able to show/hide specific view.



private void addChilds(String value) {


final LinearLayout addChildsLayout = new LinearLayout(this);
addChildsLayout.setOrientation(LinearLayout.VERTICAL);
addChildsLayout.setId(15);

// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
LinearLayout.LayoutParams addChildsLayoutLp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);

addChildsLayout.setLayoutParams(addChildsLayoutLp);

// Creating a new TextView
final TextView tv = new TextView(this);
tv.setText(value);
tv.setTextSize(20);
tv.setTextColor(Color.parseColor("#000000"));
tv.setPadding(40, 40, 40, 40);
tv.setTag(value);

// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);

// Setting the parameters on the TextView
tv.setLayoutParams(lp);

tappedView = new View(this);
tappedView.setBackgroundColor(Color.parseColor("#8461A9"));
tappedView.setVisibility(View.GONE);
tappedView.setTag(value);

// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
LinearLayout.LayoutParams tappedViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 10);

tappedView.setLayoutParams(tappedViewParams);

tv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// need to show tapped view which is just below the selected textview
v = (View) v.getTag();
tappedView.setVisibility(View.VISIBLE);
}
});

// Adding the TextView to the RelativeLayout as a child
addChildsLayout.addView(tv);
addChildsLayout.addView(tappedView);
innerLayout.addView(addChildsLayout);

}


Thanks in advance.


0 comments:

Post a Comment