Adding more rows to TableLayout from partial XML - java

So I am trying to inflate a TableLayout with more rows in my java app.
Here is the code I am currently running:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
//The part that fails
TableLayout table = (TableLayout) findViewById(R.id.toastTable);
View row = getLayoutInflater().inflate(R.layout.toast_table_layout_row, (ViewGroup) findViewById(R.id.toast_row));
table.addView(row);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0 ,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
And here are my xml files:
toast_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#c3484848">
<TableLayout
android:id="#+id/toastTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<TextView
android:tag="toast_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="Kultuur"
android:textAllCaps="true"/>
<TextView
android:tag="toast_params"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="1000 tera mass g"
android:textAllCaps="true"/>
</TableRow>
</TableLayout>
</RelativeLayout>
toast_table_layout:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:tag="toast_row"
android:id="#+id/toast_row"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<TextView
android:tag="toast_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="Kultuur"
android:textAllCaps="true"/>
<TextView
android:tag="toast_params"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:text="1000 tera mass g"
android:textAllCaps="true"/>
</TableRow>
The idea is that I want to add more rows into my existing TableLayout
If I remove the code that is commented with //The part that fails then I get a toast that displays a TableLayout with a little bit of text in it.
But adding more rows fails with with a NullPointException.
I am not sure how it should be done correctly. End result should be that I add these rows in a loop and change their TextField text to something else by getting the TextField by the tag they have.

you have call findViewById() from the inflated view because your toastTable is in the toast_layout_root. just change
TableLayout table = (TableLayout) findViewById(R.id.toastTable);
to
TableLayout table = (TableLayout)layout.findViewById(R.id.toastTable);

Related

Issue with trying to get an EditText, because of a casting issue

I'm working on a gallery type app. The issue that I have at the moment is trying to fetch the typed text within an EditText. The error that I am getting is a casting exception because the code fetches the EditText block from the layout as a TextView block. This only occurs after I swipe, which causes the some of the LinearLayouts imbeded in the second GridLayout to be removed.
The LinearLayouts are put in at runtime into the Gridlayout (with the id product_details), the amount Layouts is calculated according to which picture is pressed on.
This part of the code fetches the current LinearLayouts that are imbeded in the GridLayout:
for (int i = 0; i<codes.size(); i++) {
//Sets the different elements to te correct texts
ViewGroup linearLayout = (ViewGroup) gridLayout.getChildAt(i);
TextView textCode = (TextView) linearLayout.getChildAt(0);
TextView textPrice = (TextView) linearLayout.getChildAt(1);
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
commentsAddList.add(editTextC.getText().toString());
quantityAddList.add(editTextQ.getText().toString());
priceAddList.add(textPrice.getText().toString().substring(1));
codeAddList.add(textCode.getText().toString());
productModelAddList.add(new ProductModel());
productModelAddList.get(i).setCode(codeAddList.get(i));
if(!quantityAddList.get(i).equals("")) {
productModelAddList.get(i).setQuantity(Integer.parseInt(quantityAddList.get(i)));
}else{
productModelAddList.get(i).setQuantity(0);
}
productModelAddList.get(i).setComment(commentsAddList.get(i));
if(!priceAddList.get(i).equals("")) {
productModelAddList.get(i).setPrice(Double.parseDouble(priceAddList.get(i)));
}else{
productModelAddList.get(i).setPrice(0);
}
mRequestPermissions();
adaptToText.addToList(productModelAddList.get(i));
}
The main picture xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageLayout"
android:columnCount="2"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:fitsSystemWindows="true">
<ImageView
android:layout_columnSpan="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"/>
<GridLayout
android:id="#+id/product_details"
android:columnCount="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</GridLayout>
</GridLayout>
The LinearLayout that is inserted into the GridLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_columnSpan="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:id="#+id/orderSideLayout">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productCode"
android:text=""
android:padding="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productPrice"
android:text=""
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editText1"
android:hint="Quantity goes here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/comments"
android:text="Comments: "
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editTextComments"
android:hint="Comments here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
I'm not sure where the casting exception is caused or why exactly it happened. If anyone is able to help, I would appreciate it a lot! Thanks in advance!
Instead of getting the view by position getting it by id would be easier.
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
Try this instead:
EditText editTextQ = (EditText) linearLayout.findViewById(R.id.editText1);
EditText editTextC = (EditText) linearLayout.findViewById(R.id.editTextComments);
This will be better because if change the xml, it could change view positions, and find views by id you wouldn't need to change the code.

TextView is unclickable in application

I am designing a pop up AlertDialog that sets the view of of the alert dialog to the following XML file. For some reason the file will not add margins and I cannot figure out what I am doing wrong. Margins and Padding do not work in the activity_priority_level_values LinearLayout, the LinearLayout that holds all of the TextViews, or any of the TextViews. How can I add margins (or padding) for the dialogs borders and margins between all of the text views?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_priority_level_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/large_space">
<TextView android:id="#+id/level_1_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_header"/>
<TextView android:id="#+id/level_1_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_description"/>
<TextView android:id="#+id/level_2_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_2_header"/>
<TextView android:id="#+id/level_2_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_2_description"/>
<TextView android:id="#+id/level_3_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_3_header"/>
<TextView android:id="#+id/level_3_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_3_description"/>
<TextView android:id="#+id/level_4_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_4_header"/>
<TextView android:id="#+id/level_4_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_4_description"/>
<TextView android:id="#+id/level_5_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_header"/>
<TextView android:id="#+id/level_5_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_description"/>
</LinearLayout>
</LinearLayout>
Activity
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.activity_priority_level_values);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if(hiddenLayout == null){
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
builder.setView(hiddenInfo);
}
builder.create();
builder.show();
The problem is the inflating of your view.
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
You should never inflate with null. Instead change the code to:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, myLayout, false);
If you are inflating with null the layout params will be ignored.

android - how to inflate a map fragment in an already inflated layout?

i am creating a Table Layout in my an activity, adding dynamically table rows in a loop.
What i want to achieve, is to add a Google map fragment in each dynamically created Table row.
This is my Table row Layout:
<?xml version="1.0" encoding="utf-8">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_rowitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp">
<RelativeLayout
android:id="#+id/inflated_row_relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_poi_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:textColor="#ffffff"
android:text="Name"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/img_snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_poi_name"
android:adjustViewBounds="true"
android:src="#drawable/map_snapshot" />
<TextView
android:id="#+id/tv_poi_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#fe8301"
android:layout_below="#+id/img_snapshot"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/distance_layout"
android:layout_below="#+id/tv_poi_address" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/distance_icon" />
<TextView
android:id="#+id/tv_poi_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Distance"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
</TableRow>
And this is the loop that i am creating the Table rows dynamically:
final TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tl_menu_pois);
TableRow inflateRow = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, null);
for (int i=0;i<3;i++){
View inflate = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, >tl_menu_pois);
RelativeLayout inflated_row_relative = (RelativeLayout) >inflate.findViewById(R.id.inflated_row_relative);
inflated_row_relative.setTag(i);
TextView poi_name = (TextView) inflate.findViewById(R.id.tv_poi_name);
TextView poi_address = (TextView) inflate.findViewById(R.id.tv_poi_address);
poi_name.setText("Name");
poi_address.setText("Address");
//set tag for each TableRow
inflate.setTag(i);
//add TableRows to TableLayout
tblAddLayout.addView(inflate);
//set click listener for all TableRows
inflated_row_relative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Object tag = v.getTag();
String string_tag = tag.toString();
}
});
}
I want to display a Google map in each Table row like the image bellow (instead of the sample image, having the map):
http://goo.gl/9erYB7
Is there a way to implement this?
Thank you very much in advance.
I found a solution for adding multiple map fragment on the same View, following this example, in case someone has the same problem:
http://saadroid.blogspot.gr/2013/06/multiple-maps-in-same-view.html

Dynamically adding TextView to ScrollView on Android

I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion

Android how to add a Linearlayout to another LinearLayout from code

I have a LinearLayout in main.xml:
<?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="fill_parent"
android:orientation="vertical"
android:id="#+id/mainLayout" >
</LinearLayout>
I have made another XML file, called item_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/item_bg"
android:gravity="right" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:text="#string/item1"
android:textSize="30dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:gravity="right"
android:text="#string/number1"
android:textColor="#color/number_bg"
android:textSize="30dp" />
</LinearLayout>
Basically, what I want to do from the code (programmatically), is add a couple item_box.xml into the main.xml. How can I accomplish this?
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
try this linearlayout inside another linearlayout add id to linearlayout layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
in oncreate() call
LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
You are getting a Nullpointer because linearLayout1 is not in mainLayout. You need to inflate your view first and then add it. You should read this question I think it will help you.
Use LayoutInflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);

Categories

Resources