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
Related
I have this layout which presents my View correctly, I tried to match it with Java side to have the capability to build rows dynamically, but I kept failing into achieving that,
<TableLayout>
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaws" />
</LinearLayout>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
I do want to make it in code, I have tried many times and this is my best attempt
public static void makeTablePostOrders(Context ct, TableLayout tableLayout_PostOrders,
List<String> postOrders)
{
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(200,
LinearLayout.LayoutParams.MATCH_PARENT,1);
//randoms
for(int i = 0; i < postOrders.size()/2 ; i++)
{
//make tableRow
TableRow tableRow = new TableRow(ct);
// make LinearLayout
LinearLayout layout = new LinearLayout(ct);
layout.setLayoutParams(linearLayoutParams);
layout.setOrientation(LinearLayout.VERTICAL);
//make TextView
TextView tv = new TextView(ct);
tv.setText(postOrders.get(i));
tv.setTextColor(Color.BLACK);
// make button
// the button should send us to next view
Button bt_View = new Button(ct);
bt_View.setText("Select");
//adding views
layout.addView(tv,-2,-2);
tableRow.addView(layout);
tableRow.addView(bt_View,-2,-2);
tableLayout_PostOrders.addView(tableRow,i+1);
}
}
What I want is to make table ROWs with Text and a Button, yet the text should not pass the screen and always go down vertically if needed.
here is a picture, each row will have one of these.
The correct way to implement the design according to yr requirement is listview or recyclerview.
So best to use Listview/RecycleView instead of making list row on fly.
The xml for the list row will look like :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".65"
android:text="You text will come here" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
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);
How i can display two textview side by side in Java ?
I succeeded to do in xml !
My code :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>
Make a LinearLayout in my activity_layout.
LinearLayout lm=(LinearLayout) findViewById(R.id.Linearlayout1);
Make LinearLayout of type horizontal, dynamically.
LinearLayout llh=new LinearLayout(context);
llm.setOrientation(LinearLayout.HORIZONTAL);
Then created two TextView dynamically
TextView tv1=new TextView(context);
TextView tv2=new TextView(context);
finally add these two TextView's to the horizontal LinearLayout (that we created dynamically) and later the same layout to the xml layout.
llh.addView(tv1);
llh.addView(tv2);
lm.addView(llh);
I hope it was helpful.
Try this,
Java Code,
layout = (LinearLayout)findViewById(R.id.mainLay);
TextView tv1 = new TextView(ActivityName.this);
tv1.setText("TextView 1");
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
tv1.setLayoutParams(childParam1);
TextView tv2 = new TextView(ActivityName.this);
tv2.setText("TextView 2");
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
childParam1.weight = 0.5f;
tv2.setLayoutParams(childParam2);
layout.addView(tv1);
layout.addView(tv2);
and xml code,
<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"
tools:context=".MainActivity">
<LinearLayout
android:weightSum="1"
android:id="#+id/mainLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
This should work.
First Solution:
use online converter
[http://www.xmltojava.com/][1]
Second Solution:
inflate your layout
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layoutView = mInflater.inflate(R.rowLayout, null);
TextView tvOne=(TextView) layoutView.findViewById(R.id.tvOne);
this is what I try to do
final LinearLayout ll = (LinearLayout) findViewById(R.id.container_LV);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
//ll.setOrientation(orientation);
// params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
LinearLayout llh=new LinearLayout(getApplicationContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < fields.length(); i++)
{
final TextView txtLabel = new TextView(getApplicationContext());
final TextView txtValue = new TextView(getApplicationContext());
jsonLabel = fields.getJSONObject(i).getString(TAG_LABEL) ; //+ "\n";
jsonValue = fields.getJSONObject(i).getString(TAG_VALUE) ; //+ "\n";
//txtLabel.setBackgroundColor(Color.YELLOW);
//txtValue.setBackgroundColor(Color.YELLOW);
//txtLabel.setWidth(pixels);
txtLabel.setLayoutParams(params);
txtValue.setLayoutParams(params);
txtValue.setGravity(Gravity.RIGHT);
txtLabel.setGravity(Gravity.LEFT);
//params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
txtLabel.setText(jsonLabel);
txtValue.setText(jsonValue);
txtLabel.setTextColor(Color.WHITE);
txtValue.setTextColor(Color.WHITE);
//txtLabel.
llh.addView(txtLabel);
llh.addView(txtValue);
}
ll.addView(llh);
The key is to use android:layout_weight only after setting android:layout_width to "0dp", try this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12dip"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12dip"
android:layout_alignParentRight="true"
android:textColor="#ffffff" />
</RelativeLayout>
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>