I am trying to implement a screen like this:
The view could be scrolled horizontally and inside each of them they could be scrolled vertically. The number of items in each is arbitrary.
Currently I am thinking of implementing it using a recyclerview within a recyclerview but is there an easier way to do this?
You can use a scrollView and programatically add views in this scrollview. But i recommended recyclerview.
int countOfViews = 20;
LinearLayout root= findViewById(R.id.scrollViewRoot);
for(int i = 0; i < countOfViews; i++) {
View child = getLayoutInflater().inflate(R.layout.item_test, null);
root.addView(child);
}
XML: item_test.xml
<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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/scrollViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</ScrollView>
Related
I am new to android and this is my first time asking question on stackoverflow. I am trying to implement a layout that has
A horizontal ViewPager2
Some horizontal scrolling RecyclerViews
A vertical main RecyclerView
For now I have wrapped all of the above views inside a NestedScrollView like this
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/vpMainCarasouel"
layout="#layout/item_carasouel" />
<include
android:id="#+id/rvTrendingSlide"
layout="#layout/rv_image_card_slide" />
<include
android:id="#+id/rvPopularSlide"
layout="#layout/rv_image_card_slide" />
<include android:id="#+id/rvTopRated"
layout="#layout/rv_image_card_vertical"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
But I have read that using a Vertical Scroll view (The Vertical RecyclerView rvTopRated) inside another Vertical ScrollView (NestedScrollView) is not a good practice.
So how can I add other views (that requires scroll) and then continue on with a main Vertical RecyclerView (rvTopRated) ?
Here is the layout I am trying to implement.
the scrollbar in my RecyclerView doesn't work, I mean I can scroll the RecyclerView with touch, I also can see the scrollbar but it's can not move
Before ask the question I also tried with some answers here for example:
https://www.dev2qa.com/android-recyclerview-horizontal-scroll-example/
Android RecyclerView Scroll not working after Layout edit
But none of them work for me
Below is my code, thanks in advance
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HexViewActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_hex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:scrollbarSize="15dp"
android:scrollbars="vertical" />
</ScrollView>
Code to create the view
RecyclerView recyclerView = findViewById(R.id.rv_hex);
recyclerView.setLayoutManager(new LinearLayoutManager(HexConstant.app_ontext));
m_fileDataAdapter = new FileDataAdapter(HexConstant.app_ontext, files[0]);
recyclerView.setAdapter(m_fileDataAdapter);
recyclerView.setNestedScrollingEnabled(true);
Is there any example for Scrollbar + recyclerView, pls let me know
Change the outer layout with scroll view layout.
<ScrollView
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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HexViewActivity">
<!-- Your stuff here -->
</ScrollView>
Have you tried using RecyclerView's attribute app:fastScrollEnabled="true"?
Check the full answer here. https://stackoverflow.com/a/46026362/6455501
I need to create i number of photo_view and give it a special amount every time (loop). number of photo_view in every time will be different. What exactly should I do in the code ?
my xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/tools"
android:id="#+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.github.chrisbanes.photoview.PhotoView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/exit_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="20dp"
android:clickable="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_back_ltr" />
</android.support.design.widget.CoordinatorLayout>
Dynamically adding Views to a ViewGroup is what you are looking for. which is done by code not by XML.
In general, there are two ways to add a view dynamically,
either by inflating some layout file in a view then add this view to your parent viewgroup (LinearLayout for ex) by calling the add method on this parent view.
Or by defining a new object of that view and add all params needed to it then add it to the parent viewgroup.
See an example about way 1 here
See an example about way 2 here
in your case you could create a new layout.xml file contains only your custom view com.github.chrisbanes.photoview.PhotoView with all the needed params. then add an id for the parent linearlayout
<LinearLayout
android:"#+id/parentViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
And in your code, loop by the count and add the custom inflated view to parent. like this:
View inflatedView = View.inflate(context, yourViewXML, null);
LinearLayout parentViewGroup = findViewById(R.id.parentViewGroup);
for(int i = 0 ; i<count ; i++){
parentViewGroup.add(inflatedView);
}
I am currently developing an Android game. I have a problem with my content inside a Fragment. The content gets cut off and I am not able to find the problem. I only use the resource directory, where I define the different layouts. That is why I know the problem lies in the layouts. First I thought it has something to do with the fact I used android:layout_height="match_parent". I found this solution on StackOverflow and I hoped that would also solve my problem. But this did not help me at all.
I have the activity_game.xml. There I have a placeholder for the current displayed Fragment. The content is the following:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="y.trader.com.rhcloud.blackmonster.games.tradery.activity.GameActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
The Fragment itself has the following layout defined:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="#+id/money"
style="#style/traderY.TextCenter" />
<TextView
android:id="#+id/location"
style="#style/traderY.TextCenter" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/junkie_icon"/>
<ListView
android:id="#+id/drug_list"
style="#style/traderY.List" />
<TableRow
style="#style/traderY.ButtonRow">
<Button
android:id="#+id/sell_drugs"
style="#style/traderY.ButtonCenter"
android:text="#string/sell"/>
<Button
android:id="#+id/nothing"
style="#style/traderY.ButtonCenter"
android:text="#string/nothing"/>
</TableRow>
</LinearLayout>
And here is the captured screen to show the problem.
As you can see, the last two buttons are being cut. I would like to avoid that and rather allow the user to scroll down. I am in a dead situation and I would appreciate any help. And I hope I manage to explain the problem good enough to be understood.
Without using ScrollView, you can set weight for ListView. Try this solution:
<ListView
...
android:layout_weight="1"/>
You should wrap your fragment layout in a Scrollview to enable scrolling:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<!-- your layout here -->
</ScrollView>
You can see an example of it in use here: Android ScrollView Example
you should make your parent container height to match_parent
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="y.trader.com.rhcloud.blackmonster.games.tradery.activity.GameActivity">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
I would avoid wrapping your entire layout in a ScrollView, because it contains a ListView and nested scrolling views should be avoided. Instead, if you'd like the buttons to appear at the bottom of the list, you can make them the last list item in your ListView. You'd have to override getItemViewType(int position) and getViewTypeCount() in your adapter to do this.
Another solution would be to place the buttons on top of the ListView, by putting the ListView and the TableView in a RelativeLayout, then declaring the TableView last.
if your layout already contains a listview then applying parent layout as scrollview isnt a good practice. however if you still want to use it then you can set the height of the listview dynamically in order to make all its Items visible without scrolling. you can achieve this by using this small code
public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
#Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
}
Log.d("totalHeight " + totalHeight, "********");
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (int) ((totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))));
listView.setLayoutParams(params);
listView.requestLayout();
}
});
}
just pass your ListView Id to this method and it will set the listview height dynamically for you. and then you can use scrollview without any problem, and scroll the whole content as per your need.
usage:
ListView yourListView = (ListView) findViewById(R.id.your_list_view);
YourListViewAdapter adapter = new YourListViewAdapter();
yourListView.setAdapter(adapter);
//set height here
setListViewHeightBasedOnChildren(yourListView);
Hope it helps. Happy Coding :)
I'm pretty new to Android and need a bit of help with some code.
Basically I have an array of coins and I want to dynamically display images for the coins in a ScrollView. Each time the user adds a coin the view should update appropriately.
Thank you!
Here is a screenshot of what I want to do - http://i.imgur.com/3l2fxKw.png
Try this sample
MainActivity
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
for(int x=0;x<3;x++) {
ImageView image = new ImageView(MainActivity.this);
image.setBackgroundResource(R.drawable.ic_launcher);
linearLayout1.addView(image);
}
}
activity_main.xml
<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"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Look into using SurfaceView.
You will have a canvas object to draw your images on, and then aligning the coins is just a matter of mapping your pixels appropriately.
This is a good tutorial:
http://www.mindfiresolutions.com/Using-Surface-View-for-Android-1659.php
Use a GridView instead and customize your own adapters. Gridview works like ListView