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.
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 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.
I am fairly new to Android development.
I want to create a screen that has a horizontal scroll view of buttons (group names). Every time I click a group name, it will display the members below these buttons (I assume I will have to create a fragment with a listView of members. I haven't gotten to the last part. I was able to create a horizontal scroll view in XML and add 6 button to the XML file. But what I want to do is use Java to find the number of groups every time onCreate is called, so it can create the appropriate number of buttons based on groups. (Group names are currently stored in a temporary ArrayList (for testing purposes) and I use arrayList.size() to get the number of buttons needed to be created. The difficulty I am having is figuring out how to create these buttons inside the LinearLayout inside the HorizontalScrollView inside the RelativeLayout. I messed around with creating one layout adding a button, but I don't know how to create nested layouts, or access the LinearLayout in which I want to create my buttons using Java. Here is my XML file (I created one button for testing. That button location is where I want to create my buttons using Java. I don't want to have anything written there in XML if possible. Everytime a new group is created, a button has to be created):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFBB00">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/buttonList">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me 1"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
How do I add buttons using my Java class in this LinearLayout?
Use this link how to add button dynamically in android? and there is another way doing this by using loop, Read this article for making button using loops
Since you are talking about dynamic content, you might want to try something like a horizontial list view and create an adapter instead of creating a bunch of buttons at runtime. It will be a bit slower and more resource intensive to do it that way.
Here is an example using RecyclerView:
How to build a Horizontal ListView with RecyclerView?
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);