I am having an issue with grabbing my Buttons and other elements from within my PopupWindow, using the debugger it just report back as null?
private void initiatePopupWindow()
{
try
{
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) LoginActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_register,(ViewGroup)
findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 600, 900, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnReg = (Button) findViewById(R.id.btnReg);
inputName = (EditText) findViewById(R.id.name);
inputDOB = (DatePicker) findViewById(R.id.dob);
String name = inputName.getText().toString();
btnReg.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if ( ( !inputName.getText().toString().equals("")) &&
( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) > 15) )
{
//register user
}
else if ( ( inputName.getText().toString().equals("")) )
{
Toast.makeText(getApplicationContext(),
"Please enter your name", Toast.LENGTH_SHORT).show();
}
else if (( getAge(inputDOB.getDayOfMonth(), inputDOB.getMonth(), inputDOB.getYear()) < 16) )
{
Toast.makeText(getApplicationContext(),
"You must be at least 16 to use this app", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), email, Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
Could anyone point me in the correct direction or explain why I can't find them using R.id?
Cheers
Update: Activity_Register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/rounded"
android:orientation="vertical"
android:padding="10sp" >
<EditText
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<requestFocus />
</EditText>
<DatePicker
android:id="#+id/dobPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:calendarViewShown="false"
android:layout_below="#+id/dob"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dob"
android:layout_below="#+id/name"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp"/>
<Button
android:id="#+id/btnReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="38dp"
android:text="#string/register"
android:layout_below="#+id/dobPicker"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
You can get like
View layout = inflater.inflate(R.layout.activity_register,
(ViewGroup) youractivity.this.findViewById(R.id.popup_element));
btnReg = (Button) layout.findViewById(R.id.btnReg);
inputName = (EditText) layout.findViewById(R.id.name);
inputDOB = (DatePicker) layout.findViewById(R.id.dob);
It's becoz your all the elements coming from inflated layout view and you need to pass context to get your popup_element layout as ViewGroup.
use this layout.findViewById(R.id.popup_element)); instead of this findViewById(R.id.popup_element));
Related
How to get value from dynamically created EditText in Android?
This is my dynamic view (XML file)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/background"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.
And here is my code:
LayoutInflater l = getLayoutInflater();
final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);
I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.
This is how I am trying to get inserted value but it returns value of only last created edit text:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
sb is StringBuilder sb;, w is String w;
String queryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(queryWayPoints);
int count3 = s.getCount();
for (int j = 0; j < count3; j++) {
s.moveToNext();
w = s.getString(s.getColumnIndex("waypoints"));
// se.append(w + "." + "00");
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());
Although, I didn't get what exactly your question is, still I'll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.techmahindra.stackques.MainActivity">
<Button
android:id="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(layout which will be inflated at runtime)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
There are some problems with your code. You're trying to add editTextWayPoints to mainActivity, but editTextWayPoints already has a parent, which is view. I think you should be adding view to mainActivity (mainActivity.addView(view)). Finally, to access the value of editTextWayPoints, you simply do editTextWayPoints.getText().toString();
I am trying to create a filter function, I have a pop up window in my Search activity it displays list of switches where the user can turn some of them off and on..
I got the pop up to display fine but whenever I tick any switch the value is not being passed to the search activity and not doing anything.
here is my codes:
search Activity snippet:
filter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
initiatePopupWindow();
}
});
popup function (inside SearchActivity)
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater)SearchActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_filter,
(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout ,ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
switch1 = (Switch) findViewById(R.id.parking);
switch2 = (Switch) findViewById(R.id.delivery);
switch3 = (Switch) findViewById(R.id.reservation);
switch4 = (Switch) findViewById(R.id.bar);
switch5 = (Switch) findViewById(R.id.card);
switch6 = (Switch) findViewById(R.id.wifi);
switch7 = (Switch) findViewById(R.id.terrace);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean bChecked) {
if (bChecked) {
parking_filter=1;
Toast.makeText(getApplicationContext(),
"Searching Database using : " + parking_filter,
Toast.LENGTH_SHORT).show();
} else {
parking_filter=0;
Toast.makeText(getApplicationContext(),
"Searching Database using : " + parking_filter,
Toast.LENGTH_SHORT).show();
}
}
});
activity_filter.xml (switches)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_element"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/cardview_light_background"
android:padding="10sp" >
<Switch
android:id="#+id/delivery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Has Delivery"
android:layout_below="#+id/reservation"
android:layout_alignParentStart="true" />
<Switch
android:id="#+id/parking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Has Parking"
android:layout_marginTop="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Switch
android:id="#+id/reservation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Takes Reservations"
android:layout_marginTop="20dp"
android:layout_below="#+id/terrace"
android:layout_alignParentStart="true" />
<Switch
android:id="#+id/terrace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="outdoor seating"
android:layout_below="#+id/bar"
android:layout_alignParentStart="true" />
<Switch
android:id="#+id/wifi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Has Wifi"
android:layout_below="#+id/parking"
android:layout_alignParentStart="true" />
<Switch
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Has Bar"
android:layout_below="#+id/wifi"
android:layout_alignParentStart="true" />
<Switch
android:id="#+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accept Cards"
android:layout_marginTop="20dp"
android:layout_below="#+id/delivery"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/btn_close_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
android:layout_below="#+id/card" />
</RelativeLayout>
I am using "toast" to keep track of the "parking filter" value but with no avail. in the log
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
Instead of:
switch1 = (Switch) findViewById(R.id.parking);
Do:
switch1 = (Switch) layout.findViewById(R.id.parking);
Your switch is null because you are looking for it in wrong place.
You must find view by id in exact layout that you inflating;
Change this
switch1 = (Switch) findViewById(R.id.parking);
to this
switch1 = (Switch) layout.findViewById(R.id.parking);
So I have the following code for a popup window in an activity:
private void initiatePopupWindow(String popupText, boolean hasEditText){
LayoutInflater inflater = (LayoutInflater) PlayActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
final PopupWindow popupWindow= new PopupWindow(layout, 300, 370, true);
findViewById(R.id.play_activity_layout).post(new Runnable() {
#Override
public void run() {
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
});
TextView textPopup= (TextView) layout.findViewById(R.id.tvPopupText);
textPopup.setText(popupText);
if(hasEditText) {
EditText et = (EditText) findViewById(R.id.etNumber);
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
btnClosePopup= (Button) layout.findViewById(R.id.btnClosePopup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
//finish();
}
});
}
I get the NullPointerException for this row:
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
I tried to fix this by searching on the site for ideas but still dont know why it isnt proper.
EDIT:
My popup_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#444444"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/tvPopupText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="#string/under_construction"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
<Button
android:id="#+id/btnClosePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/ok"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNumber"
android:inputType="number"
android:layout_alignRight="#+id/tvPopupText"/>
</RelativeLayout>
I think you have to use
EditText et = (EditText) layout.findViewById(R.id.etNumber);
see layout. missing.
Note that getText() never returns null. So only findViewById should raise the NullPointerException.
Try to change this:
final View layout= inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_layout));
for this other one:
// container = get it from param of onCreateView
final View layout= inflater.inflate(R.layout.popup_layout, container);
...
EditText et = (EditText) layout.findViewById(R.id.etNumber);
After you've fix the layout.findViewById....
Initialize the EditText to some default value...see the android:text="0" below
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNumber"
android:inputType="number"
android:layout_alignRight="#+id/tvPopupText"
android:text="0"
/>
And you should also ALWAYS wrap Integer.parseInt("string_value") in a try/catch block in case string_value is not a valid number:
try {
MAX_NUMBER_OF_MOVES = Integer.parseInt(et.getText().toString());
}
catch (NumberFormatException nfe_) {
MAX_NUMBER_OF_MOVES = 5; // Or whatever your default number should be
}
I am new to android world and am working on dialog and in that I am trying to create a text view, button and imageview, but it is neither showing me error nor textview or imageview. It is showing a button. Here is the image:
.
But i am want to display my dialogue as follows
My xml file is as follows ....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity">
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/submit_dialog"
android:layout_alignParentRight="true"
android:layout_marginRight="37dp"
android:contentDescription="#string/add_Friend"
android:src="#drawable/add" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="64dp"
android:layout_toLeftOf="#+id/add"
android:text="#string/submit" />
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/add"
android:layout_marginRight="17dp"
android:layout_toLeftOf="#+id/add"
android:ems="10" />
</RelativeLayout>
I am calling it in java file as follows
dialog = new Dialog(context);
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.activity_add_friends_dialog, null, false);
dialog.setContentView(view);
dialog.setCancelable(true);
//dialog.setContentView(R.layout.activity_add_friends_dialog);
//dialog.setTitle("Add Friends");
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
friendsCount++;
Log.d("addFriendsActivity", "ashish comes here ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
After some google I understand that the issues might be in r.java or you have imported android .r but i have not done any thing in this ..
Thanks in advance.... :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" />
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
There is no error in your java code. Its perfect you have just change your xml. Use this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".AddFriendsCustomActivity" >
<EditText
android:id="#+id/writename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/writename"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/writename"
android:contentDescription="Add Friend"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/submit_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/writename"
android:layout_below="#+id/add"
android:layout_marginRight="46dp"
android:text="Submit" />
</RelativeLayout>
For your xml I think it should look like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/add_btn" />
<ImageButton
android:id="#+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/add_btn"
android:background="#null" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/my_edit_text"
android:text="Submit" />
</RelativeLayout>
Be aware that there can be some typo mistakes, because I didn't compile that code, and you should change your real variables. And create your AlertDialog like this:
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MyActivity.this);
View mView = mInflater.inflater(R.layout.my_layout, null, false);
EditText mEdit = (EditText) mView.findViewById(R.id.my_edit_text);
ImageButton mImageBtn = (ImageButton) mView.findViewById(R.id.add_btn);
Button mSubmit = (Button) mView.findViewById(R.id.submit_btn);
mBuilder.setView(mView);
AlertDialog mAlert = mBuilder.create();
mAlert.show();
Using AlertDialog.Builder instead of Dialog is lot more easier for your situation (at least in my opinion). And you can add your Submit Button to your AlertDialog as default positive button.
Hope this helps you.
Look at your code i think
add= (ImageView) dialog.findViewById(R.id.add);
friends1 = (TextView) dialog.findViewById(R.id.writename);
as
add= (ImageView) view.findViewById(R.id.add);
friends1 = (EditText) view.findViewById(R.id.writename);
That should work.
Try this..
final Dialog dialog = new Dialog(AddFriendsActivity.this);
dialog.setContentView(R.layout.activity_add_friends_dialog);
dialog.setCanceledOnTouchOutside(true);
add= (ImageView) dialog.findViewById(R.id.add);
EditText friends1 = (EditText) dialog.findViewById(R.id.writename);
submit_dialog = (Button) dialog.findViewById(R.id.submit_dialog);
add.setOnClickListener(new OnClickListener() {
//#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
#SuppressLint("NewApi")
public void onClick(View v) {
friendsCount++;
//TextView textview = new TextView(context);
//textview.setId(friendsCount);
//final RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.BELOW, friendsCount--);
//textview.setLayoutParams(params);
//textview.setVisibility(View.VISIBLE);
//relativelayout.addView(textview ,params);
Log.d("addFriendsActivity", "ashish comes here ");
}
});
submit_dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(AddFriendsActivity.this, DetailsFriendsActivity.class);
startActivity(i);
}
});
dialog.show();
Your view is covered.
May be you can try to set your parent layout to Linearlayout, not RelativeLayout.
Hi hoping someone can help. I have a list view that displays various textviews,editviews and abutton on each row. I have used the getView
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.catlistrow, null);
}
TextView pDesc = (TextView) v.findViewById(R.id.productlinerow);
TextView pPack = (TextView) v.findViewById(R.id.productlinerow3);
ImageView iThumbnail = (ImageView) v.findViewById(R.id.Thumbnail);
final Button bAdd = (Button)v.findViewById(R.id.SOCatLine);
final EditText pOrdQty = (EditText) v.findViewById(R.id.SOQty);
pDesc.setText(((HashMap<String,String>) getItem(position)).get("Column2"));
pPack.setText(((HashMap<String,String>) getItem(position)).get("Column3"));
pOrdQty.setText(((HashMap<String,String>) getItem(position)).get("OrdQty"));
String EanCode = ((HashMap<String,String>) getItem(position)).get("EANCode");
I then add a OnClickListner for the add button with in the getView
bAdd.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Object o = list.get(position);
HashMap<String, String> map2 = (HashMap<String, String>)o;
String b = map2.get("OrdQty");
String sProdCode = map2.get("ProdCode");
String ProdPrice = map2.get("ProdPrice");
b = pOrdQty.getText().toString();
int OrderQty = Integer.parseInt(b);
//Check to see if add is pressed and qty zero
if ( OrderQty == 0 )
{
OrderQty = 1;
b = "1";
}
db.open();
int iOrderNo = Integer.parseInt(OrderNo);
// update line
iLineNumber = iLineNumber + 1;
int QtyCheck = db.createCATorderline(iOrderNo, iLineNumber,
sProdCode, ProdPrice, b,"P");
bAdd.setText("Change");
//Close the database
db.close();
}
});
The button text changes fine on the button i clicked on which is great. The problem im having is that if i scroll down every now and then i see the text has also changed on one of the buttons on the screen.
Can anyone help?
Thanks Neil
XML for the list view
<ImageView
android:id="#+id/Thumbnail"
android:layout_width="190px"
android:layout_height="200px"
android:layout_alignParentLeft="true"
android:maxWidth="190px"
android:maxHeight="200px"
android:background="#null" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/productlinerow"
android:textColor="#000000"
android:layout_marginTop="10dip"
android:textSize="25dip"
android:layout_marginBottom="20dip"
android:layout_height="match_parent"
android:text="column1"
android:layout_width="400dip"
android:layout_toRightOf="#+id/Thumbnail"/>
<TextView android:id="#+id/productlinerow3"
android:textColor="#000000"
android:textSize="25dip"
android:text="column2"
android:layout_marginTop="10dip"
android:layout_height="match_parent"
android:layout_width="300dip"
android:layout_below="#+id/productlinerow"
android:layout_toRightOf="#+id/Thumbnail"/>
<TextView
android:id="#+id/qtylabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/productlinerow"
android:layout_toLeftOf="#+id/SOQty"
android:layout_marginTop="40dip"
android:layout_marginRight="30dip"
android:inputType="number"
android:editable="true"
android:gravity="right"
android:text="Order Qty:"
android:textSize="25dip" />
<EditText
android:id="#+id/SOQty"
android:layout_width="100dip"
android:layout_height="60dip"
android:layout_marginTop="25dip"
android:layout_toLeftOf="#+id/SOCatLine"
android:layout_marginRight="30dip"
android:layout_below="#+id/productlinerow"
android:text="1"
android:maxLength="3"
android:inputType="number"
android:singleLine="true"
android:textSize="25dip"
android:imeOptions="flagNoExtractUi"
android:selectAllOnFocus="true"/>
<Button android:text="Add" android:background="#drawable/green_button"
android:id="#+id/SOCatLine"
android:textSize="25dip"
android:textColor="#FFFFFF"
android:layout_width="130dip"
android:layout_marginTop="30dip"
android:layout_height="wrap_content"
android:layout_below="#+id/productlinerow"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
You have to store the text of button in some kind of collection. As you can using HashMap here you can create one more Object of HashMap that will store the text of Button inside getView() using
bAdd.setText(((HashMap<String,String>) getItem(position)).get("btn_text"));
then you can setTag() the instance to use in onClick()
bAdd.setTag((HashMap<String,String>) getItem(position));
And then inside onClick() use getTag() to get the position of the Button.
#Override
public void onClick(View v) {
HashMap<String,String> map = (HashMap<String,String>)v.getTag();
map.put("btn_text", "Change");
((Button)v).setText(map.get("btn_text"));
}
How do you initialize your bAdd button...nevermind I see it now.
I would check your XML file, two of your buttons probably have the same id.