Creating two views on runtime and placing one right of first one - android

So I'm creating ImageView and TextView in the runtime and I want the TextView to be placed right of the ImageView.
I'm trying this simple code:
private void loadContent(Module module) {
this.removeAllViewsInLayout();
this.addView(getModuleIconView(module));
this.addView(getModuleNameView(module));
}
private ImageView getModuleIconView(Module module) {
ImageView view = new ImageView(context);
view.setImageResource(module.getIconResId());
android.view.ViewGroup.LayoutParams params = new LayoutParams(120, 120);
view.setLayoutParams(params);
view.setId(1); //seting some id for the view
return view;
}
private TextView getModuleNameView(Module module) {
TextView view = new TextView(context);
view.setText(module.getName());
params.addRule(RelativeLayout.RIGHT_OF, 1); //hoping for view to be placed right of the ImageView which id was set to 1
return view;
}
What Do I do wrong here? The TextView is placed on my ImageView ignoring the RIGHT_OF
Need to mention the class inherits from RelativeLayout

It was my stupid mistake. Third method leaks setLayoutParams
So correct method body is:
private TextView getModuleNameView(Module module) {
TextView view = new TextView(context);
view.setText(module.getName());
params.addRule(RelativeLayout.RIGHT_OF, 1);
view.setLayoutParams(params);
return view;
}

Related

HorizontalScrollView - Adding Item at specific position

I want that the user can scroll throw my HorizontalScrollView and if he press my Button, a TextView will be shown on the current position of my HorizontalScrollView.
So far I already know how to present a TextView, but not on a specific position...
TextView textView;
textView = new TextView(MainActivity.this);
textView.setText(editText.getText().toString());
linearLayout.addView(textView);
Any help is welcomed!
Do some thing like this ,
final HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scroller);
final RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
findViewById(R.id.addButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView iv = new TextView(Act2.this);
iv.setText(new Date().toString());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 400);
params.leftMargin = scrollView.getScrollX();
params.topMargin = scrollView.getScrollY();
container.addView(iv, params);
}
});
And the relative layout should be inside the HorizontalScrollView. This will add a textview exactly in the current position , but you should also write code for avoiding the overlapping the TextViews.

managing visibility of relativelayout children

I had to implement the expand/collapse(show/hide) a particular view on click of a some other view..Finally got it working but with a small problem.
I have a relative layout with two children: first is the textview and second is a linear layout.
On the click event of the textview i set the visibility(VISIBLE or GONE) of the following linear layout.
Now the problem is that after the linear layout is visible it somehow manages to hide the textview..
I tried textview.bringToFront() but it just makes the textview overlap the first row of the linearlayout ie the textview comes on top of the linear layout content.
I tried putting the textview in a linearlayout, but it makes no difference.
I tried setting the linear layout as BELOW. All in vain..
I know the textview exists because when i click the first row(which is overlapping the textview) the click event gets fired..
All i want is that no matter what my textview should be visible and the linear layout must take its position below the textview if it is visible..
EDIT
RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
RelativeLayout rParent = (RelativeLayout) tv.getParent();
LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);
if(lInner.isShown())
{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.GONE);
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
}
else{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.VISIBLE);
lInner.setTop(tv.getBottom());
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
}
}
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);
HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
LinearLayout lRow = new LinearLayout(getActivity());
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lRow.setLayoutParams(lparams1);
lRow.setOrientation(LinearLayout.HORIZONTAL);
TextView txtLbl = new TextView(getActivity());
txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
TextView txtVal = new TextView(getActivity());
txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
String Lbl = RowItr.next();
txtLbl.setText(Lbl);
if(rowcnt % 2 != 0)
lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
else
lRow.setBackgroundColor(Color.parseColor("#ffffff"));
txtVal.setText(MySet.get(Lbl));
lRow.addView(txtLbl);
lRow.addView(txtVal);
lRow.setPadding(10, 10, 10, 10);
lContent.addView(lRow);
rowcnt++;
}
lContent.setVisibility(View.GONE);
wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);
well you can store your LinearLayout as an instance variable, and simply call layout.setvisibility(View.GONE); in your onClick method. Doubt theres any other solution unless you want to save the parent(the layout that both of your views are attached to) and perform findViewById inside onClick or call getChildAt(1) orso
Got a solution.. i tried setting margins..and it all worked out well.. te handle and content both are visible without hiding any other views..

Android programmatic relative layout: BELOW rule not working?

I want to create a layout (see class RosterPlayerView below) that comprises an image with text below it and then instantiate that view multiple times in a relative layout. I used relative layout instead of linear as the layout will become more complex.
When I first ran the code below (but without the setId calls) the text appeared above the image. Thanks to this stack overflow article I discovered that relative layout needs unique widget ids to work. But when I added the setId() calls the text view is not displayed at all.
What am I doing wrong?
public class RosterPlayerView extends RelativeLayout {
ImageView imageView;
TextView textView;
static int layoutId = 100;
public RosterPlayerView(Context context, int playerId, Drawable photo) {
super(context);
imageView = new ImageView(context);
textView = new TextView(context);
addView(imageView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setId(layoutId++);
RelativeLayout.LayoutParams timeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
timeLayoutParams.addRule(RelativeLayout.BELOW, imageView.getId());
addView(textView, timeLayoutParams);
imageView.setImageDrawable(photo);
textView.setId(layoutId++);
textView.setText("0:00");
}
}
a LinearLayout would be an awful lot simpler for what you are trying to do. So would inflating an XML layout, for that matter.
Try to set the Id of you imageView before adding it to the layout.
You can also create a LinearLayout with the imageView and textView inside before adding it to the RelativeLayout

Combining imageView and TextView in a List

Hi I am developing an app in android where I use a subclass that extends BaseExpandableListAdapter. Right now I have problem to combine ImageView and TextView in the list that is showing. Yesterday I found this link on stackoverflow that helped me to make this combination.
Overlay text over imageview in framelayout programmatically - Android
So it works FINE! - until I click on a listItem. The app chrash and the logcat tells me that
"android.widget.RelativeLayout" cannot be cast to android.widget.ImageView. This exception comes the getGroupView() in the class that extends BaseExpandableListAdapter . Why does this happen? (RelativeLayout extends View).
Am I completely on the wrong way when I try to return a RelativeLayout instead of an ImageView?
Here's my code from getGroupView (I'm a bit messy because I am in a teststate) :
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ImageView row = (ImageView) convertView;
RelativeLayout rLayout = new RelativeLayout(mContext);
LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT
,LayoutParams.FILL_PARENT);
rLayout.setLayoutParams(rlParams);
if(row == null) {
row = new ImageView(mContext);
}
row.setImageResource(R.drawable.ic_launcher);
row.setScaleType(ImageView.ScaleType.FIT_START);
RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
TextView text=new TextView(mContext);
text.setText("GOLDEN Gate");
text.setTextColor(Color.WHITE);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setLayoutParams(tParams);
rLayout.addView(row);
rLayout.addView(text);
return rLayout;
}
The statement ImageView row = (ImageView) convertView; gives the exception because u r trying to convert the RelativeLayout to ImageView.
You must return the convertView in getViewGroup() method but you are returning the RelativeLayout i.e., return rLayout;
Solution :
Add a ImageView and TextView inside a RelativeLayout. Set that layout for ListItem by inflating it inside getView() method and assign the inflated layout to convertView. Return the convertView in getView() method;

Programmatically Position Views in RelativeLayout

I want my TextView to appear below my ImageView in a RelativeLayout that is going into a GridView. I've tried:
public override View GetView(int position, View convertView, ViewGroup parent)
{
RelativeLayout rl = new RelativeLayout(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
ImageView imageView;
TextView tv;
imageView = new ImageView(context);
imageView.LayoutParameters = new GridView.LayoutParams(250, 250);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(8, 8, 8, 8);
imageView.SetImageResource(thumbIds[position]);
imageView.Id = position;
lp.AddRule(LayoutRules.Below, position);
lp.AddRule(LayoutRules.CenterHorizontal);
tv = new TextView(context);
tv.Text = stringIds[position].ToString();
tv.SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
tv.SetTextColor(Android.Graphics.Color.WhiteSmoke);
tv.LayoutParameters = lp;
rl.AddView(imageView);
rl.AddView(tv);
return rl;
}
But the TextView always shows up on top of the ImageView.
Check out this question.
When you call rl.AddView(tv), you should include the LayoutParams rl.AddView(tv, lp).
You have to use
imageView.setLayoutParams(lp) in order to assign the params to your view.
How do you setLayoutParams() for an ImageView?
Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. Then I made that the background for the ImageView.

Categories

Resources