I want to make a scrollable RelativeLayout where I can create Buttons with a x and y positions and custom width and height.
Here is the code i got so far XML:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/buttonDel"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/RL">
</RelativeLayout>
</LinearLayout>
</ScrollView>
Java:
RelativeLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
linearLayout = (RelativeLayout) findViewById(R.id.RL);
addButton();
}
private void addButton() {
for(int i = 0; i < 20; i++)
for(int j = 0; j < 4; j++)
{
Button but = new Button(this);
but.setX(j * 200);
but.setY(i * 200);
but.setText("B" + i);
linearLayout.addView(but, 200, 200);
}
}
It result in something that i want except the scrolling part. I don't know why the ScrollView isn't working.
Screen so far.
After I changed ScrollView layout_height="match_parent" to "warp_content"
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>
</ScrollView>
However the right way to build the requisite structure is to design a gridview or listview .
Use single child for scroll view to behave correctly.
Change the ScrollView height from "match_parent" to "wrap_content" this should fix the problem:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonDel"
android:fillViewport="true"
>
...
Related
I made buttons dynamically, to be aligned vertically after a normal button (made in xml), in a linear layout, which itself is the child of a Scrollview, but the Scrollview won't work, it treats said buttons as if they weren't part of the linear layout
I have tried adding buttons in xml in the linear layout, and those worked just fine. I also tried adding android:fillViewPort and setting it to true inside the ScrollView, and I tried adding an empty View to the bottom of the ScrollView, but it treated the start of the ScrollView as if it was the bottom, as if the buttons dynamically made weren't a part of it.
public class ShopActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
LinearLayout linearLayout = (LinearLayout)
findViewById(R.id.LinearLayoutu);
linearLayout.setGravity(Gravity.TOP);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearParams.gravity = Gravity.CENTER;
Button button = findViewById(R.id.button);
button.setText("huh");
button.setLayoutParams(linearParams);
for (int i = 0; i < 20; i++) {
Button buttons = new Button(this);
buttons.setText("heh" + (i + 1));
buttons.setY(button.getY() + 20 *i );
buttons.setLayoutParams(linearParams);
linearLayout.addView(buttons);
}
And the xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout android:id="#+id/shop_constraint_layout"
android:layout_width="match_parent"h=
android:layout_height="match_parent"
tools:context=".ui.shop.ShopActivity"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.Guideline
android:id="#+id/guideline21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.21" />
<ScrollView
android:id="#+id/scv"
android:layout_width="match_parent"
android:fillViewport="false"
android:layout_height="match_parent"
android:layout_marginTop="140dp"
android:background="#f0ffa0"
android:scrollIndicators="right"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical"
app:layout_constraintTop_toTopOf="#+id/guideline21"
tools:layout_editor_absoluteX="16dp">
<LinearLayout
android:id="#+id/LinearLayoutu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.196"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline21">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
I expect the buttons created to be in the center of the screen, aligned vertically, one above the other, at a distance of 20p, and to be scrollable trough.
for (int i = 0; i < 20; i++) {
Button buttons = new Button(this);
buttons.setText("heh" + (i + 1));
buttons.setY(button.getY() + 20 *i ); // <- Remove this
buttons.setLayoutParams(linearParams);
linearLayout.addView(buttons);
}
LinearlyLayout automatically arranges its children one after the other. There is no need to explictly set the "Y" position on any of the views. If you want spacing between them, you would use padding.
I have a LinearLayout set to a static height of 100dp. I then have a textView inside it on which I set height to WRAP_CONTENT. The problem is the textView is causing the LinearLayout to expand fully to accommodate the textView when the height of the textView is greater than 100dp
I set it up this way because I want to be able to show and hide the full text when the user clicks a button. I thought setting the linearlayout's height to some minimum and then updating it to WRAP_CONTENT when clicked would work, but when I try to limit the height, the LinearLayout ignores it. I tried to set clipChildren to true but that didn't help. Code is below. I am using Android Annotations, but I don't think that's related to the problem.
<ScrollView
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"
android:padding="30dp"
android:fitsSystemWindows="true"
android:background="#color/light_grey">
<LinearLayout
android:orientation="vertical"
android:clipChildren="true"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
And the Java code:
#EActivity(R.layout.activity_test)
public class TestActivity extends AppCompatActivity {
#ViewById(R.id.text_view)
TextView textView;
String input;
public static final String INPUT_STRING_KEY = "inputStringKeyForParser";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.input = this.getIntent().getExtras().getString(INPUT_STRING_KEY);
}
#AfterViews
public void showText() {
textView.setText(input);
}
}
Does anyone know how to get the TextView to stop causing the linearlayout to expand? Thank you!
Try
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="100dp" />
Ok. I think that you can't apply height property this way because your layout is the child of ScrollView. Put your TextView inside another LinearLayout like this:
<ScrollView
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"
android:padding="30dp"
android:fitsSystemWindows="true"
android:background="#color/light_grey">
<LinearLayout
android:orientation="vertical"
android:clipChildren="true"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
I am designing layout as follows.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<ScrollView
<LinearLayout
<LinearLayout
<TextViewなど
.
.
.
</LinearLayout>
<LinearLayout
<packagename.ExpandableHeightGridView
</LinearLayout>
</LinearLayout>
</ScrollView>
I was executing the program on the nexus5.
It is as follows, Gridview is expressed in the top on display.
enter image description here
If I scroll to the top, then LinearLayout is expressed.
enter image description here
I tried to adjust the layout.
But GridView wasn't adjusted.
I want to set LinearLayout to the top at first.
public class Activity_CommunicationSpace_MemberList extends Activity {
private List<Data_User> objects = new ArrayList<Data_User>();
private Data_User item = new Data_User();
private String user_name;
private Bitmap user_icon;
private Bitmap user_profile;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_communicationspace_memberlist);
user_name = "Test_Test";
user_icon = BitmapFactory.decodeResource(getResources(), R.drawable.icon_lamborghini);
for (int i = 0; i < 60; i++) {
item = new Data_User();
item.setNameData(user_name);
item.setIconData(user_icon);
objects.add(item);
}
Adapter_CommunicationSpace_MemberList adapter = new Adapter_CommunicationSpace_MemberList(getApplicationContext(), R.layout.adapter_communicationspace_memberlist, objects);
ExpandableHeightGridView expandableHeightGridView = (ExpandableHeightGridView) findViewById(R.id.gridview_communicationspace_memberlist);
expandableHeightGridView.setExpanded(true);
expandableHeightGridView.setAdapter(adapter);
final ScrollView scrollView = (ScrollView) findViewById(R.id.activity_communicationspace_memberlist_scrollView);
scrollView.post(new Runnable() {
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_UP);
}
});
}
If Listview And Gridview Are in Bottom Then its Scroll itself. they Does'nt need Scrollview or ExpendeableGridview in This Case
U Should Try This Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Country Codes"
android:textSize="20sp" />
<Button
android:id="#+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Selected Items" />
<GridView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"/></LinearLayout>
I have this layout :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_goster" tools:context="com.example.GosterActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_line1"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_line2"
android:layout_centerHorizontal="true"
android:layout_below="#+id/text_line1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rl_root"
android:layout_below="#+id/text_line2">
</LinearLayout>
</LinearLayout>
</ScrollView>
In Java, I set texts of this layout like this :
tvline1.setText("asdf1");
tvline1.setText("asdf2");
And I programmatically generate TextViews in rl_root like this :
LinearLayout relativeLayout2 = (LinearLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++)
{
TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw);
for(int j = 0; j < 10; j++)
{
TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
relativeLayout2.addView(textvw2);
}
}
With this configuration, I can only see the content of generated textviews, not the first two ones that I wrote by hand. Can you tell me how I can show those two TextViews as well? Thanks.
I am able to get the both kind of textviews - generated ones and handwritten. Code seems to be fine. I just removed these lines
tools:showIn="#layout/activity_goster"
tools:context="com.example.GosterActivity"
I want to put three images on a vertical scroll view, I don't know why, but I just see the third one ( tuto3 ).
here is the xml:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:id="#+id/linearLayout42"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
</LinearLayout>
And there the class:
public class about extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutlayout);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout42);
ImageView image = new ImageView(about.this);
image.setBackgroundResource(R.drawable.tuto1);
ll.addView(image);
ImageView image2 = new ImageView(about.this);
image.setBackgroundResource(R.drawable.tuto2);
ll.addView(image2);
ImageView image3 = new ImageView(about.this);
image.setBackgroundResource(R.drawable.tuto3);
ll.addView(image3);
}
}
You keep calling setBackgroundResource on image instead of image, image1, and image2.