in the bleow posted code, i am trying to add a horizontal divider after each added view to the linearlayout as shown in the code.
the problem i have is, at run time the divider is not showing
would please let me know why the divider is not showing and how to make it appears?
code:
private void inflateView(String bez, String ges) {
LinearLayout linLay = (LinearLayout) findViewById(R.id.versicherungsListeActivity2mod_linLay_meineDocList_container);
//divider
View viewDivider = new View(this);
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
viewDivider.setBackgroundColor(Color.parseColor("#000000"));
LayoutInflater inflator = this.getLayoutInflater();
View view = inflator.inflate(R.layout.versicherung_docs_row_model, null);//<<<<<< this is the view i want to add to the map as a key
ImageView imgViewLogo = (ImageView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_imgVie_logo);
TextView texVieBez = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docBezeichnung);
TextView texVieExtraItem = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_addMoreDocs);
TextView texVieGes = (TextView) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_texVie_docGesellschaft);
Button btnMore = (Button) view.findViewById(R.id.versicherungslisteactivity2_docs_lisvie_row_model_btn_more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc, this.getTheme()));
} else {
imgViewLogo.setImageDrawable(this.getResources().getDrawable(R.drawable.insurance_details_doc));
}
texVieBez.setText(bez);
texVieGes.setText(bez);
btnMore.setVisibility(View.VISIBLE);
linLay.addView(view);
linLay.addView(viewDivider);
}
viewDivider has height WRAP_CONTENT and as the view is empty, its height is calculated to 0.
You have to set desired height of the divider.
int dividerHeight = getResources().getDisplayMetrics().density * 1; // 1dp to pixels
viewDivider.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dividerHeight));
Related
I am using an adaptor to generate a view for each of several items. The view needs to show an image with centered text below it. I create a linear layout and add an image view and text view.
To show the problem I have changed the background colors to green for the linear layout and red for the text view...
Obviously the text is showing left aligned instead of centered. Here is the simple code in the adaptor GetView method...
public override View GetView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(_context);
LinearLayout.LayoutParams imageViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
imageView.LayoutParameters = imageViewParam;
imageView.Id = 1000 + position;
imageView.SetImageResource(Resource.Drawable.Code);
TextView textView = new TextView(_context);
LinearLayout.LayoutParams textViewParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent);
textView.LayoutParameters = textViewParam;
textView.Text = OPTIONS_TEXT[position];
textView.SetBackgroundColor(Color.Red);
textView.SetForegroundGravity(GravityFlags.CenterHorizontal);
LinearLayout linear = new LinearLayout(_context);
linear.LayoutParameters = new GridView.LayoutParams(200, 172);
linear.SetBackgroundColor(Color.Green);
linear.Orientation = Orientation.Vertical;
linear.AddView(imageView);
linear.AddView(textView);
return linear;
}
As far as I can work out, you cannot set the layout_gravity setting when generating the view programmatically and so I tried setting the foreground gravity but has no effect at all.
I have tried everything I can think of but nothing gets that text centered!
Set gravity center to LinearLayout.LayoutParams of TextView i.e.
textViewParam.gravity = Gravity.CENTER;
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.
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..
I open a popu window like this:
mInfoPopup = new PopupWindow(layout, 400, 600, true);
mInfoPopup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
The window then gets the exact size specified (400x600) and does NOT adjust its size to its content. What do I need to change so that the popup window will actually wrap around its contents?
Simply changed the way you create it by:
PopupWindow popupMenu = new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
PopupWindow popupWin = new PopupWindow(mContext);
// WRAP_CONTENT works well for height, but not for width.
popupWin .setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
// Measure layout here, then we can get measureHeight.
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
popupWin .setWidth(layout.getMeasuredWidth());
popupWin .setContentView(layout);
popupWin .showAsDropDown(anchorView,0 ,0, Gravity.NO_GRAVITY);
I found a solution that works for me (I am working with API 10), you can use it:
popupWindow.setWindowLayoutMode(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);
If you don't set height/width or set 0 it won't work. Example:
...
private Button button;
protected void onCreate(Bundle savedInstanceState) {
...
attached_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(getApplicationContext());
popupWindow.setContentView(popupView);
popupWindow.setWindowLayoutMode(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(1);
popupWindow.setWidth(1);
popupWindow.setFocusable(true);
Button dismiss = (Button) popupView
.findViewById(R.id.dismiss);
dismiss.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(button);
}
});
I hope to help you.
Following code worked for me.
LayoutInflater layoutInflater = LayoutInflater.from(context);
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(ListPopupWindow.WRAP_CONTENT);
popup.setHeight(ListPopupWindow.WRAP_CONTENT);
Here, used popup.setWidth(ListPopupWindow.WRAP_CONTENT) instead of popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT) in my case.
don't use RelativeLayout in layout, replace LinearLayout, maybe it's working.
popview = LayoutInflater.from(context).inflate(R.layout.pop_citypicker, null);
popwindow = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Below code is use-full for decreasing layout width and height pop-window
and i used in that custom layout with aligned center
final PopupWindow popupWindow = new PopupWindow(getActivity());
PopupMenu popup = new PopupMenu(getActivity(), v, Gravity.CENTER);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popmenu1t1, null);
l1 = (LinearLayout) view.findViewById(R.id.atoz);
l2 = (LinearLayout) view.findViewById(R.id.ztoa);
l3 = (LinearLayout) view.findViewById(R.id.phtol);
l4 = (LinearLayout) view.findViewById(R.id.pltoh);
l5 = (LinearLayout) view.findViewById(R.id.dhtol);
l6 = (LinearLayout) view.findViewById(R.id.dltoh);
l7 = (LinearLayout) view.findViewById(R.id.dotor);
l8 = (LinearLayout) view.findViewById(R.id.drtoo);
int width = 900;
int height = 400;
try {
WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = wm.getDefaultDisplay().getHeight();
} catch (Exception e) {
e.printStackTrace();
}
popupWindow.setWidth(width*3/6);
popupWindow.setHeight(height*3/8);
popupWindow.setFocusable(true);
popupWindow.setContentView(view);
popupWindow.setBackgroundDrawable(null);
popupWindow.setOutsideTouchable(true);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
These two lines not use full for now
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
Happy coding friends..
final PopupWindow newPartWindow = new PopupWindow(newPartIconView, 1, 1, false);
newPartWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
This worked for me, we need the width/height to be initialized at 1 to make the wrap content work.
I had a similar problem but even setting the size in new PopupWindow(...) wasn't working.
I had an image as background of the popup and I solved putting it as background of the layout (I'm using the new ConstraintLayout) with android:background. The image is a 9-Patch image so it resize in a nice way.
For me it depended strangely on what did I pass as a contentView to the PopupWindow constructor:
new PopupWindow(layout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
When it was a LinearLayout without child views and a fixed width and height, passing the width=height=WRAP_CONTENT to the PopupWindow constructor did not work - I got 0x0 size.
When I added a child to that light the width=WRAP_CONTENT started working as expected, but the height=WRAP_CONTENT was working more like MATCH_PARENT, taking the full height of the screen.
After I changed the contentView to a FrameLayout or CardView it started working with width=height=WRAP_CONTENT properly.
I have created a Layout file for an activity. In this layout I have created a LinearLayout with a textview and an edittext.
Now I want to create additional LinearLayouts that will look and contain the exact same views that my original LinearLayout, but with different texts. I also want to do it programmatically during run because the amount of these LinearLayout will vary between runs.
I've read some about inflaters but I don't understand them enough to use them.
I'm thinking something like this, obviously the code is wrong, but hopefully you understand what I want to do:
LinearLayout llMain = (LinearLayout)findViewById(R.id.mainLayout);
LinearLayout llToCopy = (LinearLayout)findViewById(R.id.linearLayoutToCopy);
for(int player = 0; player < size; player++)
{
LinearLayout llCopy = llToCopy.clone();
TextView tv = (TextView)llCopy.getChildAt(0);
tv.setText(players.get(player).getName());
llMain.addView(llCopy);
}
There are several ways to accomplish this.
A quick and easy approach is to inflate a new layout in every iteration of the loop:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);
for (int i = 0; i < 10; i++) {
View child = inflater.inflate(R.layout.child, null);
TextView tv = (TextView) child.findViewById(R.id.text);
tv.setText("Child No. " + i);
parent.addView(child);
}
setContentView(parent);
Another (more elegant) solution is to create a separate class extending LinearLayout:
public class ChildView extends LinearLayout {
private TextView tv;
public ChildView(Context context) {
super(context);
View.inflate(context, R.layout.child, this);
tv = (TextView) findViewById(R.id.text);
}
public void setText(String text) {
tv.setText(text);
}
}
Now you can create a ChildView in every iteration of your loop and set the text via the setText(String text) method:
for (int i = 0; i < 10; i++) {
ChildView child = new ChildView( this );
child.setText("Child No. " + i);
parent.addView(child);
}
You can achieve that by using layout inflater
get the layout inflatter by using this
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout newlayout = inflater.inflate(R.layout.yourlayout, null);
// newlayout is the copy of your layout and you can use it and to get
// the textview and edittext do it like this
TextView text = (TextView) newlayout.findView(R.id.yourtextviewid);
text.setText("new text");
EditText et = (EditText) newlayout.findView(R.id.yourtextviewid);