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"
Related
I have an app which has Dialogbox with 3 Radios and RadioGroup,
So if the Light Radio is checked, after clicking Okay, the theme of the app will be changed to light theme.
If Dark is checked, after clicking Okay, the theme will be changed to night themeand if system it will be changed to system.
Toolbar:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 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:background="#FFF"
style="#style/TextAppearance.AppCompat.Widget.Button.Borderless.Colored"
android:elevation="0dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/chooseTheme"
android:onClick="chooseTheme"
android:title="Choose Theme"
app:showAsAction="never"
tools:ignore="HardcodedText" />
</menu>
DialogBox:
<?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:gravity="center"
android:orientation="vertical">
<RadioGroup
android:id="#+id/themeGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="5dp"
android:gravity="center"
android:paddingStart="20dp"
android:paddingTop="20dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
tools:ignore="UselessParent">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginBottom="20dp"
android:text="Choose Theme"
android:textColor="#color/black"
android:textColorHint="#FFFFFF"
android:textSize="20sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="#+id/radioLight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:buttonTint="#color/colorPrimary"
android:checked="true"
android:text="Light"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="#+id/radioDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:buttonTint="#color/colorPrimary"
android:text="Dark"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="#+id/radioSystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginBottom="10dp"
android:buttonTint="#color/colorPrimary"
android:text="System"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginStart="80dp"
android:orientation="horizontal"
tools:ignore="RtlHardcoded">
<Button
android:id="#+id/btn_cancel"
style="?android:attr/borderlessButtonStyle"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center|center_vertical|fill_vertical"
android:scaleY="0.9"
android:text="Cancel"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
tools:ignore="ButtonStyle,HardcodedText" />
<Button
android:id="#+id/btn_okay"
style="?android:attr/borderlessButtonStyle"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginStart="10dp"
android:gravity="center|center_vertical|fill_vertical"
android:scaleY="0.9"
android:text="Okay"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
tools:ignore="ButtonStyle,HardcodedText" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
MainActivity:
public void chooseTheme(MenuItem item) {
final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_theme,null);
Button btn_cancel = mView.findViewById(R.id.btn_cancel);
Button btn_okay = mView.findViewById(R.id.btn_okay);
alert.setView(mView);
final AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(false);
RadioButton radioLight = findViewById(R.id.radioLight);
final RadioButton radioDark =findViewById(R.id.radioDark);
RadioButton radioSystem =findViewById(R.id.radioSystem);
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
btn_okay.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
I tried everything I knew, not I have no idea how to do it.
Thank you for attention!
The following will check which radio button is checked inside RadioGroup:
RadioGroup radioGroup = mView.findViewById(R.id.themeGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch(i) {
case R.id.radioLight:
setLightTheme();
Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
break;
case R.id.radioDark:
setDarkTheme();
Toast.makeText(getApplicationContext(),"Dark mode",Toast.LENGTH_LONG).show();
break;
}
}
});
I found some points in your code.
for first, I think you have to find your radio buttons from your view too.
like this:
RadioButton radioLight = mView.findViewById(R.id.radioLight);
final RadioButton radioDark = mView.findViewById(R.id.radioDark);
RadioButton radioSystem = mView.findViewById(R.id.radioSystem);
And for the second I think you can use SharedPreferences to find what the user has checked, before calling dialog.dimiss.
That's what was I trying to achieve
Boolean night = false;
public void chooseTheme(MenuItem item) {
final AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
final View mView = getLayoutInflater().inflate(R.layout.dialog_theme,null);
Button btn_okay = mView.findViewById(R.id.btn_okay);
Button btn_cancel = mView.findViewById(R.id.btn_cancel);
alert.setView(mView);
final AlertDialog alertDialog = alert.create();
alertDialog.setCanceledOnTouchOutside(false);
final RadioGroup themeGroup = mView.findViewById(R.id.themeGroup);
themeGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#SuppressLint("NonConstantResourceId")
#Override
public void onCheckedChanged(RadioGroup themeGroup, int i) {
switch(i) {
case R.id.radioLight:
night = false;
break;
case R.id.radioDark:
night = true;
break;
}
}
});
btn_okay.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
if(night){
sharedpref.setNightModeState(true);
Toast.makeText(getApplicationContext(),"Dark mode", Toast.LENGTH_LONG).show();
}
else if (!night){
sharedpref.setNightModeState(false);
Toast.makeText(getApplicationContext(),"Light mode",Toast.LENGTH_LONG).show();
}
alertDialog.dismiss();
restartApp();
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
#SuppressLint("SetTextI18n")
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
SharedPref:
public class SharedPref {
SharedPreferences mySharedPref ;
public SharedPref(Context context) {
mySharedPref = context.getSharedPreferences("filename",Context.MODE_PRIVATE);
}
// this method will save the nightMode State : True or False
public void setNightModeState(Boolean state) {
SharedPreferences.Editor editor = mySharedPref.edit();
editor.putBoolean("NightMode",state);
editor.apply();
}
// this method will load the Night Mode State
public Boolean loadNightModeState (){
Boolean state = mySharedPref.getBoolean("NightMode",false);
return state;
}
}
"I was trying to create Toast message when user clicks on Description TextView and Like ImageButton. But the list_item is not responding to touch Events "
"I went through many other people answering about changing focus.But none of them are working"
EventsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final EventsObject mEventsObject = mEventsAdapter.getItem(position);
final String mEventUrl = mEventsObject.geteLink();
Log.e(TAG, "Inside ListVIew");
final boolean status = mEventsObject.hasLiked();
//LikeButton likeButton = view.findViewById(R.id.heart_button);
TextView description = view.findViewById(R.id.eventDesc);
//final TextView likesCountTextView = view.findViewById(R.id.likesCount);
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
description.setFocusable(false);
description.setFocusableInTouchMode(false);
description.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEventUrl)) {
Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
openLinkInBrowser.setData(Uri.parse(mEventUrl));
startActivity(openLinkInBrowser);
} else {
Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
}
}
});
Button loveBtn = view.findViewById(R.id.loveButton);
loveBtn.setFocusable(false);
loveBtn.setFocusableInTouchMode(false);
loveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (status) {
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(true);
} else {
Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(false);
}
}
});
}
XML for list_item is
?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_margin="8dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<TextView
android:id="#+id/organiser"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:paddingLeft="16dp"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold"
tools:text="Organiser" />
<TextView
android:id="#+id/dateOfEvent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textColor="#ffffff"
android:textStyle="bold"
tools:text="12/03/20" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/organiserImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/events_circle"
android:padding="16dp"
android:src="#mipmap/ic_launcher"
android:textColor="#ffffff" />
<TextView
android:id="#+id/eventDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:gravity="fill"
android:textColor="#ffffff"
android:textSize="16sp"
tools:text="#string/test_event_desc" />
</LinearLayout>
<ImageButton
android:id="#+id/loveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:background="#drawable/events_love"
android:scaleType="center"
android:src="#drawable/love" />
<!--
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp">
<com.like.LikeButton
app:icon_type="heart"
app:icon_size="18dp"
android:id="#+id/heart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:circle_start_color="#ff2134"
app:circle_end_color="#000000"
/>
-->
<!--<TextView
android:id="#+id/likesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
tools:text="10"
android:textColor="#ffffff"/>
</LinearLayout>-->
</LinearLayout>
"I would like to have list_item's responding to click events and showing toast message..
Please Help..
THanks in Advance !!"
It may be because you are trying to perform click inside item click listener of ListView.
You can fix it by creating a custom adapter for listview. Customer ListView Adapter
After creating this custom adapter you can get the reference of description and loveBtn and perform click operation on this.
Your adapter's getView() code will be like this-
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView=layoutInflater.inflate(R.layout.list_row, null);
TextView description=convertView.findViewById(R.id.eventDesc);
Button loveBtn=convertView.findViewById(R.id.loveButton);
}
description.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEventUrl)) {
Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
openLinkInBrowser.setData(Uri.parse(mEventUrl));
startActivity(openLinkInBrowser);
} else {
Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
}
}
});
loveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (status) {
Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(true);
} else {
Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
mEventsObject.setHeartLiked(false);
}
}
});
return convertView;
}
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.
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>