I'm trying to programmatically add views to a FlexboxLayout. When the layout has reached 3 wrapped lined, I want to stop adding things to the layout. The maxLine attribute wasn't useful as it starts to add too much on each horizontal line.
<com.google.android.flexbox.FlexboxLayout
android:id="#+id/dynamic_button_layout"
android:layout_width="wrap_content"
android:layout_height="584dp"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:animateLayoutChanges="true"
android:orientation="horizontal"
app:alignContent="center"
app:alignItems="center"
app:flexDirection="row"
app:flexWrap="wrap"
app:justifyContent="center"/>
I initially tried the following in onCreate
int viewNumber = 0;
for (Button button : buttons){
flexboxLayout.addView(button);
if (flexboxLayout.getFlexLines().size() > 2){
flexboxLayout.removeViewAt(viewNumber);
break;
}
viewNumber++;
}
The idea being if I've gone onto the 4th flex line, I just remove the last view and stop adding more views. The issue is that .getFlexLines() is always empty
If I do something like this, however, I the flex lines are returned correctly and I can start removing views. The problem with this is you can very briefly see the 4th line of buttons before they're removed.
getWindow().getDecorView().post(new Runnable() {
#Override
public void run() {
final FlexboxLayout flexboxLayout = findViewById(R.id.dynamic_button_layout);
int lines = flexboxLayout.getFlexLines().size(); // this will be 4
}
});
Is there any way to get the number of flex lines before the UI is actually updated?
Related
In a RecyclerView, I change the presentation for some items as follows:
if (stk.getQty() == 0) { // mise en valeur des stocks à 0 non purgeables
cellStockQty.setTextColor(Color.RED);
GradientDrawable border = new GradientDrawable();
border.setColor(mIV.getResources().getColor(R.color.colorAugment)); // white background
border.setStroke(1, mIV.getResources().getColor(R.color.colorAccent)); // black border with full
mIV.setBackground(border);
}
When I delete an item, I manage to remove it from the display, with notifyItemRemoved(index); but the next item takes the presentation of the item that was deleted.
Of course, if I leave this activity then come back, all is right.
How to make sure to refresh the display, depending on the data?
before deleting Jjjjjj item
after deleting Jjjjjj item
Edit:
Here is my original layout
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:foreground="#drawable/card_foreground"
app:cardBackgroundColor="#color/stockRecyclerviewBackground"
app:cardCornerRadius="6dp"
app:cardElevation="4dp"
app:cardMaxElevation="6dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<include layout="#layout/recystock_cell_include" />
</androidx.cardview.widget.CardView>
Make sure to clear background mIV.setBackground(null);
in else statement.
Your onBindViewHolder is broken. A properly written onBindViewHolder sets EVERY field of the view that could possibly change. And in a proper RecyclerView implementation, non of the click handlers or other actions ever touch the view itself, they all change the model then notify the adapter an iten has changed. Obviously you're not doing that- you're inheriting some of the styling from the old view. Fix your onBindViewHolder to specify all of the view information.
It seems that just doing:
mStockRecyclerView.setAdapter(mAdapter);
is enough.
Sorry for the noise.
Another solution, found with the help of the answers here and there Reset Button to default background is to reset the background, using setTag() and getTag() like:
if ( mIV.getTag() == null ) mIV.setTag(mIV.getBackground());
if (cellStockQty.getTag() == null ) cellStockQty.setTag(cellStockQty.getCurrentTextColor());
if (stk.getQty() == 0) { // mise en valeur des stocks à 0 non purgeables
cellStockQty.setTextColor(Color.RED);
GradientDrawable border = new GradientDrawable();
border.setColor(mIV.getResources().getColor(R.color.colorAugment)); // white background
border.setStroke(1, mIV.getResources().getColor(R.color.colorAccent)); // black border with full
mIV.setBackground(border);
} else {
cellStockQty.setTextColor((int)cellStockQty.getTag());
mIV.setBackground((Drawable) mIV.getTag());
}
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){
}
}
});
I want to get EditText selection start when user click in EditText (touch).
I do with this code :
int startIndex = txtMean.getSelectionStart();
this always return 0;
and EditText xml code:
<EditText
android:id="#+id/txtMean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint=""
android:inputType="textMultiLine"
android:scrollbars="none" />
my code work in android 2.* but don't work in 4.*
txtMean.getSelectionStart();
getSelectionStart doesn't relate to the user's last touch or click per se. It relates to the text selection on the screen. By default, when the user does a long click text-handles will come up, and allow the user to expand a highlighted text selection. It's the highlighted text that refers to the selection, which is not necessarily where the user last touched.
When the user makes a text selection it becomes highlighted because the EditText will apply a SelectionSpan to the character sequence, and getSelectionStart will return the start value of this span.
Update, Solution Help:
#Override
public boolean onTouchEvent(MotionEvent event) {
// this = EditText;
// will give you the position of the nearest chracter.
int offset = this.getOffsetForPosition(event.getX(), event.getY());
I have some code which uses a textview with a linkmovementmethod so that individual words can be clicked.
However, what I really need is a scrollingmovementmethod. When I do that, the individual spanned segments are no longer clickable.
Unfortunately this means it uses a vertical scroll, even though horizontal scroll ( android:scrollHorizontally="true") is specified.
I'm trying to figure out how to retain the ability to catch onClick on the individual span words, and still allow it to be vertically scrollable.
Can someone provide advice on how to get the best of both worlds?
String page = getPage();
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(page, BufferType.SPANNABLE);
Spannable ssPage = (Spannable) textView.getText();
Integer[] indices = getSpaceIndices(textView.getText().toString(), ' ');
int start = 0;
int end = 0;
// to cater last/only word loop will run equal to the length of indices.length
for (int i = 0; i <= indices.length; i++) {
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
TextView tv = (TextView) widget;
String s = tv
.getText()
.subSequence(tv.getSelectionStart(),
tv.getSelectionEnd()).toString();
Log.d("called", s);
Speak (s);
//textView.scrollBy(tv.getWidth(), tv.getHeight());
}
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
// to cater last/only word
end = (i < indices.length ? indices[i] : ssPage.length());
ssPage.setSpan(clickSpan, start, end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = end + 1;
}
Layout:
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:alpha="245"
android:ellipsize="end"
android:gravity="top"
android:paddingBottom="15dip"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:text="#string/hello_world"
android:textColorLink="#android:color/black"
android:textSize="20dip"
tools:context=".ReadActivity" />
LinkMovementMethod already "traverses links in the text buffer and scrolls if necessary." so you can use it if you want both clickable links and scrollable views.
Then you have to adjust the textview's layout like this:
android:maxLines="3"
android:scrollbars="vertical"
By setting maxLines, the excess lines after it, will exceed the height, thus resulting in a scrollable textview.
I have a TextView within a ScrollView, which currently scrolls to the bottom of the TextView.
The TextView is filled dynamically constantly updating (the TextView is essentially acting as an actions console).
However, the problem I am having is that when the dynamic text is added to the ScrollView, the user can scroll past the text into black space, which is increasing everytime more content is added to the TextView.
I have tried various different apporaches however none of these gave the right outcome. I cannot use maxLines or define height of the layouts as I need this to be dynamic for the various screen sizes, which the number of lines visible constantly changing.
I had also orginally done this progromatically, however this was crashing at random time and therefore would like to keep it in my layout (better usabilty), example code below:
final int scrollAmount = update.getLayout().getLineTop(update.getLineCount()) - update.getHeight();
if(scrollAmount > 0)
{
update.scrollTo(0, scrollAmount);
}
The code below is my current layout xml being used to automatically scroll my TextView to the bottom as content is added:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/spacer2"
android:layout_below="#+id/spacer1"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/battle_details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12dp"
android:layout_gravity="bottom" />
</LinearLayout>
</ScrollView>
EDIT - This is the code I am using to add text to my TextView:
private void CreateConsoleString()
{
TextView update = (TextView)findViewById(R.id.battle_details);
String ConsoleString = "";
// BattleConsole is an ArrayList<String>
for(int i = 0; i < BattleConsole.size(); i++)
{
ConsoleString += BattleConsole.get(i) + "\n";
}
update.setText(ConsoleString);
}
EDIT 2 - I add content to the BattleConsole like this:
BattleConsole.add("Some console text was added");
CreateConsoleString();
To sum up my only issue is the ScrollView and/or TextView is adding blank space to the bottom rather than stop the user from scrolling at the last line of text. Any help or guidence as to where I am going wrong would be much appreciated.
It looks like that when you call
BattleConsole.get(i)
it sometimes returns an empty String so you are just basically adding new lines to your TextView.
You can do this for example:
StringBuilder consoleString = new StringBuilder();
// I'm using a StringBuilder here to avoid creating a lot of `String` objects
for(String element : BattleConsole) {
// I'm assuming element is not null
if(!"".equals(element)) {
consoleString.append(element);
consoleString.append(System.getProperty("line.separator")); // I'm using a constant here.
}
}
update.setText(consoleString.toString());
If you could post the code of BattleConsole I could help you more.
As a footnote: it is encouraged to use camelCase in java. Only class names start with capital letters in java according to the convention.