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.
Related
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)
I've read about using ActionBar Tabs, but I want to structure my layout so that I have the tabs somewhere not at the top (after a View) and the layout form the tab downwards to the end of the screen would be the area that changes fragments.
I've looked at a couple of SO questions that are sort of similar, and most mention ViewPager I think, but I haven't found any concrete answers as how to implement this. So how do I get my layout to have a common View, and then right underneath, the tab part? Can this be done with TabHost?
To better illustrate... this is my current XML for the layout (I deleted some attributes to keep it short):
<!-- top most layout -->
<ScrollView 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"
tools:context="com.xxxx.req.FinancialsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Layout that's always visible -->
<LinearLayout
android:id="#+id/"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<!-- This is where I want to have two tabs next to each other -->
<!-- Right underneath the tabs, I want a FrameLayout so I can
switch between two fragments -->
</LinearLayout>
</ScrollView>
So how do I get my layout to have a common View, and then right underneath, the tab part?
Put "the tab part" right where your code comments say that you want "the tab part". While that cannot be accomplished via action bar tabs, such tabs are deprecated anyway starting with the "L" Developer Preview. A ViewPager and tabbed indicator (e.g., PagerTabStrip) or a FragmentTabHost can go where your comments indicate.
Note that TabHost would not be practical, given that you want the tabs to be fragments -- you are better served using FragmentTabHost.
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.
I have an activity where i have a list view. On top of the list view i include another layout activity which contains two spinners for filtering the list view. But if i include the filters the list view is not working. It is not getting populated. If i remove it the list view is getting populated.
<RelativeLayout 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" >
<include android:id="#+id/filter" layout="#layout/activity_expense_list_filter" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#id/filter"
android:layout_marginTop="23dp"
android:drawSelectorOnTop="false">
</ListView>
</RelativeLayout>
if i remove the filter view the list view is working as expected. Any ideas how i can resolve this
What is the layout for activity_expense_list_filter.xml? Does it also contain an element also called #android:id/list?
EDIT:
I have a feeling that, it being titled "filter", the included layout is completely obscuring your list. Is it's opacity at 100%?
I found the issue was because i have added width and height as match_parent in the other layout. so it is on top of the listview and it is occupying the entire width and height hiding the list view from the view. I set it to wrap_content and got it fixed.
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.