Empty space at the end of RecyclerView - java

I use LinearLayoutManager to show RecyclerView list horizontally
XML
<android.support.v7.widget.RecyclerView
android:id="#+id/dailyOffersList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Code
dailyOffersList.adapter = DailyOffersListAdapter(dailyOffer)
dailyOffersList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
It is XML of item and it has width as wrap_content
https://codeshare.io/5XnldM
But there empty space int the end of list, and it happens only if it horizontally, like this
in the end

Change the match_parent in the xml to wrap_content.
match_parent is supposed to fill the whole with of the screen.

Related

Determine if a TextView requires scrolling

Inside CursorAdapter's bindView() I bind data to the following layout:
A TextView and two Buttons : "UP" and "DOWN".
The TextView is defined in XML like so:
<TextView
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:paddingTop="25dp"
android:scrollbars="vertical"
android:textAlignment="textStart"
android:textColor="#5c6284"
app:autoSizeMaxTextSize="40sp"
app:autoSizeMinTextSize="20sp"
app:autoSizeTextType="uniform" />
A vertical scrolling behavior is applied to the TextView, which is being controlled by the "UP and "DOWN" Buttons.
I would like to determine if the TextView requires scrolling ( is long enough to not fit its provided drawing area ) so that I can enable/disable the "UP" and "DOWN" buttons accordingly.
I'm currently reading BaseMovementMethod's scrollDown function, thinking of applying its measuring logic to my adapter, though I have the feeling that it should be much simpler. Maybe a built in behavior that I'm not aware of.
Is there a better way to achieve this, other than my suggested approach?
What I would do is put the textview inside a scrollview like so:
<ScrollView
android:id="#+id/scroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="test texts here"/>
</ScrollView>
In your activity, execute these lines:
boolean needScrolling = false;
if(scroller.getHeight() < tv_content.getHeight()) needScrolling = true;
You can use Static Layout class. If you set it up with your TextView's parameters you'll be able to calculate the height of the rendered text.
Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;
float spacingMultiplier = 1;
float spacingAddition = 0;
boolean includePadding = false;
StaticLayout myStaticLayout = new StaticLayout(text, myTextView.getPaint(), myTextView.getWidth(), alignment, spacingMultiplier, spacingAddition, includePadding);
float height = myStaticLayout.getHeight();
Then you can compare the height of your text and height of your TextView and figure out if it will require scrolling or not.
You can also try to manually create a Paint object with your min text size if myTextView.getPaint() approach does not work.
Calculate mTextView's height without data and with data and then compare it
mTextView.post(new Runnable() {
#Override
public void run() {
int lineHeight=mTextView.getCompoundPaddingBottom()+ mTextView.getCompoundPaddingTop()+mTextView.getLineHeight();
int height=mTextView.getHeight()-(mTextView.getCompoundPaddingTop()+mTextView.getLineHeight());
if (height>lineHeight){
}
}
});

Positional differences between creating views in XML or programatically

I have a trouble while creating a kind of view. Let's just say that i'm trying to create something similar to a clock. This means that we have to position numbers in a distance and angle from a center.
I tried to emulate a way to position two elements. A center ImageView and a clock number. In XML I have something similar to:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lyt_rootView"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/ivCenter"
android:background="#drawable/cid"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/ivNumber"
android:background="#drawable/add_big"
android:layout_marginLeft="-50dp"
android:layout_marginTop="50dp"/>
</RelativeLayout>
And it works pretty well, the images set as they should be, being completly centered the first image (ivCenter) and taking some margin from the center for positioning the second image.
Now I have to do the same programatically so I can create lots of imageViews simulating the numbers. So I have this code:
RelativeLayout lyt = (RelativeLayout) findViewById(R.id.lyt_rootView);
int avatarSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, getResources().getDisplayMetrics());
int distance = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
// center image
ImageView ivImageCenter = new ImageView(this);
ivImageCenter.setImageResource(R.drawable.cid);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(avatarSize,avatarSize);
lyt.addView(ivImageCenter, lp);
// first image (simulating a number in a clock)
ImageView ivNumber1 = new ImageView(this);
ivNumber1.setImageResource(R.drawable.add_big);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(avatarSize,avatarSize);
lp2.setMargins(distance, distance, 0, 0);
lyt.addView(ivNumber1, lp2);
but the result is not similar as the XML example. I don't see what I'm missing. The result of creating the views programatically is that the two imageViews are trying to position in a center space in the screen making the first image not centered to the screen.
What I'm missing?
Finally I solved it by creating a one pixel layout centered in XML that acts as a reference and programatically adding the views in that reference view.

How to set random TextView's position inside linear layout?

I have a linearlayout on xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
And I add dynamic 100 textviews inside it by code below:
llayout = (LinearLayout) findViewById(R.id.rootlayout);
for (int i = 0; i < 100; i++) {
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
int randomInt = new Random().nextInt(100) +1 ;
tv.setText(""+randomInt);
llayout.addView(tv);
}
The result is 100 textviews added and display vertically. This is not as my expect. I want these textviews display with random position inside the layout look like the image below:
How to do it? Thank you!
LinearLayout is for layouting its children linearly (as the name suggests).
As there is no RandomLayout, you can use a RelativeLayout with random left and top layout margins, or an AbsoluteLayout and set random x and y.
Edit: Avoid overlapping texts
Random positions can of course lead to overlapping and it would be up to you to adjust the positions or ignore positions too similar to previous ones. Or you might actually compare the bounding boxes (left, top, width, height) of the view you're about to add to all other views in the container and if there is any overlapping, find another place for it.
You can use tv.setX(position) and tv.setY(position) for show view on specific position

TableLayout - Remove space between columns

I'm having a problem with the TableLayout.
First, take a look at the screenshot:
As you can see, there is a pretty big space in the middle of the TableLayout.
I don't know how to reduce the space in the middle, so that the TableRows will have more Width to cover.
And also, I want to reduce the space between a TableRow and the one below it.
I'm adding the views to the TableLayout programmatically.
Also, I've already set the 'layout_weight' of the content of the TableRow to 1f:
TableRow tr = (TableRow) new TableRow(mTableLayout.getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
NormalCard card = new NormalCard();
card.setLayoutParams(new TableRow.LayoutParams(0, 740, 1f));
tr.addView(card);
mTableLayout.addView(tr, params);
XML declaration of the TableLayout:
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:stretchColumns="*"
android:dividerPadding="0dp"
android:showDividers="none"
android:divider="#null">
</TableLayout>
How do I reduce the space in the middle of the TableLayout.
And also, How to reduce the space between a TableRow and the one below it.
Thank you upfront.
Please specify your table layout xml file you have just shown images.
try this if it works
android:shrinkColumns="*"
android:stretchColumns="*"
in your TableLayout tag.
okay try to set padding as 0 to your table row if no of column in each row is same then specify table width as wrap_content.
Have a try with this:
params.setMargins(0, 0, 0, 0);

How to set the layout margins of edit text boxes?

In the table layout i have a tablerow and in that tablerow i have 6 edit text boxes and i want to set the layout margins for that 6 edit text boxes
TableLayout t1=(TableLayout)findViewById(R.id.table_layout01);
TableRow tr1=new TableRow(inventory.this);
tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
tr1.setBackgroundColor(Color.BLACK);
EditText ed6=new EditText(inventory.this);
//ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
/*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT);
editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/
ed6.setTextColor(Color.BLACK);
ed6.setBackgroundColor(Color.WHITE);
ed6.setText("1");
tr1.addView(ed6);
EditText ed7=new EditText(inventory.this);
//ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed7.setTextColor(Color.BLACK);
ed7.setBackgroundColor(Color.WHITE);
ed7.setText("2");
tr1.addView(ed7);
EditText ed8=new EditText(inventory.this);
//ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed8.setTextColor(Color.BLACK);
ed8.setBackgroundColor(Color.WHITE);
ed8.setText("3");
tr1.addView(ed8);
EditText ed9=new EditText(inventory.this);
//ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed9.setTextColor(Color.BLACK);
ed9.setBackgroundColor(Color.WHITE);
ed9.setText("4");
tr1.addView(ed9);
EditText ed10=new EditText(inventory.this);
//ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed10.setTextColor(Color.BLACK);
ed10.setText("5");
ed10.setBackgroundColor(Color.WHITE);
tr1.addView(ed10);
EditText ed11=new EditText(inventory.this);
//ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ed11.setTextColor(Color.BLACK);
ed11.setText("6");
ed11.setBackgroundColor(Color.WHITE);
tr1.addView(ed11);
t1.addView(tr1);
first of all something you should know: According to the Official Android Dev Pages, Views (and a TextView derives from View) do not support the setting of Margin, but ViewGroups (such as LinearLayout, RelativeLayout etc...) do.
So what you could do is the following:
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.setMargins(5, 5, 5, 5);
TextView view = new TextView(this);
view.setLayoutParams(params);
This would set the margin for all children to 5 pixels - I tried it and it worked for me (albeit with a LinearLayout with vertical alignment). Give it a shot and let me know if I can help further :) .
Cheers,
Ready4Fajir
EDIT:
I would try with the XML below (you'd, of course, update the id's etc.). The "magic" in the xml is that it distributes all available width evenly among the TextView's (and the EditText's on the second row).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The first "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="#+id/textView01" />
<!-- Here you'd add your other five TextView's accordingly -->
</LinearLayout>
<!-- The second "row" -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView 01"
android:id="#+id/editText01" />
<!-- Here you'd add your other five EditText's accordingly -->
</LinearLayout>
</LinearLayout>
In your Java code you could then access your EditText views like:
EditText editText01 = (EditText) findViewById(R.id.editText01);
editText01.setText("1");
I have now ignored the fact that you need to create your EditText's programatically. Do you really, really need to create them in Java? (Why?)
OLD ANSWER:
If you just want to set the layout margins to your EditText view i quess you could use the setMargins(left, top, right, bottom) function call on the LayoutParams variable.
int left = 6;
int top = 12;
int right = 6;
int bottom = 6;
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
EditText edXY = new EditText(inventory.this);
edXY.setLayoutParams(params);
If you ultimately wish to distribute all available space evenly among the six EditText views in a table row I would suggest you have a look at the following post: 2-column TableLayout with 50% exactly for each column

Categories