output of my json response is displaying last item in the database - android

I am trying to display the content of my database in a tablelayout, I have fetch the data from the database, and set it to be displayed in my android app, but when the data displays, it overwrite the data fetch earlier and only show the last item.
this is an except form my code where I think I got the code wrong
#Override
protected void onPostExecute(String result) {
/* TextView textView= (TextView)findViewById(R.id.textview);
textView.setText(result);*/
json_string = result;
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String id, inv_id, refnumber, description, details, qty, unit_price, amount;
Double subTotal = 00.00;
int sn = 1;
Double dAmount;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
description = JO.getString("description");
qty = JO.getString("qty");
unit_price = JO.getString("unit_price");
amount = JO.getString("amount");
txtDescription.setText(description);
txtQty.setText(qty);
txtUnit_price.setText(unit_price);
txtAmount.setText(amount);
txtsn.setText(String.valueOf(sn));
count++;
sn++;
try {
subTotal = subTotal + Double.valueOf(amount);
// dAmount = Double.valueOf(amount);
} catch (NumberFormatException e) {
subTotal = 00.00;
}
}
txtSubTotal.setText(String.valueOf(subTotal));
double VAT = 0.05*(subTotal);
txtVat.setText(String.valueOf(VAT));
Double Total = VAT + subTotal;
txtTotal.setText(String.valueOf(Total));
} catch (JSONException e) {
e.printStackTrace();
}
}
my XML layout is as follows
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ng.proartisan.invoiceapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="S/N"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:textStyle="bold"
/>
<TextView
android:text="Description"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="QTY"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Unit Price"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Amount"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
</TableRow>
<TableRow>
<TextView
android:id="#+id/sn"
android:text="S/N"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:id="#+id/description"
android:text="Description"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:id="#+id/qty"
android:text="QTY"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/unit_price"
android:text="Unit Price"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/amount"
android:text="Amount"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="SubTotal"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/subtotal"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="VAT"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/vat"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="Total"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/total"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
please help

You are setting the data into same view in loop, other way to solve your problem like create a new layout and add into your layout example: XML file layout_item_description.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="#+id/tv_sn"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="S/N"
android:textStyle="bold"
/>
<TextView
android:id="#+id/tv_description"
android:layout_weight="4"
android:gravity="center"
android:padding="5dp"
android:text="Description"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_qty"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="QTY"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_unit_price"
android:layout_weight="2"
android:gravity="center"
android:padding="5dp"
android:text="Unit Price"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_amount"
android:layout_weight="2"
android:gravity="center"
android:padding="5dp"
android:text="Amount"
android:textStyle="bold" />
</TableRow>
</TableLayout>
add this item view to your layout. Your XML Layout file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ng.proartisan.invoiceapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="S/N"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:textStyle="bold"
/>
<TextView
android:text="Description"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="QTY"
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Unit Price"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
<TextView
android:text="Amount"
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
android:textStyle="bold"
/>
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/ll_data_description"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="SubTotal"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/subtotal"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="VAT"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/vat"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
/>
<TextView
android:text="Total"
android:background="#drawable/cellborder"
android:padding="5dp"
android:layout_weight="4"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="1"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
<TextView
android:id="#+id/total"
android:text=""
android:background="#drawable/cellborder"
android:layout_weight="2"
android:padding="5dp"
android:gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
Add LinearLayout ll_data_description = (LinearLayout)findViewById(R.id.ll_data_description);
#Override
protected void onPostExecute(String result) {
/* TextView textView= (TextView)findViewById(R.id.textview);
textView.setText(result);*/
json_string = result;
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String id, inv_id, refnumber, description, details, qty, unit_price, amount;
Double subTotal = 00.00;
int sn = 1;
Double dAmount;
while (count < jsonArray.length()) {
JSONObject JO = jsonArray.getJSONObject(count);
View layout_item_description = getLayoutInflater().inflate(R.layout.layout_item_description, null);
((TextView) layout_item_description.findViewById(R.id.tv_sn)).setText(sn+"");
description = JO.getString("description");
qty = JO.getString("qty");
unit_price = JO.getString("unit_price");
amount = JO.getString("amount");
((TextView) layout_item_description.findViewById(R.id.tv_description)).setText(sn+"");
((TextView) layout_item_description.findViewById(R.id.tv_qty)).setText(sn+"");
((TextView) layout_item_description.findViewById(R.id.tv_unit_price)).setText(sn+"");
((TextView) layout_item_description.findViewById(R.id.tv_amount)).setText(sn+"");
count++;
sn++;
ll_data_description.addView(layout_item_description);
try {
subTotal = subTotal + Double.valueOf(amount);
// dAmount = Double.valueOf(amount);
} catch (NumberFormatException e) {
subTotal = 00.00;
}
}
txtSubTotal.setText(String.valueOf(subTotal));
double VAT = 0.05*(subTotal);
txtVat.setText(String.valueOf(VAT));
Double Total = VAT + subTotal;
txtTotal.setText(String.valueOf(Total));
} catch (JSONException e) {
e.printStackTrace();
}
}

Related

How to click button inside listview

I know this problem already asked many times but i still can't get effective way for solving this. I try for use ListView with clickable/editable widget and Android : How to set onClick event for Button in List item of ListView as reference but the result is i need click button several time for executing my program. Here my program
LeadActivity.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LeadSalesActivity">
<LinearLayout
android:background="#E9ECEB"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/b_menusamping2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="#drawable/menu_button"
android:backgroundTint="#27D01B"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome,"
android:textSize="20sp"
android:textStyle="italic"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/t_username3"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Rizaldi"
android:textColor="#android:color/black"
android:textSize="25sp"
android:textStyle="bold"
android:fontFamily="#font/action_man_bold"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear_refresh2"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white">
<ImageView
android:backgroundTint="#27D01B"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="10dp"
android:background="#drawable/icon_update"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="("
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:textStyle="bold"
android:id="#+id/count_lead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=")"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<Button
android:id="#+id/btn_add"
android:layout_marginRight="20dp"
android:backgroundTint="#android:color/holo_blue_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tambah Lead"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tanggal"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<TextView
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lead"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telp"
android:textColor="#android:color/black"
android:textSize="20sp"/>
</LinearLayout>
<ListView
android:id="#+id/listviewlead"
tools:listitem="#layout/c_lead"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_weight="1" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/back6"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
c_lead.xml
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_tgl"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Tgl"
android:textSize="15sp"
android:textStyle="italic"
android:textColor="#000000" />
<TextView
android:id="#+id/t_lead4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Lead"
android:textSize="15sp"
android:textColor="#000000" />
<TextView
android:id="#+id/t_numberphone"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="right"
android:text="No.Telp"
android:textSize="15sp"
android:textColor="#android:color/black"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/pencil_icon"/>
</LinearLayout>
<LinearLayout
android:id="#+id/btn_delete"
android:layout_marginLeft="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/kotak_abu"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp">
<ImageButton
android:layout_margin="10dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/delete_icon"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/t_alamat"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Alamat"
android:textColor="#android:color/black"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
MyAdapter
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.c_lead, null);
}
final HashMap<String, Object> hashMap = _data.get(_position);
final TextView t_tanggal = (TextView) _view.findViewById(R.id.t_tgl);
final TextView t_lead = (TextView) _view.findViewById(R.id.t_lead4);
final TextView t_alamat = (TextView) _view.findViewById(R.id.t_alamat);
final TextView t_numberphone = (TextView) _view.findViewById(R.id.t_numberphone);
final LinearLayout btn_edit = (LinearLayout) _view.findViewById(R.id.btn_edit);
final LinearLayout btn_delete = (LinearLayout) _view.findViewById(R.id.btn_delete);
String dateString = _data.get(_position).get("create_date").toString().replace("-","");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmm");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date = format1.parse(dateString);
String dateFinal = format2.format(date);
t_tanggal.setText(String.valueOf((long)(_position + 1)).concat(".").concat(dateFinal));
} catch (ParseException e) {
e.printStackTrace();
}
t_lead.setText(_data.get((int)_position).get("type").toString());
t_alamat.setText(_data.get((int)_position).get("street").toString());
if (_data.get((int)_position).get("mobile").toString().equals(true)){
t_numberphone.setText("Tanpa Nomor");
} else {
t_numberphone.setText(_data.get((int)_position).get("phone").toString());
}
btn_edit.setFocusable(false);
btn_edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i.setClass(getApplicationContext(), AddLeadSalesActivity.class);
startActivity(i);
}
});
btn_delete.setFocusable(false);
btn_delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Data Tidak Bisa Dihapus", Toast.LENGTH_SHORT).show();
}
});
return _view;
}
Is there any mistake inside my program? How to solve this?

List view rows not getting clicked?

I am creating a ListView with list of items and trying to print some toast on clicking each row of the ListView but the toast is getting printedwhen I am clicking on the header part of the ListView but not coming when clicking on the rows of the ListView. What can be the issue. I am not able to get as to why it is not recognising the click on the rows of the ListView.
Please help me. Thanx in advance!!!
Here is my code
Main Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ankit.surya1.MainActivity">
<HorizontalScrollView
android:layout_marginTop="5dp"
android:layout_width="1500dp"
android:layout_height="match_parent">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp" />
</HorizontalScrollView>
</RelativeLayout>
Main Activity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
ViewGroup headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.header, listView, false);
listView.addHeaderView(headerView);
String[] items = getResources().getStringArray(R.array.list_items);
LstViewAdapter adapter = new LstViewAdapter(MainActivity.this, R.layout.rowlayout, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
/*
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose");
builder.setIcon(R.drawable.checked);
final CharSequence[] items = {"Call", "Send SMS"};
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
// t12.setText(items[item]);
if (items[item] == "call") {
// write the calll code here......................................
}
if (items[item] == "Send SMS") {
//write the sms code here..........................................
}
}
});
AlertDialog alert = builder.create();
alert.show();*/
switch(position)
{
case 0: {
Toast.makeText(getApplicationContext(), "hi ki", Toast.LENGTH_SHORT).show();
}
break;
case 1: {
Toast.makeText(getApplicationContext(), "hi ki", Toast.LENGTH_SHORT).show();
}
break;
case 2: {
Toast.makeText(getApplicationContext(), "hi ki", Toast.LENGTH_SHORT).show();
}
break;
}
}
});
}
}
header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="1200dp"
android:layout_height="wrap_content"
android:background="#9E9E9E"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_column="0"
android:background="#drawable/table1"
android:gravity="left"
android:padding="10sp"
android:text="Type"
android:textColor="#color/white"
android:textStyle="bold"></TextView>
<TextView
android:layout_width="100dp"
android:layout_column="1"
android:layout_height="wrap_content"
android:padding="10sp"
android:gravity="left"
android:text="Loan #"
android:textColor="#color/white"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_width="100dp"
android:layout_column="2"
android:padding="10sp"
android:gravity="left"
android:textColor="#color/white"
android:text="Name"
android:textStyle="bold"
android:background="#drawable/table1"
android:layout_height="wrap_content"
>
</TextView>
<TextView
android:layout_width="100dp"
android:layout_column="2"
android:background="#drawable/table1"
android:gravity="left"
android:padding="10sp"
android:text="Ph No"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_height="wrap_content"
></TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="4"
android:padding="10sp"
android:gravity="left"
android:text="Address"
android:textColor="#color/white"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="5"
android:padding="10sp"
android:gravity="left"
android:textColor="#color/white"
android:text="Area"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="6"
android:padding="10sp"
android:gravity="left"
android:textColor="#color/white"
android:text="Amount"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="7"
android:padding="10sp"
android:gravity="left"
android:textColor="#color/white"
android:text="ROI"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="8"
android:padding="10sp"
android:textColor="#color/white"
android:gravity="left"
android:text="Interest"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="9"
android:padding="10sp"
android:gravity="left"
android:textColor="#color/white"
android:text="Total"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="10"
android:padding="10sp"
android:gravity="left"
android:text="DaysLeft"
android:textColor="#color/white"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="100dp"
android:layout_column="11"
android:padding="10sp"
android:gravity="left"
android:text="SMS Count"
android:textColor="#color/white"
android:textStyle="bold"
android:background="#drawable/table1"
>
</TextView>
</LinearLayout>
</LinearLayout>
rowlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="1200dp"
android:layout_height="wrap_content"
android:background="#9E9E9E"
>
<TextView
android:id="#+id/t1"
android:layout_width="100dp"
android:layout_column="0"
android:layout_height="match_parent"
android:gravity="left"
android:padding="10sp"
android:textColor="#000000"
android:inputType="text"
>
</TextView>
<TextView
android:id="#+id/t2"
android:layout_width="100dp"
android:layout_column="1"
android:layout_height="match_parent"
android:padding="10sp"
android:gravity="left"
android:inputType="number"
android:textColor="#000000"
>
</TextView>
<TextView
android:id="#+id/t3"
android:layout_height="match_parent"
android:layout_column="2"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="textPersonName"
android:layout_width="100dp"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t4"
android:layout_width="100dp"
android:layout_column="3"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="phone"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t5"
android:layout_width="100dp"
android:layout_column="4"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="text"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t6"
android:layout_width="100dp"
android:layout_column="5"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="text"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t7"
android:layout_width="100dp"
android:layout_column="6"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="number"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t8"
android:layout_width="100dp"
android:layout_column="7"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="number"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t9"
android:layout_width="100dp"
android:layout_column="8"
android:padding="10sp"
android:textColor="#000000"
android:gravity="left"
android:inputType="number"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t10"
android:layout_width="100dp"
android:layout_column="9"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="number"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t11"
android:layout_width="100dp"
android:layout_column="10"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="number"
>
</TextView>
<TextView
android:layout_height="match_parent"
android:id="#+id/t12"
android:layout_width="100dp"
android:layout_column="11"
android:padding="10sp"
android:gravity="left"
android:textColor="#000000"
android:inputType="number"
>
</TextView>
</LinearLayout>
</LinearLayout>
strings.xml
<string-array name="list_items">
<item>Gold__4__Jahn Ibrahim__9556551456__LB Nagar__Hyd__95220__6__2920__22920__-26__0__</item>
<item>Gold__3__Amit Kumar__9556325566__KpHB __Hyd__20000__3__2920__22580__-4__0__</item>
<item>Gold__7__Mohan__9556458745__Hitecy__Hyd__95226__3__2920__23570__-5__0__</item>
<item>Gold__8__Ravi__9556551456__LB Nagar__Hyd__95220__6__2920__278750__-26__0__</item>
<item>Gold__3__Sunil__9556325566__KpHB __Hyd__20000__3__2920__27540__-4__0__</item>
<item>Gold__9__Amit__9556458745__Hitecy__Hyd__95226__3__2920__27780__-5__0__</item>
<item>Gold__5__Mohan__9556551456__LB Nagar__Hyd__95220__6__2920__278740__-26__0__</item>
<item>Gold__3__Amit__9556325566__KpHB __Hyd__20000__3__2920__22920__-4__0__</item>
<item>Gold__7__Jain__9556458745__Hitecy__Hyd__95226__3__2920__22920__-5__0__</item>
</string-array>

uncaught handler when inflating a PopupWindow

I'm trying to have a popup window show details of an invoice. I look at examples on the internet and they all look like this so I don't understand why it get an error when I try to inflate the view. the only thing I noticed is that it have the word (port) after the invoice_look in the projects layout folder.
I get this error on the line:
View popupView = layoutInflater.inflate(R.layout.invoice_look,null);
this = {com.android.internal.os.RuntimeInit$UncaughtHandler#21167}
shadow$_klass_ = {java.lang.Class#20712} "class com.android.internal.os.RuntimeInit$UncaughtHandler"
accessFlags = 524288
classLoader = null
classSize = 416
clinitThreadId = 14084
componentType = null
dexCache = {java.lang.DexCache#21169}
dexCacheStrings = {java.lang.String[105519]#21170}
dexClassDefIndex = 3975
dexTypeIndex = 4549
directMethods = {java.lang.reflect.ArtMethod[2]#21171}
iFields = null
ifTable = {java.lang.Object[2]#21172}
name = {java.lang.String#21173} "com.android.internal.os.RuntimeInit$UncaughtHandler"
numReferenceInstanceFields = 0
numReferenceStaticFields = 0
objectSize = 8
primitiveType = 0
referenceInstanceOffsets = -2147483648
referenceStaticOffsets = 0
sFields = null
status = 10
superClass = {java.lang.Class#20126} "class java.lang.Object"
verifyErrorClass = null
virtualMethods = {java.lang.reflect.ArtMethod[1]#21174}
vtable = null
shadow$_klass_ = {java.lang.Class#18444} "class java.lang.Class"
shadow$_monitor_ = -1493239949
shadow$_monitor_ = -1272289465
Here is the the code in my activity.
lookBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) Pickup.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.invoice_look,null);
final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Button dismissBtn = (Button) popupView.findViewById(R.id.lookDismiss);
dismissBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
}
});
Here is the XML for the invoice_look window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/invLookLayout">
<ListView
android:id="#+id/listView"
android:layout_width="500dp"
android:layout_height="520dp" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
style="#style/BlackSText"
android:id="#+id/textView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="#string/discount" />
<TextView
style="#style/BlackSText"
android:id="#+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
style="#style/BlackSText"
android:id="#+id/lookDiscount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end" />
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
style="#style/BlackSText"
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="#string/env" />
<TextView
style="#style/BlackSText"
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
style="#style/BlackSText"
android:id="#+id/lookEnviro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end" />
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
style="#style/BlackSText"
android:id="#+id/textView31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="#string/salestax" />
<TextView
style="#style/BlackSText"
android:id="#+id/textView32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start" />
<TextView
style="#style/BlackSText"
android:id="#+id/lookSalesTax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end" />
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
style="#style/BlackText"
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="#string/total" />
<TextView
style="#style/BlackText"
android:id="#+id/lookPcs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:text="#string/pcs" />
<TextView
style="#style/BlackText"
android:id="#+id/lookTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end" />
</TableRow>
</TableLayout>
<TextView
style="#style/BlackText"
android:id="#+id/textView24"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20sp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:columnOrderPreserved="true"
android:rowCount="1"
android:rowOrderPreserved="false"
android:useDefaultMargins="false"
android:orientation="horizontal"
android:id="#+id/gridLayout"
android:focusableInTouchMode="false" >
<Button
style="#style/BlackSText"
android:id="#+id/lookDismiss"
android:layout_width="96dp"
android:layout_height="70dp"
android:layout_column="0"
android:layout_row="0"
android:background="#drawable/lightrebbtn"
android:text="#string/cancel"
android:layout_gravity="fill_horizontal" />
<Button
style="#style/BlackSText"
android:id="#+id/button58"
android:layout_width="96dp"
android:layout_height="70dp"
android:layout_column="1"
android:layout_row="0"
android:background="#drawable/yellowbtn"
android:text="#string/del"
android:layout_gravity="fill_horizontal" />
<Button
style="#style/BlackSText"
android:id="#+id/button61"
android:layout_width="96dp"
android:layout_height="70dp"
android:layout_column="2"
android:layout_row="0"
android:background="#drawable/yellowbtn"
android:text="#string/price"
android:layout_gravity="fill_horizontal" />
<Button
style="#style/BlackSText"
android:id="#+id/button62"
android:layout_width="96dp"
android:layout_height="70dp"
android:layout_column="3"
android:layout_row="0"
android:background="#drawable/yellowbtn"
android:text="#string/myo"
android:layout_gravity="fill_horizontal" />
<Button
style="#style/BlackSText"
android:id="#+id/button63"
android:layout_width="96dp"
android:layout_height="70dp"
android:layout_column="4"
android:layout_row="0"
android:background="#drawable/yellowbtn"
android:text="#string/coupon"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</LinearLayout>

how to set button below while creating textview programatically?

I have one button to create textview and edittext programmatically,everything works fine but the issue is when textview and edittext generated button is appear above the textview and edittext,i want to set button below of them,following is my code can any one tell what is the issue?
addnewdata.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
totalFields++;
lnr = (LinearLayout) findViewById(R.id.addnewlinear);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(25, 0, 0, 0);
valueTV = new TextView(MainActivity.this);
// valueTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
valueTV.setText(userInput.getText());
valueTV.setLayoutParams(lp);
valueTV.setTextSize(18);
valueTV.setTag("tv_" + totalFields);
valueTV.setId(totalFields);
valueTV.setTextColor(Color.parseColor("#2d6cae"));
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp1.setMargins(25, 0, 25, 0);
lp1.height = 50;
EditText edtvalues = new EditText(MainActivity.this);
edtvalues.setBackgroundResource(R.drawable.rect_edt);
edtvalues.setLayoutParams(lp1);
edtvalues.setTag("ed_" + totalFields);
lnr.addView(valueTV);
lnr.addView(edtvalues);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
>
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#000000"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="IT ADD$"
android:id="#+id/itaddestxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="UP"
android:textColor="#ffffff"
android:layout_toRightOf="#+id/itaddestxt"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:id="#+id/addnewlinear"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD NEW EVENT"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:id="#+id/txtaddnewevent"
android:textColor="#73b5fa"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtaddnewevent"
android:layout_marginTop="10dp"
android:id="#+id/bluelines"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of Event:*"
android:layout_below="#+id/bluelines"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtnameofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtnameofevent"
android:layout_below="#+id/txtnameofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Event:*"
android:layout_below="#+id/edtnameofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdateofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdateofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledtdate"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edtdateofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edtdateofevent"
android:layout_marginLeft="10dp"
android:id="#+id/calndrdat"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time of Event:*"
android:layout_below="#+id/reledtdate"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txttimeofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txttimeofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledttime"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edttimeofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edttimeofevent"
android:layout_marginLeft="10dp"
android:id="#+id/timepickrs"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duration of Event:*"
android:layout_below="#+id/edttimeofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdurationofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtdurationofevent"
android:layout_below="#+id/txtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/addnewdata"
android:text="Add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="#android:color/white"
android:layout_gravity="center"
android:id="#+id/btnsubmit"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Add a new layout (linear or relative) above the button.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
>
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#000000"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="IT ADD$"
android:id="#+id/itaddestxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="UP"
android:textColor="#ffffff"
android:layout_toRightOf="#+id/itaddestxt"
android:textSize="20sp" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD NEW EVENT"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:id="#+id/txtaddnewevent"
android:textColor="#73b5fa"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtaddnewevent"
android:layout_marginTop="10dp"
android:id="#+id/bluelines"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of Event:*"
android:layout_below="#+id/bluelines"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtnameofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtnameofevent"
android:layout_below="#+id/txtnameofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Event:*"
android:layout_below="#+id/edtnameofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdateofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdateofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledtdate"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edtdateofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edtdateofevent"
android:layout_marginLeft="10dp"
android:id="#+id/calndrdat"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time of Event:*"
android:layout_below="#+id/reledtdate"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txttimeofevent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txttimeofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:id="#+id/reledttime"
>
<EditText
android:layout_width="250dp"
android:layout_height="30dp"
android:id="#+id/edttimeofevent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toRightOf="#+id/edttimeofevent"
android:layout_marginLeft="10dp"
android:id="#+id/timepickrs"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Duration of Event:*"
android:layout_below="#+id/edttimeofevent"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:textSize="18dp"
android:textColor="#2d6cae"
android:id="#+id/txtdurationofevent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/edtdurationofevent"
android:layout_below="#+id/txtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:layout_marginRight="15dp"
/>
<!--Empty layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="#+id/addnewlinear"
android:orientation="vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edtdurationofevent"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:id="#+id/addnewdata"
android:text="Add"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="#android:color/white"
android:layout_gravity="center"
android:id="#+id/btnsubmit"
/>
</LinearLayout>
</ScrollView>

Cannot add childs to linearLayout

In my app what i want is to dynamically add buttons to a linear layout based on the length of the array.For example if the length of array is 4 then ,4 buttons should be added to the linear layout.I tried doing that but always my app crashes
The linearlayout which is at the bottom of the xml is where i wanna add the buttons
XMl
<?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"
android:background="#drawable/bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/hsjobslogo"
android:layout_gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mrd_home"
android:layout_marginRight="10dp"
android:src="#drawable/home168"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#drawable/header"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="0dp"
android:layout_height="33dp"
android:layout_weight="2"
android:layout_marginTop="04dp"
android:text="Resource"
android:gravity="center|start"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="18sp" />
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:padding="10dp"
android:visibility="gone"
android:layout_marginTop="-5dp"
android:src="#drawable/edit" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:visibility="visible"
android:id="#+id/ll_main_pi"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_pi_name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_gender" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=","
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_age" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profession"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_profession" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" [ "
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yrs"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_yrs" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
style="#style/Job_on_Call"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Months"
style="#style/Job_on_Call"
android:textColor="#000"
android:id="#+id/tv_mrd_exp_months" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" ]"
style="#style/Job_on_Call"
android:singleLine="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_area" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_city" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="false"
android:layout_marginBottom="2dp"
android:id="#+id/tv_mrd_state" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
style="#style/Job_on_Call"
android:singleLine="true"
android:id="#+id/tv_mrd_country" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_gravity="bottom"
android:background="#drawable/divider_light"
android:layout_weight="0.2" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1.6"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="Personal Info"
android:textStyle="bold"
android:textSize="18sp"
android:id="#+id/tv_mrd_header"
style="#style/Job_on_Call"
android:textColor="#fff"
android:layout_marginTop="05dp"
android:layout_marginLeft="19dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus_one"
android:visibility="gone"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:id="#+id/iv_mrd_add" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/edit_one"
android:visibility="visible"
android:layout_marginRight="5dp"
android:layout_marginLeft="10dp"
android:id="#+id/iv_mrd_edit" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_left"
android:src="#drawable/left_arrow" />
<ViewFlipper
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:visibility="visible"
android:id="#+id/mrd_view_flipper"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_personal_details">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nationality"
android:textStyle="bold"
android:id="#+id/tv_mrd_nationality_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:id="#+id/ll_my_resource_refference_details">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold"
android:id="#+id/tv_mrd_ref_name_header"
android:layout_marginTop="02dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Document Type"
android:textStyle="bold"
android:id="#+id/dumm1"
android:layout_marginTop="05dp"
style="#style/Job_on_Call"
android:textColor="#000" />
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/mrd_profile_pic"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ViewFlipper>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:padding="5dp"
android:id="#+id/iv_mrd_go_right"
android:src="#drawable/right_arrow" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/add_doc_circles"
android:orientation="horizontal">
The layout where i want to create buttons dynamically
</LinearLayout>
</LinearLayout>
Code
if (NewDataSet.get("Table2") instanceof JSONArray) {
isDocPresent = true;
JSONArray array = NewDataSet.getJSONArray("Table2");
numOfDocCircles = array.length();
LayoutInflater layoutInflater;
Button button = new Button(context);
for (int k = 0; k < array.length(); k++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Error
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Please help me out whats the problem?
Move your button creation into the for loop, in your code you are basically just adding the same Button instance to the parent layout several times:
for (int k = 0; k < array.length(); k++) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(convertDpToPixel(30), convertDpToPixel(30));
button.setLayoutParams(params);
button.setText("" + k);
llAddDocCircles.addView(button);
}
Use below method to convert dp to pixel.
public static float convertDpToPixel(float dp){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
In your for loop call this method as:
for (int k = 0; k < array.length(); k++) {
llAddDocCircles.addView(addMoreButton("" + k));
}
And your addMoreButton() method is:
public Button addMoreButton(String text) {
Button button = new Button(context);
button.setId(buttonID);
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
button LayoutParams.setMargins(10, 10, 10, 10); //Add this if you want margin of 10dp
button.setLayoutParams(buttonLayoutParams );
buttonArrayList.add(button);
buttonID++;
return button;
}
And you can easily access these button value by:
private static int buttonID = 0;
private ArrayList<Button> buttonArrayList= new ArrayList<Button>();
// loop to handle each button
for (int i = 0; i < buttonArrayList.size(); i++) {
Button button = buttonArrayList.get(i);
// do your stuff with each button
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}

Categories

Resources