Programmatic table row with text and a button - java

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>

Related

Add Defined TextInputLayout Dynamically and Programatically

I am trying to add a TextInputLayout with an EditText based on a number that the user selects from a Spinner. I already have the TextInputLayout attributes defined in XML and was hoping to simple just add them programmatically based on the number that the user selects from the spinner:
XML:
<FrameLayout
android:background="#drawable/image_border"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".525">
<Button
android:id="#+id/add_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click to Add Image" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".475">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_question_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="#string/create_poll_question" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/how_many_answers_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_many_answers_text"
android:textColor="#color/black"
android:textSize="16sp" />
<Spinner
android:id="#+id/number_of_answers_spinner"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:layout_height="24dp"
android:background="#android:drawable/btn_dropdown" />
</LinearLayout>
<!--Want to Add Programatically -->
<android.support.design.widget.TextInputLayout
android:id="#+id/create_poll_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_answer_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Here is the code I am currently using, but it does not add dynamically based on the view I already created:
public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
for (int i = 0; i < Integer.parseInt(selected); i++) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
EditText editText = new EditText(getActivity());
editText.setHint("Poll Answer");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
}
Easiest way to do such dynamic injections of view is to use ButterKnife library by JakeWharton
http://jakewharton.github.io/butterknife/
You can use it like in your activity's declaration part:
#BindView(R.id.title) TextInputLayout textInputLayout;
Then bind it inside onCreate() like:
ButterKnife.bind(this);
This will roughly be equivalent to inflating and finding the view by id.
Moreover, the library helps to dynamically set drawables,etc. with ease as well
Add
android:id="#+id/create_poll_linearlayout"
to your root LinearLayout.

Adding Views with Java LayoutInflater in a loop either doesn't set the width or only adds the first item in the loop

I have function that loads data form a server, like a search then adds these tot the main menu.
To accomplish this I am using a for loop on the JSON results to add the items.
This loop works fine, it reads the data and loops through fine:
Java Loop:
JSONArray teams = result.getJSONArray("teams");
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) mainMenu.findViewById(R.id.team_list_view);
//Log.d("TEAMS",teams.toString());
for(int x = 0; x < teams.length(); x++) {
JSONObject cTeam = teams.getJSONObject(x);
String name = cTeam.getString("name");
String thumb = cTeam.getString("thumb");
String id = cTeam.getString("id");
View custom = inflater.inflate(R.layout.teams_menu_template, null);
int width = LinearLayout.LayoutParams.FILL_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
ImageButton pp = (ImageButton) custom.findViewById(R.id.tempPPbtn);
Button teamName = (Button) custom.findViewById(R.id.tempPPTxtbtn);
teamName.setText(name);
loadImage loadImage = new loadImage("imagebutton",pp);
loadImage.execute(thumb);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
parent.addView(custom);
}
Now this does work fine it loops through and adds the image and text and appends to the parent layout. But instead of stacking the new layouts it places them side by side like in the image below:
After some googling I tried adding params to set the width to FILL_PARENT but the outcome only adds the first item. However it does add it as I want.
I've stuck on this for quite some time, if anyone can help it would be greatly appriated.
My Template XML file i'm using.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/tempDropCont"
android:background="#drawable/drop_down"
android:weightSum="100"
android:baselineAligned="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_width="70dp"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/tempPPbtn"
android:background="#drawable/profile"
android:layout_marginLeft="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/leader_board"
android:id="#+id/tempPPTxtbtn"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tempDrop"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/view_team"
android:id="#+id/tempTxtBtn1"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/edit_team"
android:id="#+id/tempTxtBtn2"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/team_settings"
android:id="#+id/tempTxtBtn3"
android:background="#android:color/transparent"
android:textColor="#color/white"
android:textSize="20sp"
android:layout_marginTop="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
At first I did think it was the xml but I have tried using include on a different layout and it includes the file fine as its supposed too.
NOTE from the server there is two items returned.
It would have been nice to have the parent layout as well, or at least the way you defined the parent (LinearLayout with the id listview).
However, there are several culprits for the behavior that you describe:
Make sure that the parent layout has the orientation set to vertical. At this point you can copy paste a couple of template items in your layout and see if they look alright when you define them in xml
When you inflate your item, you need to pass the parent as well, so that the child inherits the layout properties:
View custom = inflater.inflate(R.layout.teams_menu_template, parent, false);
This will create the item with the expected properties as defined in the parent container, but not attach it to the parent just yet.
This line is not used:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
You don't set the parameters once you created them. But I think this will be redundant once you do the inflation properly.

progressbar on top of Button in relative layout issue in Android Studio

Ok this is a weird one I hope someone can explain to me.
I have a custom button layout which creates a button with a circular progress bar in the middle of the button. My XML code is below. What I can't work out however is that the ProgressBar seems to be appearing behind the button. If I set the button background to anything other than transparent the progressbar cannot be seen. With the button background as transparent I can then see the ProgressBar but it still appears behind the button text. I was under the understanding that views appeared in the order they are added. I have even tried setting the view to be on top (view.bringToFront();) and I've tried removing the view and recreating it.
Why does the progressbar appear behind the button and what can I do to solve it?
Many thanks
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/holo_blue_bright"
android:padding="2dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:gravity="center"
android:textColor="#android:color/white"
android:singleLine="true"
android:clickable="false">
</Button>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:visibility="visible"
/>
</RelativeLayout>
Code using the above layout
private void setupTableLayout(int NumberOfRows, int NumberOfButtons){
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
tableLayout.removeAllViews();
for (int i = 0; i < NumberOfRows; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
RelativeLayout btnOneLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
RelativeLayout btnTwoLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
ProgressBar btnOneProgressBar = (ProgressBar)btnOneLayout.findViewById(R.id.progressBar);
ProgressBar btnTwoProgressBar = (ProgressBar)btnTwoLayout.findViewById(R.id.progressBar);
btnOneLayout.setLayoutParams(rowParams);
btnTwoLayout.setLayoutParams(rowParams);
Button btnOne = (Button)btnOneLayout.findViewById(R.id.button);
btnOne.setText("Btn 1, Row " + i);
btnOne.setId(1001 + i);
Button btnTwo = (Button)btnTwoLayout.findViewById(R.id.button);
btnTwo.setText("Btn 2, Row " + i);
btnTwo.setId(2001 + i);
setButtonClickListener(btnOneLayout, btnOneProgressBar);
setButtonLongClickListener(btnOneLayout, btnOneProgressBar);
tableRow.addView(btnOneLayout); //Add layout, instead of just Button
View adivider = new View(this);
adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
adivider.setBackgroundColor(Color.TRANSPARENT);
// This bit of code deals with odd/even numbers of buttons.
if (((i + 1) * 2) < NumberOfButtons + 1) {
tableRow.addView(adivider);
tableRow.addView(btnTwoLayout);
} else {
tableRow.addView(adivider);
btnTwoLayout.setBackgroundResource(android.R.color.transparent);
tableRow.addView(btnTwoLayout);
}
tableLayout.addView(tableRow);
}
}
You are propably running this on android >= 5.0. In 5.0 they added elevation field for views. Elevation defines z-order of views in ViewGroup.
In that case button have non-zero elevation value and progress bar have zero value elevation.
Set elevation of progress bar to e.g. 10dp
<ProgressBar
...
android:elevation="10dp"/>
Put your button into another layout (best choice for this case is probably FrameLayout).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
... />
</FrameLayout>
<ProgressBar
android:id="#+id/progressBar"
... />
</RelativeLayout>
I can't tell you why exactly you get that effect, but I suppose that is a bug. Notice that if you replace Button with other view, for example TextView that problem doesn't exits. But when you change RelativeLayout to any other (tested with FrameLayout) this bug still appears. I guess it's going about background property and order of drawing or measurement in any layout.
try using FrameLayout like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/holo_blue_bright"
android:padding="2dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:gravity="center"
android:textColor="#android:color/white"
android:singleLine="true"
android:clickable="false">
</Button>
<ProgressBar
android:layout_gravity="center"
android:id="#+id/progressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:visibility="visible"
/>
</FrameLayout>
See this link
Generally, FrameLayout should be used to hold a single child view,
because it can be difficult to organize child views in a way that's
scalable to different screen sizes without the children overlapping
each other. You can, however, add multiple children to a FrameLayout
and control their position within the FrameLayout by assigning gravity
to each child, using the android:layout_gravity attribute.
Child views are drawn in a stack, with the most recently added child on top.
By adding marginTop you can do that.. otherwise you can change the structure of button and progress bar...
<linearLayout android:orientation="horizontal" ... >
<ImageView
android:id="#+id/thumbnail"
android:layout_weight="0.8"
android:layout_width="0dip"
android:layout_height="fill_parent"
>
</ImageView>
<TextView
android:id="#+id/description"
android:layout_marginTop="-20dip"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="wrap_content"
>
</TextView>
this code is working fine for me :D

How can I implement a ListView within a LinearLayout?

I am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. I would like a list of transactions in between that scroll. I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen.
Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen?
Here is my XML file so far:
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbook);
ListView list = (ListView) findViewById(R.id.transaction_list_view);
// Create a new Adapter
mAdapter = new TransactionAdapter(list.getContext());
// Inflate footerView and headerView
LayoutInflater inflater = getLayoutInflater();
TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);
// Set listener for footerView
footerView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
}
});
list.setAdapter(mAdapter);
}
use the below code. This will satisfy your requirement. I tried this and working for me.
Relative layout with below,above attributes. Relativelayout is better than Linear layout with weight method.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:text="ListView Heading" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="ListView Footer" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_above="#id/textView2"
></ListView>
The UI will like this
Try this way, hope this will help you to solve your problem.
Instead of using header/footer just put as below code in your XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/header_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add_transaction_string">
</TextView>
</LinearLayout>
Yes, you can do it with weightsum and layout_weight in linearlayout and also you can create this type of view using RelativeLayout.
1) In LinearLayout just add weightsum="1" to your linearlayout and add layout_weight="0.2" to each of your header and footer and add layout_weight="0.6" to your listview.
2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="#+id/header" and layout_above="2+id/footer"
I found a possible solution for your problem from a similiar post. Hope this helps you.
For what you are trying to accomplish to freeze the header/footer. It will be easier to use a relative layout to position the header/footer then have your listview in the middle
<RelativeLayout ...>
<TextView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="#string/default_header_string">
</TextView>
<ListView
android:id="#+id/transaction_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_view"
android:layout_above="#+id/footer_view">
</ListView>
<TextView
android:id="#+id/footer_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="#string/add_transaction_string">
</TextView>
</RelativeLayout>
You can use a LinearLayout for this task. But I don't recommend it as it's a bit "hacky".
Get all the elements in a array: Example:- (weatherArray)
Loop through all the elements :-
Example:-
mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
mainLayout.removeAllViews();
for (int i = 0; i < weatherArray.length(); i++) {
View childView = getLayoutInflater().inflate(R.layout.weather_row4_item, mainLayout,false);
TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);
todayTempStatus.setText("");
}
This is an example without using listview, which we will populate lienarlayout using child view.

Insert a TextView under an others in a RelativeLayout in a ScrollView

I would like to know, how can i add a lot of number of TextView to an RelativeLayout in a ScrollView (for textview can scroll up, of course)
In my XML code, it's easy :
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/bde" >
<RelativeLayout
android:id="#+id/layout_"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_below="#id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</RelativeLayout>
</ScrollView>
Except i would like add in Java code, for that each TextView from a DB (no problems with db), add himself in layout and the more important, the one after the other.
Now, i have :
//Layout in ScrollView
RelativeLayout r = (RelativeLayout) findViewById(R.id.layout_);
//for add TextView
for(int i = 0 ; i < nbr_limite ; i++){
TextView v1 = new TextView(r.getContext());
v1.setText("Test n_"+i);
v1.setId(i+1);
r.addView(v1);
}
In fact, i would like a : android:layout_below="#id/textView1" but in Java and replace the : #id/textView1 by a getLastId() roughly said.
And more roughly, it will be something like : r.addLastView(v1)
I waiting for your answer or your comment :)
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(r.getContext());
v1.setText("Test n_"+i);
v1.setId(i+1);
params.addRule(RelativeLayout.BELOW, tv.getId()-1);
r.addView(tv);
also this is an answer for your question .... Programatically add view one below other in relative layout

Categories