Creating more than one TableLayout in the same layout - java

I have a need to create more than one TableLayout with a varied number of rows in one XML layout.
In my main layout I have an empty LinearLayout, as shown below:
<LinearLayout android:id="#+id/resultsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
I declare this in my activity as so:
private LinearLayout linearResultsLayout;
linearResultsLayout = (LinearLayout) findViewById(R.id.resultsLayout);
I have added a table_layout.xml and a table_row.xml to my Layouts folder:
Table_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/resultsTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="99"
android:background="#drawable/curvedbg"
>
</TableLayout>
Table_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="#+id/row_header" android:layout_weight="33" />
<TextView android:id="#+id/downstream" android:layout_weight="33"/>
<TextView android:id="#+id/upstream" android:layout_weight="33"/>
</TableRow>
My aim is to inflate the table and then inflate how ever many rows I require in to the newly inflated table whilst also passing data in the form of TextViews. The code below should give you an idea of what I'm trying to do. It doesn't work, however. NullPointerException.
private void createTable() {
String[] downResults = {"Downstream","2000","98"};
String[] upResults = {"Upstream","1000","78"};
String[] rowHeadings = {"","Bandwidth","QoS"};
TextView heading = (TextView) findViewById(R.id.row_header);
TextView downstream = (TextView) findViewById(R.id.downstream);
TextView upstream = (TextView) findViewById(R.id.upstream);
TableLayout newTable = (TableLayout) findViewById(R.id.resultsTable);
View table = inflater.inflate(R.layout.table_layout, null);
View row = inflater.inflate(R.layout.table_row, null);
linearResultsLayout.addView(table);
for(int i = 0; i < rowHeadings.length; i++){
heading.setText(rowHeadings[i].toString());
downstream.setText(downResults[i].toString());
upstream.setText(upResults[i].toString());
newTable.addView(row);
}
}
I assume it will be more complicated than how I'm trying to accomplish it.

You should probably use a ListView:
But, assuming you can't / won't, the problem you have is that you are referencing the wrong layout when you get your TextView objects. Your code should probably look more like this:
String[] downResults = {"Downstream","2000","98"};
String[] upResults = {"Upstream","1000","78"};
String[] rowHeadings = {"","Bandwidth","QoS"};
View table = inflater.inflate(R.layout.table_layout, linearResultsLayout, false));
View row = inflater.inflate(R.layout.table_row, linearResultsLayout, false));
TextView heading = (TextView) row.findViewById(R.id.row_header);
TextView downstream = (TextView) row.findViewById(R.id.downstream);
TextView upstream = (TextView) row.findViewById(R.id.upstream);
TableLayout newTable = (TableLayout) table.findViewById(R.id.resultsTable);
linearResultsLayout.addView(table);
for(int i = 0; i < rowHeadings.length; i++){
heading.setText(rowHeadings[i].toString());
downstream.setText(downResults[i].toString());
upstream.setText(upResults[i].toString());
table.addView(row);
}
Notice the row.findViewById and also the table.addView
EDIT: note the change in the layout inflater

Related

How to set text in textview from array

I am trying to get the values I have in an array to a text view, I am using a ViewFlipper and I would like to change the textview at the same time the image changes.
for (int i = 0; i < imagens.length && i < categories.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imagens[i]);
simpleViewFlipper.addView(imageView);
info_text3.setText(Arrays.toString(categories));
}
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
simpleViewFlipper.setInAnimation(in);
simpleViewFlipper.setOutAnimation(out);
simpleViewFlipper.setFlipInterval(5000);
simpleViewFlipper.setAutoStart(true);
The image part is working and setting the text to the textview is also working, the problem is that the textview is static and that the text in the text view is the entire array and I just want one value, I was thinking linking the array with the 'i' from the for sentence, how can I do this?
Try below,
info_text3.setText(categories[i]);
You can directly access arrays values at any index
info_text3.setText(categories[i]);
1. Create a layout with an image and a text view as mentioned below:
custom_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/element_spacing"
android:text="Text" />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/yourimage" />
</LinearLayout>
2. Inflate that layout
View customView = activity.getLayoutInflater().inflate(R.layout.custom_view, null);
3. Add custom view to viewflipper.
simpleViewFlipper.addView(customView);
FINAL CODE
for (int i = 0; i < imagens.length && i < categories.length; i++) {
View customView = context.getLayoutInflater().inflate(R.layout.custom_view, null);
TextView info_text3 = customView.findViewById(R.id.text);
ImageView imageView = customView.findViewById(R.id.image);
imageView.setImageResource(imagens[i]);
simpleViewFlipper.addView(customView);
info_text3.setText(categories[i]);
}

Edit view programmatically in linearlayout

This is my layout xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/container1"
android:tag="1"
//... >
<TextView
android:id="#+id/txt1"
android:tag="1"
//... />
</LinearLayout>
<LinearLayout
android:id="#+id/container2"
android:tag="2"
//... >
<TextView
android:id="#+id/txt2"
android:tag="2"
//... />
</LinearLayout>
//...
It has 2 containers and one textview inside each one. Now, when I touch one button I need the container to exchange the textviews. To get this, this is my approach:
person1 = (TextView) findViewById(R.id.txt1);
person2 = (TextView) findViewById(R.id.txt2);
place1 = (LinearLayout) findViewById(R.id.place1);
place2 = (LinearLayout) findViewById(R.id.place2);
-
place1.removeView(person1);
place2.removeView(person2);
place1.addView(person2);
place2.addView(person1);
But this is what I'm getting on the LogCat:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Both the layouts contains a single TextView inside them. So instead of removing and adding views why don't you just switch the text between them? It would save your time and your performance would be better.
LinearLayout ll = (LinearLayout) findViewById(R.id.your_ll_id);
TextView tv = (LinearLayout) findViewById(R.id.your_tv_id);
ViewGroup parent = (ViewGroup) tv.getParent();
parent.removeView(tv);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//...
ll.addView(tv);
You have to add textview at runtime in linearlayout rather than add textview in xml at compile time.
if you add textview at runtime it will not give you error.
linearLayout.removeAllViews();
final TextView person1 = new TextView(this);
linearLayout1.addView(person1);
final TextView person2 = new TextView(this);
linearLayout2.addView(person2);

Button layout_alignParentRight does not work on the first loop?

I add dynamically viewflipper and on each view I add multiple views one of which is a RelativeLayer containg two buttons - one to go on the left on to the right of the layer.
it works for all loops , but not for the first, where both buttons stay one atop another on the left.
Anyone with helpful advice appreciated.
LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup parent = (ViewGroup) findViewById(R.id.slider_holder);
for (int i = 0; i < temp.length; i++) {
ViewGroup np = (ViewGroup) findViewById(R.id.nexpre);
LinearLayout l=(LinearLayout) inflater.inflate(R.layout.flipper_slides, parent, false);
RelativeLayout next_prev =(RelativeLayout) inflater.inflate(R.layout.next_prev, l, false);
for (int k = 0; k< temp1.length; k++) {
ViewGroup nested_parent = (ViewGroup) findViewById(R.id.nested_slider_holder);
LinearLayout nl=(LinearLayout) inflater.inflate(R.layout.nested_flipper_slides, nested_parent, false);
TextView temp_tv1 =(TextView) inflater.inflate(R.layout.nested_flipper_tv, nl, false);
temp_tv1.setText(temp[i]+" "+temp1[k]);
nl.addView(temp_tv1);
ff.addView(nl);
}
next_prev.addView(nested_prev);
next_prev.addView(nested_next);
l.addView(ff);
l.addView(next_prev);
f.addView(l);
}
This is the next_prev.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nexpre"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginTop="-50dip" >
</RelativeLayout>
Here is the nested_prev.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Prev" >
</Button>
And this is the nested_next.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next" >
</Button>
The issue was that on the first loop the button that was supposed to go all the way to the right, did not. For all other loops after the first it works but not for the first.
I added this line to the code:
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
next_prev.getChildAt(1).setLayoutParams(params);
}
for the first loop I do "ALIGN PARENT RIGHT" programmatically with layout params.
Anyway if anyone could explain the reason why the first loop behaves differently by the others I would appreciate.

Aligning A Relative Layout Progmatically in Another Activity

Ok, I am completely stuck on this. I have looked everywhere for an answer but no solutions I found did me any good. What I am trying to do is add a relative layout to a linear layout and I can do that just fine, the problem is that I can only set the height and the width of the relative layout, and can't align it below any views in the linear layout. I'm guessing this is because the relative layout isn't a child of the linear layout? Only reason I am taking the above approach is because I need to add a relative layout for every record I have in a database table. Anyway here is the code below any help would be great :)
individual_past_test.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/past_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E4EFF5" >
<TextView
android:id="#+id/candidate_name"
style="#style/past_test_candidate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp" />
</RelativeLayout>
IndividualPastTest.java
public class IndividualPastTests extends RelativeLayout {
private Context mContext;
public IndividualPastTests(Context context) {
super(context);
mContext = context;
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.individual_past_test, this, true);
}
}
PastTests.java (relevant parts)
past_tests_label = (TextView) findViewById(R.id.past_tests_label);
past_tests_label.setId(1);
addViewForFirstTest();
private void addViewForFirstTest() {
past_test = new IndividualPastTests(this);
RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
past_test.setGravity(Gravity.CENTER_HORIZONTAL);
// Not being called?
past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());
past_test_view_params.height = 100;
past_test_view_params.width = 600;
System.out.println(past_tests_label.getId());
past_test.setLayoutParams(past_test_view_params);
this.addContentView(past_test, past_test_view_params);
}
Ok I ended up fixing it by adding a relative layout to my xml file for past tests(which I didnt add to the question), then changed my PastTests.java as follows -
// Added this line to get the RelativeLayout I created in the xml to hold my view
pasts_tests_holder = (RelativeLayout) findViewById(R.id.past_tests_holder)
past_tests_label = (TextView) findViewById(R.id.past_tests_label);
past_tests_label.setId(1);
addViewForFirstTest();
private void addViewForFirstTest() {
past_test = new IndividualPastTests(this);
RelativeLayout.LayoutParams past_test_view_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
past_test.setGravity(Gravity.CENTER_HORIZONTAL);
past_test_view_params.addRule(RelativeLayout.BELOW, past_tests_label.getId());
past_test_view_params.height = 100;
past_test_view_params.width = 600;
past_test.setLayoutParams(past_test_view_params);
// Changed this line to add the past_test view to my new relative layout
past_tests_holder.addView(past_test);
Here are the relevant parts to my past_tests.xml file for reference -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/prev_test_view"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/past_tests"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/past_tests_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="227dp" >
</RelativeLayout>
</LinearLayout>
Hope this helps anyone else who is having trouble pragmatically setting views in android

Adding custom relative layout to one single liner layout

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);
}
}

Categories