How to add item blocks to a scrollview? - java

I'm creating a simple app for personal use and I'm stuck because I need to add/remove a text from my scrollview. In the screenshot below the blue square is my scrollview and the mail button (placeholder) needs to open a popup where I can put 2 values in 2 different boxes, a string and a double.
Main_Activity
I know how to do almost everything but I don't know how to add a text every time I click on the mail button and it should look like this when I click on it
this is how it should be when is done
The left button of a car block is the "delete from scrollview" button that removes it from the scrollview and preferences and the right button that does like the 2nd image but where I can edit it.
My questions are:
How can I add a "block" of items in a scroll view? Per block I mean: Edit and delete button and the text like in the example.
Suggestions on what to use for the popup that adds 2 input fields?

In my opinion, I suggest to use recyclerView instead because of performance issues.
link
But, if you need to do the way using ScrollView just follow this.
In your xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:fillViewport="true"
android:padding="16dp">
<LinearLayout
android:background="#android:color/holo_red_light"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
And add item into linear layout when handle click event.
val linear = view.findViewById<LinearLayout>(R.id.linearLayout)
val item =
LayoutInflater.from(requireContext()).inflate(R.layout.item_list_row, linear, false)
linear.addView(item)

Related

How can I add two button (one is back, one is next) under scrollview that filled with textview and imageview in a screen, android?

I want to ask, can I add next and back button under scrollview that filled with textview and imageview in one screen, both with it's xml, java and screenshot result for android?
Here's the picture I intend to ask.
I got from here: https://sensortower.com/android/nl/softartstudio/app/selfishop-camera/com.selfishop.camera/
enter image description here
Where actually, what I really aim is a view like a blog (when you see an image on your above text that can be scrolled, where it's end with Back button that will go to previous page on the bottom left, and Next button that will go to the next page at the reverse side).
Really would like an example in xml, java, and it's screenshot result.
Again, thank you very much
Very appreciate your answer. Thank you
This is what XML is for.
Create your layout in xml and import it via java.
You will want to create your own "bar layout" for the bars in the center, though you could I suppose, fill 3 separate LinearLayouts to achieve the same effect, making a separate layout and putting in it here will be easier for you to read, and follow along with what is happening.
Below is a mock up of the xml layout you would be using to get a view very similar to what you have in your picture. I have not tested this, this was made on the fly so it may not be perfect but it will get you very close to what you are looking for.
<LinearLayout>
android:orientation="vertical"
android:weightSum=10
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout>
android:weight=1
android:layout_height=0dp
android:layout_width="match_parent">
<TextView>
android:width="wrap_content"
android:id="#+id/info_bar"
android:centerHorizontal="true"/>
</RelativeLayout>
<LinearLayout>
android:weight=8
android:width="match_parent"
android:height=0dp>
<TextView>
android:id="#+id/text_view1"
android:width="match_parent"
android:height="wrap_content"/>
<com.yourpackage.yourapp.createabarlayout>
</com.yourpackage.yourapp.createabarlayout>
<com.yourpackage.yourapp.createabarlayout>
</com.yourpackage.yourapp.createabarlayout>
<com.yourpackage.yourapp.createabarlayout>
</com.yourpackage.yourapp.createabarlayout>
</LinearLayout>
<RelativeLayout>
android:weight=1
android:layout_width="match_parent"
android:layout_height=0dp>
<Button>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/back_button"
android:text="#string/back_button_hint"/>
<TextView>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/steps_textview"
android:text="#string/steps_hint"
android:centerHorizontal="true"/>
<Button>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/next_button"
android:text="#string/next_button_hint"/>
</RelativeLayout>
</LinearLayout>

Android RecyclerView scroll and position item at top

I'm not sure if this is hard to do with a RecyclerView or not, I thought there was an easy way to do it but I'm having some difficulties.
I have a RecyclerView on a activity, and when I go to the activity, I want it to scroll to an item. Seems fine enough. However, my issue is that, say only 5 items fit on screen at a time, and we have 6 items, and we scroll to the 5th or 4th item, the recycler view does scroll a tiny bit, but it doesn't look like it scrolls 'to' the element, as it is confined by the RecyclerView bounds.
Is there a way to make it so if I scroll to the 4th or 5th item in the RecyclerView, it will position it at the top of the view, and the area below the last item will just be the background color of the normal view, and I can just scroll back up normally?
I hope I'm explaining myself correctly. I'm not sure if this can be done easily and I have made a mistake in the XML setup or code setup.
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/timelineSwitcherView"
layout="#layout/view_timeline_view_switcher"/>
<ViewAnimator
android:id="#+id/viewAnimator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/timelineSwitcherView">
<RelativeLayout
android:id="#+id/timeLineRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lighterBlue">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/timeLineSwipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/timeLineRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
Appreciate any help and guidance with this.
You should use
mRecyclerview.scrollTo(x,y);
mRecyclerview.scrollToPosition();
Or even better:
mRecyclerview.smoothScrollBy( x , y);
mRecyclerview.smoothScrollToPosition(List item position);
So you need to get the Y position, or the child position in the list.

Putting a TableLayout inside a ScrollView?

So I have a TableLayout that I want to put inside a ScrollView. But I have a problem:
The TableLayout is initially empty and I programmatically add TableRows to it by tapping a button. This works fine if I put the TableLayout into the ScrollView normally.
But I want to put the TableLayout at the BOTTOM of the ScrollView. So every time I add a TableRow, the last cell will always be aligned to the bottom of the parent ScrollView. (kind of like how chat apps work - when you press send, your message is added at the bottom while all the other messages are pushed up).
What happens is that if I use android:layout_gravity="bottom" to try to achieve this, I'm unable to see any of the rows that are pushed upwards out of the view of the screen (I can't scroll upwards). However, I'm able to scroll downwards for some reason into emptiness, which shouldn't be possible since the last TableRow should be at the bottom.
Basically the problem is that I can scroll downwards where there are no TableRows but I can't scroll upwards where there are.
This is the relevant XML code:
<ScrollView
android:id="#+id/tableScrollView"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dip"
android:scrollbars="none"
android:orientation="vertical"
android:background="#drawable/stripes_background" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom">
<TableLayout
android:id="#+id/outputTable"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:divider="#drawable/table_divider"
android:showDividers="middle">
</TableLayout>
</RelativeLayout>
</ScrollView>
It looks like you should be using a ListView or some variant thereof; it provides support for all kinds of optimizations related to scrolling content. Take a look at some of the tutorials and developer docs available for custom ListView implementations.
EDIT Check out the question Listview Scroll to the end of the list after updating the list for details on performing the scroll operation you require using a ListView.

How to display an options menu like this when buttons are pressed?

I'm stuck figuring how to do the following requirement.
I need to make a simple screen with two buttons, like this:
That part of the screen is already done.
The hardest thing for me is to make the behavior of the two buttons
when they are pressed. In that case, they must show an option menu
below like this:
Button 1 Pressed:
Button 2 Pressed:
I didn't start to program the menu yet,but in case it helps, this is
the layout for the screen (without the buttons).
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grayBackgound"
tools:context=".MenuActivity" >
<include android:layout_width="match_parent" layout="#layout/header" />
<include android:layout_width="match_parent" layout="#layout/loggedin_subheader"/>
</LinearLayout>
The layout for the header:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/header"
android:contentDescription="#string/header"
/>
<ImageButton
android:id="#+id/btnClose"
android:src="#drawable/btn_close"
android:contentDescription="#+string/salir"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="260dp"
/>
</FrameLayout>
The layout for the sub-header is almost the same.
I just need help to make the options menu for every button.
Thanks in advance!
The menu that you want to implement is what is commonly called a QuickAction Dialog. There are several tutorials available for that.
To get you started, here is a few links:
http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
https://github.com/ruqqq/WorldHeritageSite (This is not a tutorial per se, but has the complete implementation for the QuickAction Dialog)
https://code.google.com/p/simple-quickactions/ (You will need to make a few modifications in this)
The first link is the most promising in your case. If you scroll down that page to the Gallery3D-like QuickActions section, you will see it kind of resembles what you want the end result in your app to be. Naturally, goes without saying, you will still need to make modifications to the source available on the site.
Hope this helps. ;-)

How can I get a ListView and TextView to scroll as one unit when content is taller than the screen height?

I have a main menu screen with a simple ListView that contains "links" to further screens in my app (Browse, Bookmarks, Settings, About, etc.). Underneath the ListView there is a TextView (more accurately, a TextSwitcher that rotates TextViews) that changes every 10 seconds to display a new "tip".
In portrait mode, this works fine. There are my five list items in the ListView , and my tip label underneath. However, when I switch to landscape mode, the ListView is taller than the screen. The ListView scrolls normally, but I cannot scroll past the end of the ListView to see the TextView underneath.
I have tried every possible combination of Layouts, wrappers, ScrollViews, and layout_height parameters and I simply cannot get it to behave.
Here is the simplest code I can use to get the result pictured above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ListLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/TipLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="#+id/ListLayout">
<TextSwitcher android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/TipSwitcher">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="7pt"
android:id="#+id/Tip1TextView" android:text="Tip: Hello, Android!"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tip: This is the second TextView in the TipSwitcher!"
android:id="#+id/Tip2TextView" android:textSize="7pt"></TextView>
</TextSwitcher>
</LinearLayout>
</RelativeLayout>
Like I've said, I've already tried so many different combinations that I can't list them, and for the most part I was randomly inserting XML in an attempt to get something to work the way I wanted. So I'd greatly appreciate suggestions as to how I would go about doing this the right way.
Thanks.
EDIT: Something I forgot to mention, this may or may not be relevant. My MainMenuActivity is extending ListActivity. According to the docs, "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen." But, "If you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate()." So I don't think the ListActivity is interfering.
Put the TextSwitcher in the ListView itself. You can use addFooterView() for this.

Categories