Whenever I add a TextView on top of RecyclerView, the horizontal scroll offset resets to start.
I have a LinearLayout which contains two items - TextViewContainer and the RecyclerView. After the RecyclerView has initialized, I update the horizontal scroll offset by 100 pixels the following way:
recyclerView.scrollTo( 100, 0 ).
Then whenever I add a TextView to the TextViewContainer, the RecyclerView offset gets reset to the start for whatever reason, even though they're not related in any way.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
I'm adding the TextView the following way:
LinearLayout container = rootView.findViewById(
R.layout.textViewContainer );
TextView textView = new TextView(container.getContext());
textView.setLayoutParams(new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER);
textView.setTextSize( 20.5f );
textView.setText("TEST" );
container.removeAllViews();
container.addView(textView);
I'm expecting for the RecyclerView stay at the 100 pixel horizontal offset and NOT to get reset back to 0 when I add a TextView above in a separate container.
The textViewContainer is unrelated to the RecyclerView, I don't understand why it's resetting to the start as soon as I add the TextView to the Text View Container.
The only reason I could think of why this is happening is because each item in the RecyclerView is wider than the screen width and maybe that's why Android is trying to reset the horizontal scroll offset since there has been changes in the main layout, not sure..
Is there any way I can prevent this from happening? Thanks!
Related
I have XML file where I display information using LinearLayout vertical. All what I need to do is to create horizontal LinearLayout and attach there an image and a TextView. Then I need to display this horizontal LinearLayout on my main vertical LinearLayout. How can I do this? I need to do this programmatically in java
First create a LinearLayout with orientation = vertical then create another LinearLayout inside this with orientation = horizontal with your image and textview. Like this
<LinearLayout
android:orientation="vertical"
>
//Your other views
<LinearLayout
android:orientation="horizontal"
>
// Your image and textview
</LinearLayout>
</LinearLayout>
I currently have a program that uses a linear layout that contains a Textview that is populated with x amount of values (dependent on arrayList size determined in separate activity) but Scrollview is not working as I anticipated.
Since Scrollview can only have one direct child, I have the Linear Layout with the TextView nested within; however when I wrap both of these within Scrollview, the Linear Layout is still not scrollable
//This is what I am using to populate the Linear Layout that I'm trying to make scrollable
TextView DisplayString = (TextView)findViewById(R.id.stringNumsCon1);
LinearLayout LinContainer = (LinearLayout)findViewById(R.id.LinLay);
Intent intent = getIntent();
ArrayList<String> timerVals = (ArrayList<String>)getIntent().getSerializableExtra("timerVals");
DisplayString.setTextSize(15);
DisplayString.setTextColor(Color.BLACK);
for(int i=0; i<timerVals.size(); i++){
DisplayString.append(timerVals.get(i));
DisplayString.append("\n");
}
//This is how I am trying to make it scrollable within the xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/LinLay"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="391dp">
<TextView
android:id="#+id/stringNumsCon1"
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical">
</TextView>
</LinearLayout>
</ScrollView>
First Activity (used to create arraylist that will populate Linear Layout on next activity)
Portion of second activity I wish to make scrollable (Blue lines delineating what part of page I want scrollable, the rest I want to remain static)
I expected this to make the Linear Layout portion scrollable, but I can see data is cut off and inaccessible when actually implementing this code
You won't need the ScrollView, neither the LinearLayout. Remove them from your layout.
All you will need is set in your TextView in xml:
android:scrollbars = "vertical"
so your final xml layout would be:
<TextView
android:id="#+id/stringNumsCon1"
android:layout_width="match_parent"
android:layout_height="391dp"
android:orientation="vertical"
android:scrollbars = "vertical" />
then set the movement method in onCreate of your activity/fragment as shown below:
Java
DisplayString.setMovementMethod(new ScrollingMovementMethod());
Kotlin
DisplayString.movementMethod = ScrollingMovementMethod()
P.S.1: You can find the original answer here
P.S.2: Consider to use camel case naming convention for your variable names. E.g. use displayString instead of DisplayString.
I have two ListView and I want them to share the same layout position so when I click a button one ListView hides.
Maybe this is not possible or there is a better way like fragments?
Use FrameLayout. This layout view overlies two views over each other.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</FrameLayout>
For showing the first page (i.e. the first ListView):
findViewById(R.id.list1).setVisibility(View.VISIBLE);
findViewById(R.id.list2).setVisibility(View.GONE);
And for the second page:
findViewById(R.id.list1).setVisibility(View.GONE);
findViewById(R.id.list2).setVisibility(View.VISIBLE);
fragments are the easy way to do this IF you don't plan on changing the data in your views.
Make a button and
/*create fragment of the opposite view, probably through a boolean field and an if block
then*/
getSupportFragmentManager.beginTransaction().replace(/*your fragments*/).commit().
in your onclicklistener.
how do I get a TextView into the visible area of a screen by scrolling a ScrollView, if the TextView is not a direct child of the ScrollView?
I've got a LinearLayout view that has a TextView at the top, then a ScrollView and below a Button:
<LinearLayout
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/s"
android:layout_height="0dip"
android:layout_weight="1" >
<TableLayout
android:id="#+id/t"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/r" >
<TextView
android:id="#+id/a"/>
<TextView
android:id="#+id/b"/>
<TextView
android:id="#+id/c"/>
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:layout_height="wrap_content"/>
</LinearLayout>
That generated a wholly filled screen with the label at the top border, the button at the bottom border, and a table between it; depending on how many rows this table has, you can scroll it or not.
The contents of the table are generated by Java code, I just added one row as example of what is inserted there.
I now need to make sure a certain row (not necessarily the last one) is visible by scrolling vertically. I can access every element from s to c, but I can't figure out the code to make the ScrollView s scroll at all.
I tried requestChildRectangleOnScreen, scrollBy and, scrollTo, but possibly always with the wrong arguments.
For now I don't care whether TextView a is vertically centered or at the bottom or top border, it would indeed be great if it was visible at all. X-scroll should stay 0, ideally (i. e. leftmost position).
In case this is important: this function is only called when the user enters the screen with a special Intent that's telling the screen to scroll to TableRow x (it's just to show the latest change).
If you need even more information, please ask.
Thank you!
The problem was indeed that I called the function directly from a sub call of onCreate. This meant the layout wasn't done yet, thus all scrolling was done before the elements had a height and so it scrolled by 0 pixels.
The solution is to use View.post(Runnable r):
ScrollView s = (ScrollView) findViewById(R.id.s);
TextView a = (TextView) findViewById(R.id.a);
s.post(new Runnable() {
public void run() {
int[] location = new int[2];
a.getLocationInWindow(location);
int y = location[1];
s.getLocationInWindow(location);
s.scrollTo(0, y-location[1]); // or some other calculation
}
});
I need to align button,and textview in on same line,button aligned right side and textview also aligned right side,
I tried lot of methods but aligned first textview then aligned button,
how to solve this problem please any one help and solve my problem in programatically,
aligned success in layout xml design but I need it programatically.
place both views inside your layout and set orientation to "horizontal".
<LinearLayout>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
linear layout can also be nested into one another!
if you want to implement more lines of views then you could just define a vertical layout (the main.xml will by default define one for you when first created) and inside that vertical linear layout just insert how many horizontal linear layouts (like the one I written above) as you wish.
Something like this should work. You should modify this to fit your needs (i.e. set the proper text size, width, height, etc).
TextView tv = new TextView(this);
tv.setText("Text");
Button bt = new Button(this);
bt.setText("Button");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.addView(tv);
ll.addView(bt);
setContentView(ll);