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.
Related
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"
>
...
I want to set the buttons one after another vertically. I was trying something like below, but it doesn't work. Need help please.
RelativeLayout body=(RelativeLayout) findViewById(R.id.body);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button btn;
List<Button> allEds = new ArrayList<Button>();
for(int i=0;i<totalSID;i++){
btn = new Button(FaqList.this);
btn.setId(i);
allEds.add(btn);
if(i==0){}
else
buttonParams.addRule(RelativeLayout.BELOW,allEds.get(i-1).getId());
body.addView(btn,buttonParams);
}
Adding the xml file. For reference you can go through it.
<?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 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true"
>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginBottom="20dp"
>
<LinearLayout
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:animateLayoutChanges="true">
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</LinearLayout
I am not sure where is the problem actually
If you want to add buttons vertically then just use LinearLayout instead of RelativeLayout in your body.
int iNumberOfButtons = productTypeList.size();
Button[] dynamicButtons = new Button[iNumberOfButtons];
LinearLayout.LayoutParams paramsButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < iNumberOfButtons; i++) {
ProductType productType = productTypeList.get(i);
dynamicButtons[i] = new Button(getActivity());
dynamicButtons[i].setText(productType.getTitleString());
dynamicButtons[i].setId(i);
dynamicButtons[i].setTextSize(15.0f);
dynamicButtons[i].setOnClickListener(this);
dynamicButtons[i].setLayoutParams(paramsButton);
dynamicButtonsLinearLayout.addView(dynamicButtons[i]); // dynamicButtonsLinearLayout is the container of the buttons
}
Use linear layout instead of Relative layout. If you need to add multiple button dynamically use RecyclerView based on Item position you can do whatever you need
LayoutParams lparams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button bt=new Button(this);
bt.setLayoutParams(lparams);
this.m_vwJokeLayout.addView(bt);
you used relative layout . with that bottons cover eachother . use linearlayout as parent container . but if you want use relative layout you should store previous buttons id and set new buttons below property like this :
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
Here I am using this code to paste my main_activity.xml on top of itself 5 times, making a long list of itself
Here is my code:
for(int i = 0; i < 5; i++){
LayoutInflater loiViewInflater = (LayoutInflater)
getSystemService (Context.LAYOUT_INFLATER_SERVICE);
View mView = loiViewInflater.inflate(R.layout.activity_main,
null);
FrameLayout.LayoutParams params = new
FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0,i * 400,0,0);
addContentView(mView, params);
}
Everything works handy dandy, but how can I make it to where I can scroll down?
The activities go off the screen and I can't think of a way to scroll down. Is it possible?
Thanks, Cooper
You can always use a ScrollView, just set some container, like a linear layout, and copy your layout inside.
The hierarchy should look like:
ScrollView
LinearLayout
YourCopy
YourCopy
YourCopy
For instance, for your container view:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/list_container"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
And on your loop (once you set your layout above with setContentView) something like:
LinearLayout container = (LinearLayout)findById(R.id.list_container)
for(int i = 0; i < 5; i++){
// inflate and set up your layout mView
// (...)
container.addView(mView);
}
Basically, you are injecting your copies inside the LinearLayout (which is inside the ScrollView).
As Logain suggested, you can use a ScrollView and copy your layouts inside.Here is a sample.
<ScrollView
android:id="#+id/start_root"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/fragment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/page_title_text"
android:textSize="32sp"
android:paddingBottom="5dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/fragment_intro"
style="#style/wizard_intro_style"
android:layout_below="#+id/fragment_title" />
</RelativeLayout>
</ScrollView>
I'm currently working on a server-client android application, and in one of my views I have a HorizontalScrollView that contains a vertical LinearLayout. This LinearLayout will contain a bunch of TextViews that are customized with a background. It works fine until the TextViews get a new row because of the character length, it looks like this:
http://puu.sh/9L127/bf29a86639.jpg
The idea is that the text is supposed to fit in the background, is there any way that I can keep the TextView position like the other ones, even though it has multiple rows?
I hope I made myself clear enough, this is how the TextViews are set up in the code:
public void setupCard(GameCard card) {
final int CARD_WIDTH = 200;
final int CARD_HEIGHT = 100;
m_card = card;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(CARD_WIDTH, CARD_HEIGHT);
lp.setMargins(0, 0, 5, 0);
super.setText(m_card.getContent());
super.setBackgroundResource(R.drawable.cardbg);
super.setLayoutParams(lp);
super.setGravity(Gravity.CENTER);
}
And this is how the activity is set up:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/bng"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/cardLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
Thanks in advance!
Thats because the LinearLayout alignes the text baselines of the TextViews.
Try to specify
android:baselineAligned="false"
for the LinearLayout.
I have made following relative layout in an xml file lets say add_relative_layout.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"
android:orientation="vertical"
android:id="#+id/addAccountLinearLayout">
</LinearLayout>
Above is the main layout in which i want to add copies of below Code file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/UIContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white" >
<TextView
android:id="#+id/amountLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:text="Amount"
android:textColor="#android:color/black"
android:textStyle="bold" />
<EditText
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" >
</EditText>
I have another android xml file named as Show_all.xml. It is a linear layout xml
I want to add this relative layout above as many times as i want in this show_all layout
currently i am using this code
private void callOnCreate()
{
linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout.
layout = (RelativeLayout) findViewById(R.layout.ui_relative_layout_style); // name of xml File of above code.
for (int i=0; i < 4; i++)
{
Account account = accountArray.get(i);
linear.addView(layout, i);
}
}
I am getting Null point exception. Please tell me what to do .
Best Regards
Hello Umar I have no idea how you can use xml layout to add Dynamically But you can use Below Code to get Your Required thing add in to LinearLayout
public RelativeLayout createViewTOAdd(){
lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout mRelativeLayout=new RelativeLayout(this);
mRelativeLayout.setBackgroundColor(Color.WHITE);
TextView mTextView=new TextView(this);
RelativeLayout.LayoutParams Textview_lp=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
mTextView.setText("Amout");
Textview_lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Textview_lp.addRule(RelativeLayout.CENTER_VERTICAL);
Textview_lp.leftMargin=10;
mTextView.setTextColor(Color.BLACK);
mTextView.setTextAppearance(this, R.style.TextStyle);
//mTextView.setLayoutParams(Textview_lp);
EditText mEditText=new EditText(this);
RelativeLayout.LayoutParams EditText_param=new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
EditText_param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
EditText_param.addRule(RelativeLayout.CENTER_VERTICAL);
EditText_param.rightMargin=10;
//mEditText.setLayoutParams(EditText_param);
//mRelativeLayout.addView(mTextView, 0);
//mRelativeLayout.addView(mEditText, 1);
//mRelativeLayout.addView(mTextView);
//mRelativeLayout.addView(mEditText);
mRelativeLayout.addView(mTextView, Textview_lp);
mRelativeLayout.addView(mEditText, EditText_param);
return mRelativeLayout;
}
Now How to add View in to LinearLayout is Below
mLinearLayout=(LinearLayout)findViewById(R.id.mainLinearView);
mLinearLayout.removeAllViews();
for(int i=0;i<4;i++){
mLinearLayout.addView(createViewTOAdd(), i);
}
Hey your are using findViewById to get the instance of relativelayout which is not available in your current layout show_all.xml .thats why you are getting null pointer exception.So create separate layout for that specific relativelayout and names that xml layout file as UIContianer then try to use below code
private void callOnCreate()
{
linear = (LinearLayout) findViewById(R.id.addAccountLinearLayout); // the layout in which i want to make dynamic copies of this layout.
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.ui_relative_layout_style, null);
for (int i=0; i < 4; i++)
{
Account account = accountArray.get(i);
linear.addView(vi, i);
}
}