How to inflate xml layout in android? - android

Hi Iam inflating a layout into another one. But it is overwriting the existing layout. Please do let me know to inflate a layout into another layout if anybody knows. When the plus button is pressed it should inflate a layout.
My xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="14dp"
android:text="Daily Call Report"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="253dp"
android:text="Date"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/dayworkcategory"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:layout_marginTop="10dp"
android:text="Day Work Plan:"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="101dp"
android:layout_toRightOf="#+id/spinner1" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/spinner2"
android:layout_alignLeft="#+id/spinner2"
android:text="Category:"
android:textSize="15dp"
android:textStyle="bold" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/placeworkstation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/dayworkcategory"
android:layout_marginTop="12dp"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:ems="10"
android:hint="STATION NAME" >
</AutoCompleteTextView>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_marginLeft="27dp"
android:layout_toRightOf="#+id/textView4"
android:text="Actual Working :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="16dp"
android:text="Place of work(as per TP) :"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/placeworkstation"
android:layout_marginLeft="28dp"
android:text="Stockists Visited :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/stocklist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView6"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Stocklist Name" >
</AutoCompleteTextView>
<Spinner
android:id="#+id/spinner3"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/autoCompleteTextView2"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/autoCompleteTextView2" />
<EditText
android:id="#+id/editText1"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/autoCompleteTextView2"
android:layout_alignBottom="#+id/autoCompleteTextView2"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/spinner3"
android:ems="10"
android:hint="Qty" />
<EditText
android:id="#+id/editText2"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/autoCompleteTextView2"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/editText1"
android:ems="10"
android:hint="Remarks" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/editText2"
android:text="+" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/chemistlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView7"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Chemist Name" />
<EditText
android:id="#+id/editText4"
android:layout_width="145dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button2"
android:layout_marginRight="63dp"
android:ems="10"
android:hint="Remarks" >
</EditText>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_marginLeft="472dp"
android:layout_toRightOf="#+id/autoCompleteTextView3"
android:text="+" />
<EditText
android:id="#+id/editText3"
android:layout_width="60dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/editText4"
android:ems="10"
android:hint="Qty" />
<Spinner
android:id="#+id/spinner4"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="19dp"
android:layout_toLeftOf="#+id/editText3" />
</RelativeLayout>
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/chemistlist"
android:layout_marginTop="12dp"
android:text="Travel Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/travelexpenses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView8"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="FROM" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="43dp"
android:layout_alignBaseline="#+id/editText6"
android:layout_alignBottom="#+id/editText6"
android:layout_alignLeft="#+id/editText6"
android:layout_marginLeft="125dp"
android:text="+" />
<Spinner
android:id="#+id/spinner5"
android:layout_width="175dp"
android:layout_height="45dp"
android:layout_alignBottom="#+id/autoCompleteTextView5"
android:layout_marginLeft="13dp"
android:layout_toRightOf="#+id/autoCompleteTextView5" />
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView5"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/autoCompleteTextView4"
android:layout_alignBottom="#+id/autoCompleteTextView4"
android:layout_marginLeft="37dp"
android:layout_toRightOf="#+id/autoCompleteTextView4"
android:ems="10"
android:hint="TO" >
</AutoCompleteTextView>
<EditText
android:id="#+id/editText6"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText5"
android:layout_alignBottom="#+id/editText5"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/editText5"
android:ems="10"
android:hint="Km" />
<EditText
android:id="#+id/editText5"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/spinner5"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/spinner5"
android:ems="10"
android:hint="AMOUNT" >
</EditText>
</RelativeLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/travelexpenses"
android:layout_marginTop="14dp"
android:text="Miscelleneous Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/chemistlist"
android:layout_centerVertical="true"
android:text="Chemists Visited :"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_alignRight="#+id/travelexpenses"
android:layout_marginRight="34dp"
android:text="Daily Allowance Expenses Detail :"
android:textSize="20dp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/miscelleneous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView9"
android:orientation="vertical" >
<EditText
android:id="#+id/editText7"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="Amount" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignTop="#+id/editText7"
android:layout_marginLeft="276dp"
android:layout_toRightOf="#+id/editText7"
android:text="+" />
<EditText
android:id="#+id/editText8"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignTop="#+id/button4"
android:layout_marginLeft="40dp"
android:layout_toRightOf="#+id/editText7"
android:ems="10"
android:hint="Remarks" />
</RelativeLayout>
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBottom="#+id/miscelleneous"
android:layout_alignLeft="#+id/textView10"
android:layout_marginBottom="10dp"
android:layout_marginLeft="35dp"
android:text="Total(INR):"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignBaseline="#+id/textView11"
android:layout_alignBottom="#+id/textView11"
android:layout_marginLeft="23dp"
android:layout_toRightOf="#+id/textView11"
android:text="TOTAL"
android:textSize="20dp"
android:textStyle="bold" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dayworkcategory"
android:layout_marginRight="33dp"
android:text="UPGRADE DRAFT" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/placeworkstation"
android:text="SUBMIT LATER" />
</RelativeLayout>
My code for inflating the layout:
package abts.medismo.e_detailing;
import abts.medismo.e_detailing.Model.Spinmodel;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class DCR extends Activity {
Spinner spinworkplan, spincategory, spinstockproduct, spinchemistproduct,
spintravelmode;
Spinner autostation, autostock, autochemist;
String dbcatid, dbcatname, strcatname, spcatid, spcatname, strcatid;
RelativeLayout stocklist;
int i = 1;
int count = 0;
View rowView;
private View mExclusiveEmptyView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dcrdummo);
spinworkplan = (Spinner) findViewById(R.id.spinner1);
spincategory = (Spinner) findViewById(R.id.spinner2);
spinstockproduct = (Spinner) findViewById(R.id.spinner3);
spinchemistproduct = (Spinner) findViewById(R.id.spinner4);
spintravelmode = (Spinner) findViewById(R.id.spinner5);
stocklist = (RelativeLayout) findViewById(R.id.stocklist);
DatabaseHandler dbh = new DatabaseHandler(DCR.this);
SQLiteDatabase db = dbh.getWritableDatabase();
final ArrayAdapter<Spinmodel> adapterWorkPlan = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
adapterWorkPlan
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterWorkPlan.add(new Spinmodel("Doctor Visit", "Doctor Visit"));
adapterWorkPlan.add(new Spinmodel("Transit", "Transit"));
adapterWorkPlan.add(new Spinmodel("Meeting", "Meeting"));
adapterWorkPlan.add(new Spinmodel("Strike", "Strike"));
adapterWorkPlan.add(new Spinmodel("Head Quarters", "Head Quarters"));
adapterWorkPlan.add(new Spinmodel("Sales Closing", "Sales Closing"));
adapterWorkPlan.add(new Spinmodel("Others", "Others"));
spinworkplan.setAdapter(adapterWorkPlan);
final ArrayAdapter<Spinmodel> adapterCategory = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
String strquery1 = "SELECT * FROM category";
Cursor cursor = db.rawQuery(strquery1, null);
if (cursor.getCount() != 0) {
cursor.moveToFirst();
do {
dbcatid += "," + cursor.getString(0);
dbcatname += "," + cursor.getString(1);
} while (cursor.moveToNext());
}
System.out.println("dbCatid-->>" + dbcatid);
System.out.println("dbCatName-->>" + dbcatname);
if (dbcatname != null) {
int pos = dbcatname.indexOf(",");
int pos1 = dbcatid.indexOf(",");
if (pos == pos1) {
strcatid = dbcatid.substring(pos1 + 1);
strcatname = dbcatname.substring(pos + 1);
}
String delimiter = "\\,";
String[] catid = strcatid.split(delimiter);
String[] catname = strcatname.split(delimiter);
for (int i = 0; i < catid.length; i++) {
spcatid = catid[i];
spcatname = catname[i];
adapterCategory.add(new Spinmodel(spcatname, spcatid));
}
spincategory.setAdapter(adapterCategory);
}
dbcatid = null;
dbcatname = null;
final ArrayAdapter<Spinmodel> adapterStockProduct = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
String strproductquery = "SELECT * FROM product";
Cursor productcursor = db.rawQuery(strproductquery, null);
if (productcursor.getCount() != 0) {
productcursor.moveToFirst();
do {
dbcatid += "," + productcursor.getString(0);
dbcatname += "," + productcursor.getString(1);
} while (productcursor.moveToNext());
}
if (dbcatname != null) {
int pos = dbcatname.indexOf(",");
int pos1 = dbcatid.indexOf(",");
if (pos == pos1) {
strcatid = dbcatid.substring(pos1 + 1);
strcatname = dbcatname.substring(pos + 1);
}
String delimiter = "\\,";
String[] catid = strcatid.split(delimiter);
String[] catname = strcatname.split(delimiter);
for (int i = 0; i < catid.length; i++) {
spcatid = catid[i];
spcatname = catname[i];
adapterStockProduct.add(new Spinmodel(spcatname, spcatid));
}
spinstockproduct.setAdapter(adapterStockProduct);
spinchemistproduct.setAdapter(adapterStockProduct);
}
final ArrayAdapter<Spinmodel> adaptertravelmode = new ArrayAdapter<Spinmodel>(
this, android.R.layout.simple_spinner_item);
adaptertravelmode
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adaptertravelmode.add(new Spinmodel("TwoWheeler", "TwoWheeler"));
adaptertravelmode.add(new Spinmodel("Bus", "Bus"));
adaptertravelmode.add(new Spinmodel("Car", "Car"));
adaptertravelmode.add(new Spinmodel("Train", "Train"));
adaptertravelmode.add(new Spinmodel("Airlines", "Airlines"));
spintravelmode.setAdapter(adaptertravelmode);
}
public void onAddNewClicked(View v) {
count++;
System.out.println("value of i: " + i);
inflateEditRow(count);
v.setVisibility(View.VISIBLE);
System.out.println("value of count " + count);
}
private void inflateEditRow(int id) {
i = id - 1;
for (int j = i; j < id; j++) {
System.out.println("value of j " + j);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.dummaa, null);
mExclusiveEmptyView = rowView;
rowView.setId(id);
stocklist.addView(rowView);
// stocklist.addView(rowView, stocklist.getChildCount());
}
}
public void onDeleteClicked(View v) {
stocklist.removeView((View) v.getParent());
}
}

I got a bit lost in your code, but if I understood right you may need to have the main view as a parameter in rowView = inflater.inflate(R.layout.dummaa, null); instead of passing null.

Related

Button not respond to on click event

I'm trying for hours to make the button click but for some weird reason it doesnt work.
I have the main activity and from there I set the content view to first run activity and this is the code of the first run activity
import android.app.Activity;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class FirstRun extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_run);
final Button button = (Button) findViewById(R.id.nextBtn);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Test","test");
System.out.println("Button one is pressed");
button.setText("Test");
//Get edit text values
EditText nameET = (EditText) findViewById(R.id.Name);
EditText ageET = (EditText) findViewById(R.id.Age);
EditText emailET = (EditText) findViewById(R.id.Email);
EditText phoneET = (EditText) findViewById(R.id.Phone);
//set data in the info
//info = new UserInfo(nameET.getText().toString(),emailET.getText().toString(),phoneET.getText().toString());
if (nameET.getText().toString() != "" && ageET.getText().toString() != "" && emailET.getText().toString() != "" && phoneET.getText().toString() != "") {
SharedPreferences settings = getSharedPreferences("UserInfo", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("Name", nameET.getText().toString());
editor.putString("Email", emailET.getText().toString());
editor.putString("Phone", phoneET.getText().toString());
editor.putInt("Age", Integer.parseInt(ageET.getText().toString()));
}
}
});
}
}
Xml Layout details about the layout of the app and the button
i tried to add to the button onclick event on xml but every time I did it the app just crush and didn't respond at all
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp"
android:background="#drawable/backgroundimage">
<TextView
android:id="#+id/WelcomeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="#string/welcome_to_my_app"
android:textColor="#android:color/background_dark"
android:textSize="24sp" />
<Space
android:layout_width="fill_parent"
android:layout_height="10dp" />
<TextView
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="#string/please_fill_the_information"
android:textColor="#android:color/holo_blue_dark"
android:textSize="20sp" />
<Space
android:layout_width="fill_parent"
android:layout_height="10dp" />
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="3"
android:rowCount="7">
<TextView
android:id="#+id/nameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="2"
android:text="#string/name"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Name"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:capitalize="sentences"
android:ems="14"
android:inputType="textPersonName"
android:visibility="visible" />
<TextView
android:id="#+id/phoneTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="3"
android:text="#string/phone"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Phone"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:ems="14"
android:inputType="phone"
android:visibility="visible" />
<TextView
android:id="#+id/emailTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="4"
android:text="#string/email"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Email"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="4"
android:ems="14"
android:inputType="textEmailAddress"
android:visibility="visible" />
<TextView
android:id="#+id/ageTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_row="5"
android:text="#string/age"
android:textColor="#color/colorAccent"
android:textSize="18sp" />
<EditText
android:id="#+id/Age"
style="#style/Widget.AppCompat.EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="5"
android:ems="14"
android:inputType="number"
android:visibility="visible" />
</GridLayout>
<Button
android:id="#+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#android:color/holo_blue_dark"
android:text="#string/next" />
</LinearLayout>

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 wrap Dialog Window to Content?

I'm developing a Dialog, which should shrink to its content, so, I want to get a behaviour like wrap_content in a common view but for a normal Dialog.
That's what I want to show in a Dialog Window
In a Dialog Window should look like this
but this is what I actually get
Could you say me what I do wrong?
I thank you in Advance.
That's my code
...
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.advanced_options);
dialog.setTitle(titleId);
// Stuff referred to builder
AlertDialog.Builder builder ...
...
int type = WindowManager.LayoutParams.TYPE_INPUT_METHOD;
WindowManager.LayoutParams w_layout_params = new WindowManager.LayoutParams(type);
dialog.getWindow().setAttributes(w_layout_params );
...
builder.create();
dialog.show();
advanced_options.xml Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayout_advancedOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="" >
<TextView
android:id="#+id/speed_limit"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/speed_limit"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp" />
<TextView
android:id="#+id/speed_limit_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/speed_limit"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="#string/speed_limit_alert"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/speed_limit_alert"
android:layout_toRightOf="#+id/speed_limit_alert_edit_text"
android:text="#string/percentage_symbol"
android:textColor="#color/black" />
<TextView
android:id="#+id/minimum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/speed_limit_alert"
android:layout_below="#+id/speed_limit_alert_edit_text"
android:layout_marginTop="15dp"
android:text="#string/minimum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/minimum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minimum_speed"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/speed_limit_alert_edit_text"
android:ems="3"
android:inputType="number"
android:textColor="#color/black"
android:text="6" />
<TextView
android:id="#+id/minimum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/percent"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/maximum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minimum_speed"
android:layout_below="#+id/minimum_speed_edit_text"
android:layout_marginTop="15dp"
android:text="#string/maximum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black"/>
<EditText
android:id="#+id/maximum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_speed"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_alignLeft="#+id/minimum_speed_edit_text"
android:ems="3"
android:inputType="number"
android:textColor="#color/black"
android:text="18" />
<TextView
android:id="#+id/maximum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_toRightOf="#+id/maximum_speed_edit_text"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black"/>
<TextView
android:id="#+id/mobile_device_performance"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/maximum_speed_edit_text"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="#string/mobile_device_performance"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/maximum_number_objects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/maximum_speed"
android:layout_below="#+id/mobile_device_performance"
android:layout_marginTop="20dp"
android:text="#string/maximum_objects_in_view"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/maximum_number_objects_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_number_objects"
android:layout_alignBottom="#+id/maximum_number_objects"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/maximum_number_objects"
android:ems="3"
android:inputType="number"
android:text="450"
android:textColor="#color/black" />
<EditText
android:id="#+id/speed_limit_alert_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/percent"
android:layout_alignBottom="#+id/percent"
android:layout_alignLeft="#+id/maximum_number_objects_edit_text"
android:ems="3"
android:inputType="number"
android:text="42"
android:textColor="#color/black" >
<requestFocus />
</EditText>
<View
android:id="#+id/void_view"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/maximum_speed_unit"
android:layout_below="#+id/maximum_number_objects" />
Change RelativeView's width to match_parent
What you're looking for is match_parent's behavior.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLayout_advancedOptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="" >
Note: wrap_content in android specifies the view to use as little space as needed and that is the behavior you're getting.
I could adjust the Dialog (NOT AlertDialog!) to my custom Layout but I couldn't* set either the Title or the Buttons so that I got an Exception cause of setting content before requestFeature, then I decided to custom my Dialog and including the Buttons and title in my Custom Layout.
*I have researched and it's possible to overcome that if we define the Dialog in OnCreate other even in OnCreateDialog but I have an extra Class just for static Dialog Methods and was no solution for me.
In Addition I post what it was useful for me, perhaps someone is also useful.
Now my custom dialog looks like this
Here the code for whom wants to test it.
/**
* #param c
* the Context
* #return the about dialog
*/
public static void getAdvancedOptions(final Activity activity) {
Log.i("TAG", "Dialogs::getAdvancedOptions:: 0");
//settings = session.getSettings();
// Creation of a Dialog with Frame and title Characteristics
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.advanced_options_expanded);
// Here we control the validity of "edit_text" Fields
speed_limit_alert = (EditText)dialog.findViewById(R.id.speed_limit_alert_edit_text);
minimum_speed = (EditText)dialog.findViewById(R.id.minimum_speed_edit_text);
maximum_speed = (EditText)dialog.findViewById(R.id.maximum_speed_edit_text);
max_objects_in_view = (EditText)dialog.findViewById(R.id.maximum_number_objects_edit_text);
//showAdvancedOptions("getAdvancedOptions 1::");
// First we set the "SharedPreferences"-saved values on EditText Fields
performEditText(speed_limit_alert,Constants.MIN_PERCENTAGE, Constants.MAX_PERCENTAGE,"speed_limit_alert");
performEditText(minimum_speed, "","", "minimum_speed");
performEditText(maximum_speed, "","","maximum_speed");
performEditText(max_objects_in_view, "","","max_objects_in_view");
//showAdvancedOptions("getAdvancedOptions 4::");
ok_button = (Button)dialog.findViewById(R.id.ok_button);
cancel_button = (Button)dialog.findViewById(R.id.cancel_button);
// Definition of "OK" Button for the Dialog
ok_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
if ( dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
});
cancel_button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Log.i("TAG", "Dialogs::getAdvancedOptions::onClick");
if ( dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
});
dialog.show();
}
private static void performEditText(EditText edit_text, final String min_value, final String max_value, final String id) {
String edit_text_value = edit_text.getText().toString();
Log.i("TAG", "Dialogs::performEditText:: id: "+ id +" edit_text_value: "+edit_text_value+ " (min_value,max_value)=("+min_value+","+max_value+")");
TextWatcher textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
int i = 0;
Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 0 : s: "+s.toString());
int length = s.length();
if ( length == 0 )
i = 0;
else if ( length < 3 ) {
if ( s.charAt(0) == '0')
s.delete(1, length );
i = Integer.parseInt(s.toString());
Log.i("TAG","Dialogs::performEditText::afterTextChanged:id: "+ id +" - 1a : s: "+s+", i:"+i);
} else {
//String hundred = "100";
String s_value = s.toString();
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 1b : s: "+s+", i:"+i);
if ( ( min_value != null ) && ( min_value.length() != 0 ) && ( max_value != null) && ( max_value.length() != 0 )) {
if ( !s_value.equalsIgnoreCase(max_value))
s.delete(2, length );
}
i = Integer.parseInt(s.toString());
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 2b : i: "+i);
}
if (i >= 0 && i <= 100) {
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 3 : (i >= 0 && i <= 100): i: "+i);
//speed_limit_alert.setText(s); // This ensures 0-100 value for speed_limit_alert
}
Log.i("TAG","Dialogs::performEditText::afterTextChanged: id: "+ id +" - 4 : i: "+i);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.i("TAG","Dialogs::performEditText::beforeTextChanged: id: "+ id +" s: "+s);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("TAG","Dialogs::performEditText::onTextChanged: id: "+ id +" s: "+s);
}
};
edit_text.addTextChangedListener(textWatcher);
}
private static void showAdvancedOptions(String entryPoint) {
String s_l_a = speed_limit_alert.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: s_l_a: "+s_l_a);
String mi_s = minimum_speed.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: mi_s: "+mi_s);
String ma_s = maximum_speed.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: ma_s: "+ma_s);
String m_o_i_v = max_objects_in_view.getText().toString();
Log.i("TAG", entryPoint + "Dialogs::setAdvancedOptions:: m_o_i_v: "+m_o_i_v);
}
And the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/border_advanced_options"
android:divider="?android:listSeparatorTextViewStyle"
android:showDividers="middle" >
<RelativeLayout
android:id="#+id/relLayout_advancedOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:divider="?android:listSeparatorTextViewStyle"
android:orientation="vertical"
android:showDividers="" >
<TextView
android:id="#+id/speed_limit"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/void_view_0"
android:layout_marginTop="10dp"
android:text="#string/speed_limit"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp" />
<TextView
android:id="#+id/speed_limit_alert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/speed_limit"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="#string/speed_limit_alert"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/speed_limit_alert"
android:layout_toRightOf="#+id/speed_limit_alert_edit_text"
android:text="#string/percentage_symbol"
android:textColor="#color/black" />
<TextView
android:id="#+id/minimum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/speed_limit_alert"
android:layout_below="#+id/speed_limit_alert_edit_text"
android:layout_marginTop="15dp"
android:text="#string/minimum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/minimum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minimum_speed"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/speed_limit_alert_edit_text"
android:ems="3"
android:inputType="number"
android:text="6"
android:textColor="#color/black" />
<TextView
android:id="#+id/minimum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/minimum_speed"
android:layout_alignLeft="#+id/percent"
android:paddingRight="5dp"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/maximum_speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minimum_speed"
android:layout_below="#+id/minimum_speed_edit_text"
android:layout_marginTop="15dp"
android:text="#string/maximum_speed"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/maximum_speed_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_speed"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_alignLeft="#+id/minimum_speed_edit_text"
android:ems="3"
android:inputType="number"
android:text="18"
android:textColor="#color/black" />
<TextView
android:id="#+id/maximum_speed_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/maximum_speed"
android:layout_alignRight="#+id/minimum_speed_unit"
android:layout_toRightOf="#+id/maximum_speed_edit_text"
android:paddingRight="5dp"
android:text="#string/metric_speed_unit"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<TextView
android:id="#+id/mobile_device_performance"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/maximum_speed_edit_text"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="#string/mobile_device_performance"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/maximum_number_objects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/maximum_speed"
android:layout_below="#+id/mobile_device_performance"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="#string/maximum_objects_in_view"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<EditText
android:id="#+id/maximum_number_objects_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/maximum_number_objects"
android:layout_alignBottom="#+id/maximum_number_objects"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/maximum_number_objects"
android:ems="3"
android:inputType="number"
android:text="450"
android:textColor="#color/black" />
<EditText
android:id="#+id/speed_limit_alert_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/percent"
android:layout_alignBottom="#+id/percent"
android:layout_alignLeft="#+id/maximum_number_objects_edit_text"
android:digits="0123456789"
android:ems="3"
android:inputType="number"
android:text="42"
android:textColor="#color/black" >
<requestFocus />
</EditText>
<View
android:id="#+id/void_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/lin_layout_buttons"
android:layout_below="#+id/maximum_number_objects"
android:background="#color/black" />
<TextView
android:id="#+id/title_advanced_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/minimum_speed_unit"
android:drawableLeft="#drawable/ic_launcher_48"
android:drawableRight="#drawable/ic_action_settings_48"
android:gravity="center|center_vertical"
android:paddingLeft="5dp"
android:text="Advanced Options"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/blue" />
<View
android:id="#+id/void_view_0"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/title_advanced_options"
android:layout_below="#+id/title_advanced_options"
android:background="#color/black" />
<LinearLayout
android:id="#+id/lin_layout_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/minimum_speed_unit"
android:layout_alignTop="#+id/void_view"
android:weightSum="3" >
<Button
android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/maximum_number_objects"
android:layout_alignRight="#+id/speed_limit_alert"
android:layout_below="#+id/maximum_number_objects_edit_text"
android:layout_weight="1.5"
android:text="OK" />
<Button
android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:layout_weight="1.5"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>

Inflating layout in android

Hi i want to inflate an xml layout in another layout. Iam doing it correctly but it is not properly coming. Because the page in which i inflating the layout contains 2 relative layouts. I give different id'sandroid:id="#+id/stocklist" and android:id="#+id/gifts" for the two relative layouts. The stocklist layout contains a + button and when we click the + button it has to inflate the layout below to it.When iam inflating another layout inside the stocklist layout, the inflated xml is not properly alligned and it is not coming below the parent layout. Please help me if anybody knows. I added my code and layout here.
My parent layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="704dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView8"
android:layout_alignBottom="#+id/textView8"
android:layout_toRightOf="#+id/textView7"
android:text="Quantity"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RelativeLayout
android:id="#+id/stocklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/textView8"
android:layout_marginTop="11dp"
android:orientation="vertical" >
<Spinner
android:id="#+id/spinproducts"
android:layout_width="175dp"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Stocklist Name" >
</Spinner>
<EditText
android:id="#+id/editText1"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_alignTop="#+id/spinproducts"
android:layout_marginLeft="146dp"
android:layout_toRightOf="#+id/spinproducts"
android:ems="10"
android:inputType="number"
android:hint="Qty" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_marginRight="136dp"
android:layout_toLeftOf="#+id/spindetail" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="11dp"
android:onClick="onAddNewClicked"
android:text="+" />
<Spinner
android:id="#+id/spindetail"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginRight="50dp"
android:layout_toLeftOf="#+id/button1"
android:ems="10"
android:hint="Remarks" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/gifts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/textView12"
android:layout_marginTop="11dp"
android:orientation="vertical" >
<Spinner
android:id="#+id/spingift"
android:layout_width="375dp"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Stocklist Name" >
</Spinner>
<EditText
android:id="#+id/etgiftqty"
android:layout_width="500dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/spingift"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/spingift"
android:ems="10"
android:inputType="number"
android:hint="Qty" >
</EditText>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="+" />
</RelativeLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView12"
android:layout_below="#+id/gifts"
android:layout_marginTop="33dp"
android:text="Call Outcome :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView14"
android:layout_below="#+id/textView14"
android:layout_marginTop="20dp"
android:text="Follow Up:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/stocklist"
android:layout_marginLeft="10dp"
android:layout_marginTop="16dp"
android:text="Gifts / Others :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView12"
android:layout_below="#+id/textView7"
android:layout_marginTop="12dp"
android:text="Product"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignLeft="#+id/textView13"
android:layout_alignRight="#+id/editText8"
android:layout_below="#+id/textView13"
android:layout_marginTop="12dp"
android:ems="10" >
</EditText>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner4"
android:layout_alignParentBottom="true"
android:layout_marginLeft="13dp"
android:text="Dr.Not Available" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText6"
android:layout_alignParentBottom="true"
android:layout_marginLeft="16dp"
android:text="Add Call" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="51dp"
android:layout_toRightOf="#+id/button3"
android:text="Cancel" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/spinner1"
android:layout_toLeftOf="#+id/textView9"
android:text="In Time:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<MultiAutoCompleteTextView
android:id="#+id/multiAutoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/spinner3"
android:layout_marginRight="17dp"
android:ems="10" >
</MultiAutoCompleteTextView>
<EditText
android:id="#+id/editText8"
android:layout_width="130dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText7"
android:layout_alignBottom="#+id/editText7"
android:layout_alignRight="#+id/multiAutoCompleteTextView1"
android:ems="10"
android:hint="Notes">
</EditText>
<EditText
android:id="#+id/editText6"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/spinner3"
android:layout_alignTop="#+id/spinner4"
android:ems="10"
android:hint="Amount">
</EditText>
<EditText
android:id="#+id/editText7"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/editText6"
android:layout_alignBottom="#+id/editText6"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#+id/editText8"
android:ems="10"
android:hint="date"/>
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView15"
android:layout_toLeftOf="#+id/editText7"
android:text="Spon.Req"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/spinner1"
android:text="Detailing and Samples Dispensed :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/textView2" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:layout_marginTop="20dp"
android:text="Schedule Follow"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner1"
android:layout_below="#+id/textView1"
android:layout_marginTop="12dp"
android:text="Location of Visit"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentTop="true"
android:text="Doctor Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#008000"
android:textSize="35dp"
android:textStyle="bold" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/textView15"
android:layout_alignBottom="#+id/textView15"
android:layout_alignLeft="#+id/editText5"
android:ems="10" >
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/multiAutoCompleteTextView1"
android:layout_marginLeft="41dp"
android:layout_toRightOf="#+id/button4"
android:text="Work with"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView9"
android:layout_alignBottom="#+id/textView9"
android:layout_marginRight="38dp"
android:layout_toLeftOf="#+id/textView17"
android:text="Detailed"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="123dp"
android:layout_height="38dp"
android:layout_alignLeft="#+id/textView9"
android:layout_alignTop="#+id/textView5"
android:layout_marginLeft="22dp" />
<EditText
android:id="#+id/editText5"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="#+id/editText4"
android:layout_marginLeft="29dp"
android:layout_marginTop="11dp"
android:layout_toRightOf="#+id/spinner1"
android:ems="10" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText5"
android:layout_alignBottom="#+id/editText5"
android:layout_alignLeft="#+id/textView15"
android:text="Follow Up Actions:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/stocklist"
android:layout_alignLeft="#+id/editText7"
android:layout_marginLeft="25dp"
android:text="Discussion Topics"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView10"
android:layout_below="#+id/textView2"
android:text="Out Time:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner3"
android:layout_width="123dp"
android:layout_height="38dp"
android:layout_above="#+id/textView7"
android:layout_toLeftOf="#+id/textView11" />
<Spinner
android:id="#+id/spinner4"
android:layout_width="145dp"
android:layout_height="40dp"
android:layout_alignRight="#+id/textView10"
android:layout_alignTop="#+id/editText5" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/spinner3"
android:layout_alignLeft="#+id/spinner4"
android:text="Duration of Visit"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Layout which has to be inflated:
<?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="50dp"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/textView8"
android:layout_marginTop="11dp"
android:orientation="vertical" >
<Spinner
android:id="#+id/inflatespinproducts"
android:layout_width="175dp"
android:layout_height="40dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="Stocklist Name" >
</Spinner>
<EditText
android:id="#+id/inflateeditText1"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_alignTop="#+id/inflatespinproducts"
android:layout_marginLeft="146dp"
android:layout_toRightOf="#+id/inflatespinproducts"
android:ems="10"
android:inputType="number"
android:hint="Qty" />
<CheckBox
android:id="#+id/inflatecheckBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/inflateeditText1"
android:layout_marginRight="136dp"
android:layout_toLeftOf="#+id/inflatespindetail" />
<Button
android:id="#+id/inflatebutton1"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="11dp"
android:onClick="onDeleteClicked"
android:text="-" />
<Spinner
android:id="#+id/inflatespindetail"
android:layout_width="180dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginRight="50dp"
android:layout_toLeftOf="#+id/inflatebutton1"
android:ems="10"
android:hint="Remarks" />
</RelativeLayout>
</LinearLayout>
After inflating the layout my page looks like:
http://i.stack.imgur.com/SOWBJ.png">
My source code:
package abts.medismo.e_detailing;
import abts.medismo.e_detailing.Model.Spinmodel;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class addcall extends Activity {
CheckBox chkdetailed;
TextView txtdocname, txtdisctopic;
MultiAutoCompleteTextView autoWorkwith;
Spinner spinlocation, intime, outtime, spinproducts, spingift, spindetail,
spinSponsorship;
String docid, docname, addid, addname, straddid, straddname, spaddid,
spaddname, dbcatid, dbcatname;
String strcatid, strcatname, spcatid, spcatname;
String spinlocationvalue, spingiftvalue, spinintimevalue, spinouttimevalue,
spinproductvalue, spinworkwithvalue, spindetailvalue,
spinsponsorshipvalue;
Intent bgIntent;
EditText etprodqty,etgiftqty;
RelativeLayout relStocklist;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dupaddcall);
autoWorkwith = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
autoWorkwith
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
etprodqty=(EditText)findViewById(R.id.editText1);
etgiftqty=(EditText)findViewById(R.id.etgiftqty);
relStocklist=(RelativeLayout)findViewById(R.id.stocklist);
spinlocation = (Spinner) findViewById(R.id.spinner1);
spindetail = (Spinner) findViewById(R.id.spindetail);
spinSponsorship = (Spinner) findViewById(R.id.spinner4);
chkdetailed = (CheckBox) findViewById(R.id.checkBox1);
chkdetailed.setChecked(true);
intime = (Spinner) findViewById(R.id.spinner2);
outtime = (Spinner) findViewById(R.id.spinner3);
spinproducts = (Spinner) findViewById(R.id.spinproducts);
spingift = (Spinner) findViewById(R.id.spingift);
txtdocname = (TextView) findViewById(R.id.textView1);
txtdisctopic = (TextView) findViewById(R.id.textView11);
spindetail.setEnabled(false);
bgIntent = getIntent();
docid = bgIntent.getStringExtra("docid");
docname = bgIntent.getStringExtra("docname");
txtdocname.setText(docname);
DatabaseHandler dbh = new DatabaseHandler(addcall.this);
SQLiteDatabase db = dbh.getWritableDatabase();
addid = null;
addname = null;
}
public void onAddNewClicked(View v) {
inflateEditRow(null);
//v.setVisibility(View.VISIBLE);
}
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.inflateaddproduct, null);
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
// Inflate at the end of all rows but before the "Add new" button
relStocklist.addView(rowView, relStocklist.getChildCount());
}
public void onDeleteClicked(View v) {
relStocklist.removeView((View) v.getParent());
}
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(addcall.this, DCR.class);
intent.putExtra("planid", bgIntent.getStringExtra("planid"));
intent.putExtra("tourdate", bgIntent.getStringExtra("tourdate"));
final int result = 1;
startActivityForResult(intent, result);
finish();
}
}
You should change your relative stocklist layout to a linear layout, so the inflated layouts automatically arrange below each other.
<LinearLayout
android:id="#+id/stocklist"
android:orientation="vertical"
... >
...
</LinearLayout>

Button Click is not working.View is posting message on buttons

Now i encountered problem while debugging my code,that the Buttons in my application is not responding to click action.I kept break point on Click view method but on click view method is not responding.
What i am doing in on Click method is, if i clicked the first button then swapping the value with empty button value.But on click is not responding.
Here is my full code to check.
public class Play extends Activity implements OnClickListener
{
private ArrayList<Integer> m_buildButtonsIDs;
private ArrayList<Integer> m_gameButtonsIDs;
public static Character UserWords;
public static Character RemainingWords;
public static Character OpenCard;
static ArrayList<Character> ShuffledCards = new ArrayList<Character>();
static ArrayList<Character> UserBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWords = new ArrayList<Character>();
static ArrayList<Character> RemainingBuildWordsAfterShowCard = new ArrayList<Character>();
static ArrayList<Character> DroppedCards = new ArrayList<Character>();
static Stack<Character>UserDroppedCards = new Stack<Character>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
InitializeBoard();
}
public void InitializeBoard()
{
m_buildButtonsIDs = new ArrayList<Integer>();
m_buildButtonsIDs.add(R.id.Button1);
m_buildButtonsIDs.add(R.id.Button2);
m_buildButtonsIDs.add(R.id.Button3);
m_buildButtonsIDs.add(R.id.Button4);
m_buildButtonsIDs.add(R.id.Button5);
m_buildButtonsIDs.add(R.id.Button6);
m_buildButtonsIDs.add(R.id.Button7);
m_buildButtonsIDs.add(R.id.Button8);
m_buildButtonsIDs.add(R.id.Button9);
m_gameButtonsIDs = new ArrayList<Integer>();
m_gameButtonsIDs.add(R.id.pickButton);
m_gameButtonsIDs.add(R.id.showButton);
m_gameButtonsIDs.add(R.id.emptyButton);
String random = RandomAlphabetGenerator.Random();
for(int i = 0;i<random.length();i++)
{
char randomcards = random.charAt(i);
ShuffledCards.add(randomcards);
}
for(int i = 0;i < 9;i++)
{
UserWords = ShuffledCards.get(i);
UserBuildWords.add(UserWords);
if(i == 8)
{
for(int j = 9;j < 52;j++)
{
RemainingWords = ShuffledCards.get(j);
RemainingBuildWords.add(RemainingWords);
}
}
}
OpenCard = RemainingBuildWords.get(0);
DroppedCards.add(OpenCard);
RemainingBuildWords.remove(0);
RemainingBuildWordsAfterShowCard.addAll(RemainingBuildWords);
FillUserBuildButtons(ShuffledCards);
StackingDroppedButtons(DroppedCards);
StackingPickButtons(RemainingBuildWordsAfterShowCard);
}
private void FillUserBuildButtons(ArrayList<Character> shuffledCards)
{
for (int i=0 ; i<m_buildButtonsIDs.size() ; i++)
{
Button BuildButton = (Button)findViewById(m_buildButtonsIDs.get(i));
BuildButton.setText(UserBuildWords.get(i).toString());
}
}
private void StackingDroppedButtons(ArrayList<Character> droppedCards)
{
Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(1));
ShowButton.setText(DroppedCards.get(0).toString());
}
private void StackingPickButtons(ArrayList<Character> remainingBuildWordsAfterShowCard)
{
Button ShowButton = (Button)findViewById(m_gameButtonsIDs.get(0));
ShowButton.setText(RemainingBuildWordsAfterShowCard.get(0).toString());
}
public void onClick(View v)
{
CharSequence text;
switch(v.getId())
{
case R.id.Button1:
Button FirstButton = (Button)findViewById(m_buildButtonsIDs.get(0));
text = FirstButton.getText().toString();
FirstButton.setText(SwapValue(text));
break;
case R.id.emptyButton:
break;
}
}
private CharSequence SwapValue(CharSequence k)
{
CharSequence empty = null;
Button EmptyButton = (Button)findViewById(m_buildButtonsIDs.get(0));
empty = EmptyButton.getText().toString();
EmptyButton.setText(k);
return empty;
}
}
Here is my xml code for checking:
<TableLayout
android:id="#+id/MainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="50dp"
android:layout_marginLeft="115dp">
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp" >
<Button
android:id="#+id/Button1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Dummy"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Button4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button6"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="30dp">
<Button
android:id="#+id/Dummy2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Dummy3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Button7"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button8"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp" />
<Button
android:id="#+id/Button9"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/Dummy4"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
<Button
android:id="#+id/Dummy5"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:clickable="false"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp">
<Button
android:id="#+id/pickButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/showButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/button_label"
android:textSize="15dp"/>
<Button
android:id="#+id/emptyButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/empty"
android:textSize="15dp" />
<Button
android:id="#+id/emptyButton1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text=""
android:textSize="15dp" />
<Button
android:id="#+id/dropButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/drop"
android:textSize="15dp"/>
<Button
android:id="#+id/declareButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="72dp"
android:text="#string/declare"
android:textSize="15dp"/>
</LinearLayout>
</TableLayout>
you should add an onClickListener to your Button :
btn.setOnClickListener(Play.this);

Categories

Resources