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);
Related
How to programmatically align a textview, a seekbar and another textview in the same line in android studio?
Textview seekbar textview
I have written the following code snippet:
LinearLayout.LayoutParams sblayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
sblayoutParams.setMargins(10, 100, 10, 10);
tv1.setText("0");
tv1.setBackgroundResource(R.color.yellow);
tv1.setLayoutParams(sblayoutParams);
sb1.setLayoutParams(sblayoutParams);
tv2.setText("100");
tv2.setBackgroundResource(R.color.green);
tv2.setLayoutParams(sblayoutParams);
LinearLayout sblinearLayout = findViewById(R.id.rootContainer);
// Add SeekBar to LinearLayout
if (sblinearLayout != null) {
sblinearLayout.addView(sb1);
sblinearLayout.addView(tv1);
sblinearLayout.addView(tv2);
}
The following is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootContainer"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
</LinearLayout>
Current scenario:
To adjust the margins of a view programmatically is not an easy job. You need to use the right LayoutParams for that view. To make things worst, there are bunch of them. One way that I know is using instanceof like the following:
if(tv1.getLayoutParams() instanceof LinearLayout.LayoutParams) {
Toast.makeText(this, "YES", Toast.LENGTH_SHORT).show();
}
You have to test with every single LayoutParams, so good luck with that. To make story short, in your context, you need to use LinearLayout.Params:
LinearLayout.LayoutParams tvparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tvparams.setMargins(10, 10, 10, 100); //left, top, right, bottom
LinearLayout.LayoutParams sbparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
sbparams.setMargins(10, 100, 10, 0); //left, top, right, bottom
TextView tv1 = new TextView(this);
tv1.setText("0");
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundColor(Color.YELLOW);
tv1.setLayoutParams(tvparams);
SeekBar sb1 = new SeekBar(this);
sb1.setMax(100);
sb1.setProgress(100);
sb1.setLayoutParams(sbparams);
LinearLayout layout = findViewById(R.id.rootContainer);
layout.addView(tv1);
layout.addView(sb1);
[EDIT]
If you just want to them in same spot, I recommend using xml like following:
seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Text"
android:gravity="left|center"
android:layout_gravity="center"
android:background="#999999"/>
<SeekBar
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="center"/>
</FrameLayout>
And change a bit in your main layout to look like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/rootContainer"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/seekbar" />
</LinearLayout>
Currently the LinearLayout has android:orientation="vertical", so the Views appear one below the other.
If you want them to appear side by side, you should change the orientation to android:orientation="horizontal"
Or you can set the orientation programmatically:
sbLinearLayout.setOrientation(LinearLayout.HORIZONTAL)
Change the width to WRAP_CONTENT and set a weight so all three Views get a third of the available width:
LinearLayout.LayoutParams sblayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
sblayoutParams.setMargins(10, 100, 10, 10);
sbLayoutParams.setWeight(1.0f);
tv1.setText("0");
tv1.setBackgroundResource(R.color.yellow);
tv1.setLayoutParams(sblayoutParams);
sb1.setLayoutParams(sblayoutParams);
tv2.setText("100");
tv2.setBackgroundResource(R.color.green);
tv2.setLayoutParams(sblayoutParams);
LinearLayout sblinearLayout = findViewById(R.id.rootContainer);
// Add SeekBar to LinearLayout
if (sblinearLayout != null) {
sblinearLayout.addView(tv1);
sblinearLayout.addView(sb1);
sblinearLayout.addView(tv2);
}
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 know its easy question but i am newbie .....i want to add button for every row e.g for row1 there should be button1 and for row2 there should be button2 , buttons insertion and text are controlled using for loop
first.xml
<LinearLayout
android:id="#+id/summaryHeaderLinear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/summaryTable_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
</TableLayout>
firstrow.xml
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/summary_tablerow"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/summaryHeader_linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/summaryHeader_firstlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/summaryHeader_secondlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/summaryHeader_thirdlinearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</TableRow>
firstRow.java:
setContentView(R.layout.first);
summaryTableLayout = (TableLayout)findViewById(R.id.summaryTable_layout);
summaryInflator = getLayoutInflater();
for(int i =0;i<2;i++)
{ final TableRow summaryTableRow = (TableRow)summaryInflator.inflate(R.layout.firstrow,null);
LinearLayout summaryHeaderLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_linearlayout);
summaryTableRow.setId(i);
Button summaryBtn = new Button(this);
summaryBtn.setId(i);
summaryBtn.setText("sample button:" +i);
summaryBtn.setBackgroundColor(Color.MAGENTA);
summaryTableRow.addView(summaryBtn);
for(int k=0;k<5;k++)
{
LinearLayout summaryFirstHorizontalLinearLayout = (LinearLayout)summaryTableRow.findViewById(R.id.summaryHeader_firstlinearlayout);
TextView newText = new TextView(this);
newText.setId(k);
newText.setText(topRowArray[k]);
newText.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT,1f));
newText.setBackgroundColor(Color.BLACK);
newText.setTextColor(Color.WHITE);
summaryFirstHorizontalLinearLayout.addView(newText);
}
summaryTableLayout.addView(summaryTableRow);
}
the output of code is:
while i want to create this layout:
Button for row1
name qty cost sell margin
Button for row2
name qty cost sell margin
but its not displaying like this i already have defined separate xml i dnt want to create dynamic Row.I want to use defined firstrow.xml please help me what should i need to do changes in my existing code
use to a dynamically add textview and button on linearlayout
LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
for(int y =0;y<5;y++){
TextView textv= new TextView(this);
final Button myButton = new Button(this);
textv.setText("record");
myButton.setText("button"+y);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(textv,lp);
ll.addView(myButton,lp);
}
i think this are usefull your problem solution
I have an Android app with this portion of main activity:
<LinearLayout
android:id="#+id/vistaAnni"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:text="1998" />
<CheckBox
android:id="#+id/CheckBox02"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1" />
<CheckBox
android:id="#+id/CheckBox07"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1" />
On the main activity class, I need another linear layout with other checks box.
LinearLayout myView = (LinearLayout) findViewById(R.id.vistaAnni);
Log.v("blah", "Crea tabella dinamica");
for(int i = 0; i < anni.size();i++){
Log.v("blah", "Creo colonna: "+anni.get(i));
LinearLayout newLine = new LinearLayout(getApplicationContext());
newLine.setOrientation(1);
newLine.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
TextView testo = new TextView(getApplicationContext());
testo.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
testo.setText(anni.get(i));
newLine.addView(testo);
CheckBox check1c = new CheckBox(getApplicationContext());
check1c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
newLine.addView(check1c);
CheckBox check2c = new CheckBox(getApplicationContext());
check2c.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, .1f));
newLine.addView(check2c);
myView.addView(newLine);
The system creates the box and the text fine, but with different colors and hardly visibility.
But I haven't customized it over activity.xml it or over activity.java. Why this change?
This is a sreen of app:
I have change all:
CheckBox check1c = new CheckBox(getApplicationContext());
with:
CheckBox check1c = new CheckBox(this);
as suggested by Luksprog and works.
i am newbie and facing difficulty to achieve my required output.
XML CODE:
//this code is inside of ScrollView
<LinearLayout
android:id="#+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></TableLayout>
</LinearLayout>
JAVA CODE:
setContentView(R.layout.status_view);
statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
TableRow statusTableRow = new TableRow(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(TableRow.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);
for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
TableRow.LayoutParams rlp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout,rlp);
}
statusTableLayout.addView(statusTableRow);}
please tell me what should i need to do changes in my current code to product required given image
You can use LinearLayout instead of TableRow like this
TableLayout statusTableLayout = (TableLayout)findViewById(R.id.statusDisciplineTable_layout);
for(int i=0;i<2;i++)
{
LinearLayout statusTableRow = new LinearLayout(this);
statusTableRow.setId(i);
statusTableRow.setOrientation(LinearLayout.VERTICAL);
TextView productsTextView = new TextView(this);
productsTextView.setText("product name:"+i);
statusTableRow.addView(productsTextView);
//statusTableRow.setBackgroundColor(Color.BLACK);
for(int j=0;j<2;j++)
{
RelativeLayout statusRelativelayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.setMargins(0, 0, 16, 0);
rl.addRule(RelativeLayout.ALIGN_LEFT);
TextView label = new TextView(this);
label.setId(j);
label.setText("abcd:"+j);
label.setLayoutParams(rl);
statusRelativelayout.addView(label);
statusTableRow.addView(statusRelativelayout);
}
statusTableLayout.addView(statusTableRow);}
also put everything inside Relativelayout and then put inside ScrollView like this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/statusSecond_Layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/statusDisciplineTable_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</TableLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>