Cannot center text when programmatically creating view from adaptor - android

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;

Related

Use SnapHelper to Scroll to a Child view in a recyclerview item

In my recyclerview onCreateViewholder method I create a dynamic which would have unspecified number of text views inside a linear layout
if (sessionManager.getBookScrolOrientation() == AppConstents.HORIZONTAL) {
LinearLayout linearLayout = new LinearLayout(context);
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
Spannable spannable = getVerseSpanableHorizontal(pidreference.get(position) , cidreference.get(position));
Pagination pagination = new Pagination(spannable,
pageWidth,
pageHight,
textPaint,
lineSpacingMultiplier,
lineSpacingExtra,
isTextPaddingIncluded);
List<CharSequence> list = pagination.getPages();
for (CharSequence charSequence : list){
TextView tv = new TextView(linearLayout.getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Tools.getScreenWidth() , ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(params);
tv.setText(charSequence);
linearLayout.addView(tv);
}
MainViewHolder viewHolder = new MainViewHolder(linearLayout);
return viewHolder;
}
When i use SnapHelper with this recyclerview it scrolls to the center of linear layout not the next textview. How can i make it scroll to the next textview inside the linearlayout?
If I understand correctly, the problem is that SnapHelper snaps the center of the current view to the center of the recycler. As the docs say:
The implementation will snap the center of the target child view to the center of the attached RecyclerView.
If you want the the current view to snap to the start of the recycler use a libraray such as this one. Import it with gradle and activate it like this:
GravitySnapHelper(Gravity.START).attachToRecyclerView(recyclerview);

How to Add a Child View to the Center of a ViewGroup

I have created a custom circular ProgressBar following this. Now I want to add this progress bar to the center of my layout. The problem is that it is added to the top left of the activity, no matter how I change LayoutParams attributes passed to addView(). The code is as follows:
// Create a progress bar to display while the list loads
mProgressBar = new DualProgressView(getApplicationContext());
ViewGroup root = findViewById(android.R.id.content);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(500, 500, Gravity.CENTER);
root.addView(mProgressBar,params);
make your progress bar height and width as wrap_content, or, say 40sp in xml.
set attribute android:layout_gravity="center" to your ProgressBar within your LinearLayout.
or programmatically, try this:
ViewGroup layout = (ViewGroup) findViewById(android.R.id.content).getRootView();
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout ll = new LinearLayout(thisActivity);
ll.setGravity(Gravity.CENTER);
ll.addView(progressBar);
layout.addView(ll,params);

how to add a horizontal divider programmatically

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));

Set framelayout margin and its children width, height and position

I am currently having a programatically created framelayout with a imageview and a text view. By original layout is relative layout.
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rel);
FrameLayout fr = new FrameLayout(this);
fr.setBackgroundResource(R.drawable.question_bar_icon);
ImageView b1 = new ImageView(this);
TextView b2 = new TextView(this);
b1.setBackgroundResource(R.drawable.mcq);
fr.addView(b1);
fr.addView(b2);
layout.addView(fr);
Now i am unable to resize the imageview and the button, also m unable to position them, in the sence textview in the center and imageview in the verticle center and left positioned.
I tried layout params, but not working, any help would be greatly appreciated!
Try using the overloaded version of addView that takes a LayoutParameter as well in every case you are programatically adding a view, otherwise it is inserted to the ViewGroup with default layout parameters. (P.S. you can also control the order of insertion).
For example for the ImageView you wanted to be in the center of the frame layout:
int width = FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
int height= FrameLayout.LayoutParams.WRAP_CONTENT; //or whatever you want
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, height, Gravity.CENTER);
fr.addView(b1,lp);
Do the same for the other view, and most importantly, do the same, using RelativeLayout.LayoutParams for inserting the frame element itself into your RelativeLayout.

android textview fill parent

I have a simple ExpandalbeList. For the group header view, I'm using a simple TextView, I am setting the background of the text to a drawable. I have the textview parameters set to FILL_PARENT, but the background of the TextView only covers the width of the text. I realize that I can put this all inside of a Linear Layout, but that just seems like a waste. isn't there a way to make the bounds of the text view stretch to the edge of the listview?
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(PulseSearchResults.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(6, 0, 0, 0);
textView.setTextColor(Color.BLACK);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setTextSize(16.0f);
textView.setBackgroundResource(R.drawable.search_section_group);
return textView;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
thx
Ben
I just figured out that if i set the width of the listview to FILL_PARENT, it solves the issue...

Categories

Resources