I would like to know how to add text to a scroll view at run time. I am working in Android Studio. I have a file that I am reading from and have put the lines of text into a StringBuilder buffer object.
Now how do I display the text in my ScrollView? Do I need to use .AddView?
Scrollview is a single-element container, meaning you should only have one element in it.
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
So you should add a LinearLayout in your ScrollView and add views to that instead.
Add TexView inside ScrollView:
TextView tv = new TextView(this);
tv.setText("Your string");
scrollView.addView(tv);
Related
How to have multiple layouts (eg. Relative layouts) stacked one above other (each contains text and images regarding news) and move up/down or animate the top most layout by scrolling action in android studio. Please see this screenshot to kmow what i am talking about
https://drive.google.com/file/d/1MrqgKn7CEVuZwkMi8a6ZZB0MXxR3w10R/view?usp=drivesdk
in the new version of Android studio you can resize your preview layout by drag it. try this
There's a few ways to accomplish this.
Option 1: Dynamic content
- Utilize a Recycler view
- In your ViewHolders, use your RelativeLayouts as the layouts to inflate.
Option 2: Static Content
- Utilize a ScrollView root
- Utilize a vertical LinearLayout with height of wrap_content.
- Stack your RelativeLayouts.
Option 3: Another Static Content Option
- Utilize a ScrollView
- Utilize a RelativeLayout child
- Utilize your RelativeLayout childs inside the previous RelativeLayout and use layout_below in each of them.
(Extra info: Consider using ConstraintLayout or something else in place of RelativeLayout to avoid the double measurement issue with RelativeLayout)
i am using Listview inside the Scroll view. and this Listview is the last item of scroll view. Problem is when data gets load the scroller move down to the bottom. How can i prevent it from scrolling on loading data.
You shouldn't use a ListView inside a ScrollView as they are both scrollable.
However to solve your problem I guess you should use the attribute android:descendantFocusability="blocksDescendants" for your main container which resides in the ScrollView.
this is somewhat of of difficult to describe issue but I'll do my very best:
I am developing a an android app that uses a custom camera activity. In this camera activity I use create a surface view programmatically and set it to the framelayout (covers full screen) that was defined in the xml layout file.
My question now is, how can I add other elements to the frame layout? Only programmatically? I am asking because as of now I was only able to add other elements programmatically. Elements that i added in the xml layout didn't appear on screen.
Is it possible that they are just behind the surface view that i add to the frame layout? If so, would it be possible to bring them to the front?
Thank you guys!
Sure, you can add as many buttons and other widgets to the FrameLayout you have. Since the FrameLayout allows stacking of views, the components that you added in the xml file are now behind the View that you added programmatically. Here's how you can create and add widgets dynamically:
// find your framelayout
frameLayout = (FrameLayout) findViewById(....);
// add these after setting up the camera view
// create a new Button
Button button1 = new Button(this);
// set button text
button1.setText("....");
// set gravity for text within button
button1.setGravity(Gravity.....);
// set button background
button1.setBackground(getResources().getDrawable(R.drawable.....));
// set an OnClickListener for the button
button1.setOnClickListener(new OnClickListener() {....})
// declare and initialize LayoutParams for the framelayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// decide upon the positioning of the button //
// you will likely need to use the screen size to position the
// button anywhere other than the four corners
params.setMargins(.., .., .., ..);
// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;
// add the view
fl1.addView(button2, params);
// create and add more widgets
....
....
Edit 1:
There's a trick you can use here:
// Let's say you define an imageview in your layout xml file. Find it in code:
imageView1 = (ImageView) findViewById(....);
// Now you add your camera view.
.........
// Once you add your camera view to the framelayout, the imageview will be
// behind the frame. Do the following:
framelayout.removeView(imageView1);
framelayout.addView(imageView1);
// That's it. imageView1 will be on top of the camera view, positioned the way
// you defined in xml file
This happens because:
Child views are drawn in a stack, with the most recently added child on top (from android resource page on FrameLayout)
http://developer.android.com/reference/android/widget/FrameLayout.html
"FameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute."
I'm trying to create my android app for a lot of different devices so I'm trying to avoid using fixed heights and width and instead using the property WRAP_CONTENT.
Now I need to create a textview on top of a button and align that to the bottom. However the documentation states that you can't use WRAP_CONTENT in combination with ALIGN_PARENT_BOTTOM (which is obvious). Is there another way to achieve this?
The structure is something like this. A RelativeLayout which wraps a button and a textview.
RelativeLayout fl = new RelativeLayout(this);
fl.setLayoutParams(relativeWrapContentParams);
fl.addView(filterBtn);
fl.addView(filterCaption);
The buttons are also created dynamically so theres no xml. Instead the buttons are created in java code.
Also is this a good way of programming for multiple resolutions? Or is it ok to use fixed heights because then the problem is easy to fix and I can just give the relativelayout a fixed height and align its children with ALIGN_PARENT_BOTTOM
See this link this article is the bible for the newbies in android.
Now coming to your question you don't need to use relative layout just for this purpose
you can use linearlayout with vertical orientation place text and then button.
and you need to place this linearlayout inside relative layout with property alignparentbottom=true.
in such way you can have this layout of text and button at the bottom of the screen
In my main layout (mainlayout) I am displaying some text and images which are set dynamically based on the actions of the user. For one particular button click I need to display the contents of another layout (secondlayout). I do this using:
setContentView(R.layout.secondlayout);
On the second layout I have another button that I use to return to the main layout, once again using:
setContentView(R.layout.mainlayout);
The problem is on displaying the mainlayout again all the text and images I was displaying have now disappeared.
How can I return to the mainlayout and still display the contents I was displaying?
don't do it that way. setContentView() is meant to be called once in your onCreate() method. however, couple of reasonable ways to do it,
encapsulate each layout in a fragment, then show / hide each fragment as needed.
bundle both layouts into a single layout, and show / hide each section of the layout by calling setVisibility() on the layout's outermost container.