I put a scrollview around a Relative Layout that I am adding to dynamically. If I set the height of the Relative Layout to a fixed amount that goes off of the page the scrollview will work.
Example:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<RelativeLayout
android:id="#+id/llcustomrow"
android:layout_width="match_parent"
android:layout_height="800dp" >
</RelativeLayout>
</ScrollView>
But I need the RelativeLayout to be able to hold however many items I add to it dynamically o I set The relativelayout height to wrap_content. But once I add enough items to the relative layout so that they are going off the screen the Scrollview doesnt register
Below is how I am dynamically adding to the relative layout
LinearLayout mLinearLayout;
RelativeLayout rlcopy;
RelativeLayout[] rArray = new RelativeLayout[20];
int counter = 0;
RelativeLayout llcustomrow;
RelativeLayout.LayoutParams paramsleft;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mLinearLayout = (LinearLayout) inflater.inflate(
R.layout.customworkout, container, false);
paramsleft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
llcustomrow = (RelativeLayout)mLinearLayout.findViewById(R.id.llcustomrow);
for(int i = 0;i<=rArray.length-1;i++){
rArray[i] = (RelativeLayout)View.inflate(getActivity(), R.layout.addworkoutlayout, null);
paramsleft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
paramsleft.setMargins(10, 0, 0, 0);
rArray[i].setLayoutParams(paramsleft);
}
Button bAdd = (Button) mLinearLayout.findViewById(R.id.bAddExcercise);
bAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rArray[counter].setY(rArray[counter-1].getY() + (rArray[counter-1].getHeight() +25));
llcustomrow.addView(rArray[counter]);
counter++;
}
});
return mLinearLayout;
}
Thanks
Why are you using RelativeLayout? I'd advise switching to LinearLayout.
TRY THIS it will work for me
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/outer_ll"
android:layout_width="match_parent"
android:layout_height="900dp"
android:background="#E7D687"
>
</RelativeLayout>
</ScrollView>
change the layout_height of your RelativeLayout to match_parent.
it will solve the problem.
Related
Earlier on I asked a question about how to reach the last item of a scrollview and someone pointed out that I should be using NestedScrollView, at first it worked but now it's not what I want.
I want to fit my list of item in the ScrollView so only that part of the screen can be scrolled and the other parts stay at their place (the 3 TextView)
So basically my xml file is like this :
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cl_framgnent_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context=".ui.home.AperoDetailFragment">
<TextView
android:id="#+id/name_apero"
/>
<TextView
android:id="#+id/date_apero"
/>
<TextView
android:id="#+id/ingredient_title_apero"
/>
<ScrollView
android:id="#+id/rv_apero_ingredient"
android:layout_width="408dp"
android:layout_height="603dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/ingredient_title_apero"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ingredient_title_apero"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:id="#+id/vertical_layout_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
And in my Java code I try to populate my list with the following code :
public class AperoDetailFragment extends Fragment {
private View root;
private Apero detailApero;
public AperoDetailFragment(Apero apero) {
this.detailApero = apero;
}
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.getName());
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.getDate());
LinearLayout ll = (LinearLayout) root.findViewById(R.id.vertical_layout_ingredient);
LinearLayout a = new LinearLayout(root.getContext());
ConstraintLayout.LayoutParams lparams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
a.setLayoutParams(lparams);
a.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 20; i++) {
Button b = new Button(root.getContext());
b.setText("Button " + i);
a.addView(b);
}
ll.addView(a);
return root;
}
}
The problem is that the item are covering the whole screen instead of staying in the parent container (ScrollView) :
How can I fit my list of item to stay in the parent ?
If this is your actual code, then it might help to set constraints to the text views too.
Also, if your ScrollView has a fixed height, then you should remove either the top or bottom constraint. So if you want it to stick to the bottom, remove the top constraint.
I solved this with removing the ScrollView and using a ListView
I faced with a problem that i cannot put my dataGridView into scrollView and in case I have a lot of columns they just become so thin that it's impossible to see something there. That's why I decided to remake it and create LinearLayout with Vertical Layout for each column and each of them will have another LinearLayout with Horizontal Layout just to simulate GridView. (Hope it's a good idea)
But unfortunately I'm facing some problems during its creation. It is not being created and my application turning off. Ask you for your help
Here is my code: grid_container.xml
<?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="match_parent">
<ScrollView
android:id="#+id/GridScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
PageFragment.java (place where LinearLayout should be fill out)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grid_container, container, false);
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_grid_layout);
int colCount = mPage.get(0).split(",").length;
int rowCount = mPage.size();
ArrayList<ArrayList<String>> tempNormList = createNormList(mPage);
for(int currCol=0;currCol<colCount;currCol++){
LinearLayout linearLayout = new LinearLayout(view.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(llParams);
for(int currRow=0; currRow<rowCount;rowCount++){
TextView textView = new TextView(view.getContext());
textView.setText(tempNormList.get(currCol).get(currRow));
if(currRow==0){
textView.setBackgroundResource(R.drawable.header_borders);
}
linearLayout.addView(textView);
}
mainLayout.addView(linearLayout);
}
return view;
}
Thank you in advance for your help
try my linked Answer, it will provide your solution, but you have do some changes like below
Don't Use Custom Adapter, use for loop with getView() method of
CustomAdapter to Set Data.
Cast your Linearlayout which id is main_grid_layout by `findViewById().
add convertView of getView() method to main_grid_layout
Skip Item Android Grid View in condition
hope it will help you
My parent Linearlayout1 contains two child Frame Layouts: FrameLayout1 and FrameLayout2.
The FrameLayout1 is on top of FrameLayout2 but only covers the half of FrameLayout2.
I replaced FrameLayout1 with some fragment1 and also replaced FrameLayout2 with some fragment2.
Now, when I click on FrameLayout2, the FrameLayout1 should get Invisible. But this is not happening.
I tried the following to do so:
userDetails.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.userDetails, container, false);
topLayout = (LinearLayout) getActivity().findViewById(R.id.FrameLayout1);
bottomLayout = (LinearLayout) getActivity().findViewById(R.id.FrameLayout2);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
topLayout.setVisibility(View.GONE);
}
});
}
I also found that onClick listener of view is not getting called on its click.
UPDATE:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/FrameLayout1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/FrameLayout2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</FrameLayout>
You've set OnClickListener to whole View instead of bottomLayout. and I think you can't cast FrameLayout to LinearLayout. The following code works fine here.
final FrameLayout topLayout = (FrameLayout) findViewById(R.id.FrameLayout1);
final FrameLayout bottomLayout = (FrameLayout) findViewById(R.id.FrameLayout2);
bottomLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
topLayout.setVisibility(View.GONE);
}
});
I think that you have to add to your FrameLayout2 clickable. Try adding
android:clickable="true"
Then in your code setOnClickListener in your FrameLayout2. Btw I think you should cast your topLayout and your bottomLayout as FrameLayout instead of LinearLayout.
Note that your framelayouts should be final in order to be accessed by the listener class.
Hope it helps :)
I want to add 3 HorizontalScrollView into one fragments. I have successfully implemented one but when I tried to implement another horizontalscrollview into same fragments, then I got an error that HorizontalScrollViews support only one child. So, I created another horizontalscrollview in XML. I have created a second linearlayout and passed a new imageviewer ID to that second linearlayout, but it won't show on screen.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" >
<HorizontalScrollView
android:orientation="horizontal"
android:layout_width="800px"
android:id="#+id/horizontalScroll"
android:background="#C6D7D2" android:layout_height="600px">
<LinearLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:orientation="horizontal"
android:layout_width="800px"
android:id="#+id/horizontalScroll2"
android:background="#C6D7D2"
android:layout_height="600px">
<LinearLayout
android:id="#+id/container2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and here is java fragment code
public class FevFragment extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.cfragment, container, false);
}
#Override
public void onStart() {
super.onStart();
/** Setting the multiselect choice mode for the listview */
initfrag();
}
private void initfrag() {
LinearLayout linearlayout1 = (LinearLayout)getView().findViewById(R.id.container);
LinearLayout linearlayout2 = (LinearLayout)getView().findViewById(R.id.container2);
for (int i = 0; i < 15; i++) {
ImageView iv = new ImageView(getActivity());
iv.setPadding(5, 55, 5, 5);
iv.setImageResource(R.drawable.test_play_image);
linearlayout1.addView(iv, 260, 260);
ImageView iv2 = new ImageView(getActivity());
iv2.setPadding(5, 255, 5, 5);
iv2.setImageResource(R.drawable.test_play_image);
linearlayout2.addView(iv2, 100, 100);
TextView tv = new TextView(getActivity());
tv.setText("testing");
tv.setPadding(5, 55, 5, 5);
linearlayout1.addView(tv, 100, 100);
}
} }
Read the documentation
A HorizontalScrollView 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 horizontal orientation, presenting a horizontal array of top-level items that the user can scroll through
In your first LinearLayout (you didn't declare id) use this attribute
android:orientation="vertical"
I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?
Thanks
I see the error u r doing here
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);
You r taking the layout as Linearlayout object, you should take the LinearLayout id
Try this
LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);
Button b1 = new Button(this);
b1.setText("Btn");
lnr.addView(b1);
You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.
Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html
Ok, I have got it to work.
The steps are the following:
First inflate the xml layout, ie,
View view = View.inflate(this, R.layout.main, null);
Then instantiate the container object from the xml layout into a ViewGroup class, ie,
ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);
Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,
container.addView(buttonsLayout);
this.setContentView(view);
You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/label"
android:layout_width="fill_parent"/>
<LinearLayout android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
Lets assume that you want to add your additional views to the LinearLayout with id id/container. In your onCreate method you could retrieve that object for later use:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContainer = (ViewGroup)view.findViewById(R.id.container);
}
Now you are all set to add other views to your container ViewGroup:
LinearLayout theButtons = getButtons()
mContainer.addView(theButtons);
In the getButtons method you need to create your LinearLayout containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. See LayoutInflater.inflate.
just try this:
LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.layout.llmain);
now create button dynamically like this
Button btn1 = new Button(this);
btn1.setText=("Button 1");
mainLinearLayout .addView(btn1);
now if you want to add onether linearlayout then add it below button then
LinearLayout llinner = new LinearLayout(this);
Button btn2 = new Button(this);
btn2.setText=("Button 2");
mainLinearLayout .addView(btn2);
llinner.addView(btn2 );
mainLinearLayout .addView(llinner);
Try this :
LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);
This might help you
Here is what I did to display a set of runtime buttons on table layout.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = View.inflate(this, R.layout.activity_main, null);
TableRow myrow = (TableRow) view.findViewById(R.id.myrow);
Button btn1 = new Button(this);
btn1.setText("Button 1");
myrow .addView(btn1);
Button btn2 = new Button(this);
btn2.setText("Button 2");
myrow .addView(btn2);
this.setContentView(view);
}
}
activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="#+id/myview"
android:layout_width="404dp"
android:layout_height="691dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableLayout
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow android:id="#+id/myrow"></TableRow>
</TableLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
AVD Pixel4API30
Output screenshot