I have a ListView and a button i want when I press the button it takes me to a Dialogue it has a Text Field and another Button when I fill the text and press the button it will be saved in the list view ( I know how to add items from the same activity to the ListView) but I don't how to add items from another activity or AlterDialoge .
public class Main3Activity extends AppCompatActivity {
EditText et;
ListView lv;
Button bt;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
et = (EditText) findViewById(R.id.TextAdd);
bt = (Button) findViewById(R.id.btnadd);
lv = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<String>();
adapter= new ArrayAdapter<String>(Main3Activity.this,android.R.layout.simple_list_item_1,arrayList);
lv.setAdapter(adapter);
Button mShowDialog = (Button) findViewById(R.id.btnSave);
mShowDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Main3Activity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_add,null);
final EditText mUser = (EditText) mView.findViewById(R.id.TextAdd);
Button mAdd = (Button) mView.findViewById(R.id.btnadd);
mAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!mUser.getText().toString().isEmpty()) {
String result = et.getText().toString();
arrayList.add(result);
adapter.notifyDataSetChanged();
Toast.makeText(Main3Activity.this, "Success", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Main3Activity.this, "Error pls Write", Toast.LENGTH_SHORT).show();
}
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="#+id/Add"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="25sp"
android:text="POP UP Window" />
<EditText
android:id="#+id/TextAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="10dp"
android:inputType="textPersonName"
android:hint="write"/>
<Button
android:id="#+id/btnadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:textColor="#android:color/white"
android:text="Add" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jim_a.testapp.Main3Activity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="#+id/btnSave"
android:layout_height="73dp"
android:text="Open Window"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1" />
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="13dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="13dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="33dp"
app:layout_constraintTop_toBottomOf="#+id/btnSave"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="31dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"></ListView>
</android.support.constraint.ConstraintLayout>
Okay so I made 2 minor modifications to get your code working -
Replace "et" with "mUser" to avoid Null Pointer Exception when calling getText()
Dismiss Dialog upon successful updation of list.
Below is the onClick() method body :
AlertDialog.Builder mBuilder = new AlertDialog.Builder(Main3Activity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_add, null);
final EditText editText_mUser = (EditText) mView.findViewById(R.id.TextAdd);
Button mAdd = (Button) mView.findViewById(R.id.btnadd);
mBuilder.setView(mView);
//create dialog instance here, so that it can be dismissed from within the OnClickListener callback
final AlertDialog dialog = mBuilder.create();
mAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!editText_mUser.getText().toString().isEmpty()) {
// Instead of et.getText(), call mUser.getText()
String result = editText_mUser.getText().toString();
arrayList.add(result);
adapter.notifyDataSetChanged();
Toast.makeText(Main3Activity.this, "Success", Toast.LENGTH_SHORT).show();
//dismiss dialog once item is added successfully
dialog.dismiss();
} else {
Toast.makeText(Main3Activity.this, "Error pls Write", Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
See this for more info.
For other cases, where you want communication between 2 Activities/Fragments for Event-Based List Updation, you can check out EventBus.
Related
I have a custom dialog in which I include another layout. In this layout there are 3 buttons and a TextView none of these is null. If I click the buttons they work correctly. But the TextView doesn't show any text. The text should appears when I click another button. That's how it should works
Custom dialog layout:
<LinearLayout
............
.....
<androidx.cardview.widget.CardView
android:id="#+id/result_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="2dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:visibility="gone"
app:cardCornerRadius="8dp">
<include android:id="#+id/result_layout" layout="#layout/result_block" />
</androidx.cardview.widget.CardView>
....
....
</LinearLayout>
The result_block layout
<?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="wrap_content"
android:padding="16dp"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/result_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text=""
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"
android:padding="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_copy_24px_outlined"
android:layout_marginEnd="16dp"
android:tint="#color/colorWhite"
android:background="?attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:tint="#color/colorWhite"
android:src="#drawable/ic_share_24px_outlined"
android:background="?attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#color/colorWhite"
android:src="#drawable/ic_volume_up_24px_outlined"
android:background="?attr/selectableItemBackgroundBorderless"
/>
</LinearLayout>
</LinearLayout>
And now the dialog
private void showDiag() {
final View dialogView = View.inflate(getActivity(), R.layout.custom_dialog_layout,null);
final View resultLayout = View.inflate(getActivity(), R.layout.result_block,null);
final Dialog dialog = new Dialog(getActivity(), R.style.MyAlertDialogStyle);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(dialogView);
final TextView resultTxt = resultLayout.findViewById(R.id.result_txt);
final ImageButton btn1 = resultLayout.findViewById(R.id.btn1);
final CardView resultCardView = dialog.findViewById(R.id.result_card_view);
final TextInputEditText editText = dialog.findViewById(R.id.text_edit);
final ImageButton clearText = dialog.findViewById(R.id.clear_text);
MaterialButton resultConfirm = dialog.findViewById(R.id.result_confirm);
ImageButton btn2 = dialogView.findViewById(R.id.copy_btn);
ImageButton btn3 = dialogView.findViewById(R.id.share_btn);
resultConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText.getText().toString().equals("")) {
String theWord = editText.getText().toString();
String result = Utils.getSecondWord(theWord);
// result it shows me the correct string with no errors or something else
resultTxt.setText(result); // doesn't work. Not set the text
insertWord(theWord, result);
resultCardView.setVisibility(View.VISIBLE);
resultCardView.setVisibility(View.VISIBLE);
} else {
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
// Share btn
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Utils.revealShow(dialogView, true, null, resultConfirm);
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (i == KeyEvent.KEYCODE_BACK){
Utils.revealShow(dialogView, false, dialog, resultConfirm);
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
Everything inside the dialog works correctly but the TextView not. Even if I try to write something else like resultTxt.setText("Hello there"); the text not appears. Any suggestions?
Please you remove the android:text=""in TextView because textview is get default text is empty or you write anything in TextView like android:text="abc"
All I want to do is start a new intent on click of a button. Here is my code(I have removed irrelevant parts):
activity_login.xml:
<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:gravity="center_horizontal"
tools:context="com.example.android.altro.LoginActivity">
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:textColor="#color/colorWhite"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/email_sign_up_button"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_button"
android:onClick="registerNewUser"
android:text="#string/action_sign_up"
android:textAllCaps="false" />
</LinearLayout>
</ScrollView>
</LinearLayout>
On clicking #+id/email_sign_up_button registerNewUser is called. I have defined this fuction in LoginActivity.java:
public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {
/**
* Id to identity READ_CONTACTS permission request.
*/
private static final int REQUEST_READ_CONTACTS = 0;
/**
* A dummy authentication store containing known user names and passwords.
* TODO: remove after connecting to a real authentication system.
*/
private static final String[] DUMMY_CREDENTIALS = new String[]{
"foo#example.com:hello", "bar#example.com:world"
};
/**
* Keep track of the login task to ensure we can cancel it if requested.
*/
private UserLoginTask mAuthTask = null;
// UI references.
private AutoCompleteTextView mEmailView;
private EditText mPasswordView;
private View mProgressView;
private View mLoginFormView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Set up the login form.
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
populateAutoComplete();
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
mEmailSignInButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
attemptLogin();
}
});
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
/* Button mRegisterButton = (Button) findViewById(R.id.email_sign_up_button);
mRegisterButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), RegisterActivity.class);
startActivity(intent);
}
});*/
}
public void registerNewUser(View view) {
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
}
I also tried doing it by finding view and then creating an intent (the code is commented out in onCreate() method. That also gave me the same error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.altro/com.example.android.altro.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
You have only one Button with id email_sign_up_button
Please check your layout. Also try replacing
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
With
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_up_button);
I want to get Values from EditText dynamically.. I have a lot of EditText generated when user press add button..When User presses add Button it generates 3 Edittext each time. I dont know how to get the values from this dynamically generated EdiTtext . Now My question is how can i get the values from The 3 Edittext on each row. ALso I need to verify that if user removed the view or not. Please help I am new android development.This should happen when a user presses on Save Button. Thanks in advance!
This is class .
public class SecondActivity extends Activity {
Button saveBtn,cancelBtn,addBtn;
RelativeLayout layout;
EditText third,first,second;
LinearLayout Container;
int counter=0;
int all=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
saveBtn=(Button) findViewById(R.id.save);
cancelBtn=(Button) findViewById(R.id.cancel);
Container=(LinearLayout) findViewById(R.id.container);
addBtn=(Button) findViewById(R.id.addBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
Toast.makeText(SecondActivity.this, "The Result: "+all,Toast.LENGTH_LONG).show();
finish();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
// EditText ed1=(EditText) findViewById(R.id.editText1);
// EditText ed2=(EditText) findViewById(R.id.editText2);
// EditText ed3=(EditText) findViewById(R.id.editText3);
// all=all+Integer.parseInt(ed1.getText().toString())+Integer.parseInt(ed2.getText().toString())+Integer.parseInt(ed3.getText().toString());
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Container.addView(addView);
}});
}
}
This is my row.xml .which i am using as a view in java code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_margin="10dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Field 1" />
<EditText
android:id="#+id/editText1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:inputType="numberPassword" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:inputType="numberPassword" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:text="Remove" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignLeft="#+id/editText2"
android:text="Field 2" />
<EditText
android:id="#+id/editText3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_toRightOf="#+id/editText2"
android:editable="false"
tools:ignore="Deprecated" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_alignLeft="#+id/editText3"
android:text="Field 3" />
</RelativeLayout>
This is my layout which is attached with my activity class.
<?xml version="1.0" encoding="utf-8"?>
<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:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".SecondActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Add" />
</RelativeLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="300dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/save"
android:layout_width="146dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Save" />
<Button
android:id="#+id/cancel"
android:layout_width="146dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Cancel" />
<EditText
android:id="#+id/editText1"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_above="#+id/save"
android:layout_alignParentLeft="true"
android:editable="false"
android:hint="T 1"
tools:ignore="Deprecated" >
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:editable="false"
android:hint="T 2"
tools:ignore="Deprecated" >
</EditText>
<EditText
android:id="#+id/editText3"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_toRightOf="#+id/editText2"
android:editable="false"
android:hint="T 3"
tools:ignore="Deprecated" />
<EditText
android:id="#+id/editText4"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText3"
android:layout_alignParentRight="true"
android:editable="false"
android:hint="T 4"
tools:ignore="Deprecated,HardcodedText" />
</RelativeLayout>
</LinearLayout>
This is your edited SecondActivity.java
public class SecondActivity extends Activity {
Button saveBtn, cancelBtn, addBtn;
RelativeLayout layout;
EditText third, first, second;
LinearLayout Container;
int counter = 0;
int all = 0;
int tag = 0;
ArrayList<Integer> dynamicLayoutsTags;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
saveBtn = (Button) findViewById(R.id.save);
cancelBtn = (Button) findViewById(R.id.cancel);
Container = (LinearLayout) findViewById(R.id.container);
addBtn = (Button) findViewById(R.id.addBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dynamicLayoutsTags.size() > 0) {
for (int i = 0; i < dynamicLayoutsTags.size(); i++) {
View getView = Container
.findViewWithTag(dynamicLayoutsTags.get(i));
EditText editText1 = (EditText) getView
.findViewById(R.id.editText1);
EditText editText2 = (EditText) getView
.findViewById(R.id.editText2);
EditText editText3 = (EditText) getView
.findViewById(R.id.editText3);
Toast.makeText(
SecondActivity.this,
"Row " + i + " : " + "editext 1 is : "
+ editText1.getText()
+ " editext 2 is : "
+ editText2.getText()
+ " editext 3 is : "
+ editText3.getText(),
Toast.LENGTH_LONG).show();
}
}
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
Toast.makeText(SecondActivity.this, "The Result: " + all,
Toast.LENGTH_LONG).show();
finish();
}
});
cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Button buttonRemove = (Button) addView
.findViewById(R.id.remove);
addView.setTag(tag);
buttonRemove.setTag(tag);
dynamicLayoutsTags.add(tag);
Container.addView(addView);
tag++;
buttonRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ((LinearLayout) addView.getParent())
// .removeView(addView);
Integer removeTag = (Integer) v.getTag();
View deleteView = Container.findViewWithTag(removeTag);
Container.removeView(deleteView);
dynamicLayoutsTags.remove(removeTag);
}
});
}
});
}
#Override
protected void onResume() {
super.onResume();
tag = 0;
dynamicLayoutsTags = new ArrayList<Integer>();
}
}
I'm trying to click on row in list view but my code only works when i pressed between two rows
you can see this :
http://www.screencast.com/t/2vei8yOQh
note in rep.xml i using table layout and inside it rowTable
i uses this code :
public class LastDaysActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_days);
final Button newFourm = (Button) findViewById(R.id.button1);
LastDaysAdapter adapter = new LastDaysAdapter(this, getDates(),
getApplicationContext(), getBusID());
setListAdapter(adapter);
newFourm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(LastDaysActivity.this,
TabHostActivity.class);
startActivity(i);
}
});
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView myView = (TextView) v.findViewById(R.id.textView2);
String text = myView.getText().toString();
Toast.makeText(this, text + " selected", Toast.LENGTH_LONG).show();
}
edit xml :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/TableRow05"
android:layout_width="fill_parent"
android:layout_height="27dp"
android:layout_marginTop="4dp"
android:background="#drawable/trans01" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="رقم اللوحة" />
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="التاريخ"
android:textSize="12sp" />
</TableRow>
</TableLayout>
I have a dialog that I launch with:
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
The problem is that when I click the button it does not dismiss the dialog.
My dialog xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tastePickTitle"
android:text="Select a Taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/taste_array"
/>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="#+id/ammountPickTitle"
android:text="How much taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/ammount_array"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1"
/>
<Button
android:id="#+id/addTasteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_weight="1"
android:onClick="addTasteNow" />
</LinearLayout>
</LinearLayout>
Here is the full taste tag java code to give the snipet above some more context:
public class TasteTags extends Activity {
BeerData e;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tastetag_page);
//get beer data
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);
beerTitle.setText(e.beerName + " Taste Profile");
String url = "myURL";
url = url + "b=" +e.beerId;
//async task to get beer taste tag percents
new GetTasteJSON(this).execute(url);
}
public void addTaste(View v){
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("dialog", "cancel pressed");
dialog.dismiss();
}
});
dialog.show();
}
}
make final Dialog dialog = null; a global variable (activity level), remove the final and initialize at it where you have it right now, that is fine.
Looking at your code nothing jumps out, if you post your full code or a simple example to replicate i'm sure someone could fully decipher the problem. I have included some sample snippets that show a dialog that work, take a look at those to see if they shed any insight as to why yours does not work.
One thing to check before looking at the working sample, make sure you don't have a button in your dialog view with id 'cancelButton' defined twice(common from copy/paste errors). This will cause the behavior you are seeing. If you take the working example below and use the following view it will reproduce your behavior:
Broken Dialog View
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
See below for working example.
Dialog View:
<RelativeLayout 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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="close" />
Application class
package com.example.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Add Taste");
Button dialogButton = (Button) dialog.findViewById(R.id.button1);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}