I have a dialog where I wish to force the user to fill in a name into an EditText, if it's empty I wish for pressing done to have no effect. However,
EditText x = (EditText) view.findViewById(R.id.name);
always returns null. I have tried searching but have only found the solution to add a view to findViewById(...) but I already have that. My guess it that it has to do with me creating my own version of the button.
This is the code for the Dialog.
package com.example.dialogruta;
#SuppressLint({ "NewApi", "ValidFragment" })
public class RecordDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(view).setPositiveButton("Save experiment", null); //Creating my own
final Dialog d = builder.create();
d.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = ((AlertDialog) d).getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText x = (EditText) view.findViewById(R.id.name);
if( x!= null && !x.toString().isEmpty()){
String xs = x.getText().toString();
d.dismiss();
}
else{
//TODO: Stuff
}
}
});
}
});
return d;
}
}
And this is the .xml-file
<?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" >
<EditText
android:id="#+id/name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Experiment name" />
<EditText
android:id="#+id/notes"
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_weight ="1"
android:lines="5"
android:minLines="1"
android:gravity="top|left"
android:maxLines="10"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Notes" />
</LinearLayout>
Try this..
Change View name
final View main_view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(main_view).setPositiveButton("Save experiment", null);
and use it
EditText x = (EditText) main_view.findViewById(R.id.name);
because in your public void onClick(View view) { onclick has view that edit text need to refer inflated view. Not onclick view.
Or
As like #Raghunandan said change onclick view name as public void onClick(View v) {
Change this
public void onClick(View view) {
to
public void onClick(View v) {
You have
final View view = inflater.inflate(R.layout.record_fragment, null);
and this
public void onClick(View view) {
Both View view.
What happens here
#Override
public void onClick(View view) {
EditText x = (EditText) view.findViewById(R.id.name);
EditText is not a child of button and view.findViewById looks for a view in the current view hierarchy. It should look for the view in the inflated layout final View view = inflater.inflate(R.layout.record_fragment, null);
change local view name in onClick() method.
public void onClick(View v)
Declare the variable as private at the onCreate method beginning and access there.
EditText x = (EditText) findViewById(R.id.name);
Now you will get the value of x in onClick() method.
Related
I have a problem about defining objects in Dialog screen via ButterKnife.
I can define a holder class to determie each object using in Dialog screen.
When I run the app, it throws an error shown below.
java.lang.NullPointerException: Attempt to read from field 'android.widget.Button com.example.dialog.AlertDialogActivity$ViewHolder.buttonUyeOl' on a null object reference
How can I connect each object via ButterKnifer to the Dialog Screen?
How can I fix it?
Here is my code shown below.
public class AlertDialogActivity extends AppCompatActivity {
#BindView(R.id.dialogAc)
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alert_dialog);
ButterKnife.bind(this);
openDialog();
}
private void openDialog() {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialogScreen();
}
});
}
private void openDialogScreen() {
LayoutInflater ınflater = this.getLayoutInflater();
View view = ınflater.inflate(R.layout.alert_dialog, null);
final ViewHolder holder;
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
holder = new ViewHolder(view);
view.setTag(holder);
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setView(view);
alert.setCancelable(false);
final AlertDialog dialog = alert.create();
holder.buttonUyeOl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), holder.mailEditText.getText().toString() + " "
+ holder.kadiEditText.getText().toString() + " " + holder.sifreEditText.getText().toString()
, Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
holder.buttonCik.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.show();
}
static final class ViewHolder {
#BindView((R.id.mailAdres))
EditText mailEditText;
#BindView(R.id.kullanici)
EditText kadiEditText;
#BindView(R.id.sifre)
EditText sifreEditText;
#BindView(R.id.buttonUyeOl)
Button buttonUyeOl;
#BindView(R.id.buttonExit)
Button buttonCik;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#324589"
android:orientation="vertical"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/kullanici"
android:hint="Kullanıcı İsmini Giriniz"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mailAdres"
android:hint="Mail Adresi Giriniz"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sifre"
android:inputType="textPassword"
android:hint="Şifre Giriniz"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Uye Ol"
android:id="#+id/buttonUyeOl"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="İptal Et"
android:id="#+id/buttonExit"
/>
</LinearLayout>
</LinearLayout>
My answer
private void openDialogScreen() {
LayoutInflater ınflater = this.getLayoutInflater();
View view = ınflater.inflate(R.layout.alert_dialog, null);
final ViewHolder holder = new ViewHolder(view);
....
}
My project now is how to make a Snackbar displayed at the bottom of the screen. The Snackbar contain with editText and three button for selection categories.
I use LayoutInflater and follow these code for my script, and it's work. My problem now is, how to get response when user click the button ?
Here my code
#Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
layout.addView(snackView, 0);
snackbar.show();
}
and here my snacbar.xml layout
<?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">
<EditText
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="#+id/event_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:hint="#string/type_your_message"
android:inputType="textNoSuggestions"
android:maxLines="4"
android:singleLine="false"
android:textSize="18sp"
android:paddingLeft="4dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_mapel"
android:onClick="mapel"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_image_edit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_image_edit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_image_edit" />
</LinearLayout>
here is the source required to implement button listener to your snackbar custom layout.
#Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
// button id for snackbar
Button button_mapel = (Button) snackView.findViewById(R.id.btn_mapel);
// perform button click listener
button_mapel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here perform your button click task
}
});
layout.addView(snackView, 0);
snackbar.show();
}
Follow this documentation
https://developer.android.com/training/snackbar/action.html
public class MyUndoListener implements View.OnClickListener{
&Override
public void onClick(View v) {
// Code to undo the user's last action
}
}
Now for setAction()
Snackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),
R.string.email_archived, Snackbar.LENGTH_SHORT);
mySnackbar.setAction(R.string.undo_string, new MyUndoListener());
mySnackbar.show();
You have to get id of your Button. And have to set clickListener.
You have to just replace your code with this.
I have put listener on btn_mapel, you can put any other button according to your requirement.
Try this.
#Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
Button mapelBt = (Button)snackView.findViewById(R.id.btn_mapel);
mapelBt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get your button click callback here
}
});
layout.addView(snackView, 0);
snackbar.show();
}
Try This,
Button btn_feed_back = (Button)snackView.findViewById(R.id.feedback);
btn_feed_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
i have a custom dialog box, it has a textview an edittext and 2 buttons. i'm trying to create a new textview that containes the text of the edittext everytime the user presses on the 'add' button,and locate it beneath the buttons. how can i do that? here's what i tried:
ingDlgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//showDialog(INGS_DIALOG);
final Dialog customIngsDlg = new Dialog(context);
customIngsDlg.setContentView(R.layout.custom_ings_dialog);
customIngsDlg.setTitle("Ingredients");
TextView ingsDlgTitle = (TextView)findViewById(R.id.ingsDialogTitleTv);
final EditText ingsEt = (EditText)customIngsDlg.findViewById(R.id.ingsDialogEt);
final Button ingsAddBtn = (Button)customIngsDlg.findViewById(R.id.ingsAddButton);
final Button ingsFinishBtn = (Button)findViewById(R.id.ingsFinishButton);
ingsAddBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ArrayList<String> ingsTmp = new ArrayList<String>();
String ingredient = ingsEt.getText().toString();
ingredients.add(ingredient);
ingsEt.setText("");
TextView ingItemTv = new TextView(customIngsDlg.getContext());
ingItemTv.setText(ingredient);
RelativeLayout layout = new RelativeLayout(OwnRecipeAdding.this);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.BELOW,ingsEt.getId());
layout.setLayoutParams(parms);
customIngsDlg.addContentView(ingItemTv,parms);
}
});
// ingsFinishBtn.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v) {
//
// customIngsDlg.dismiss();
// }
// });
customIngsDlg.show();
}
});
I would suggest adding the text view to your custom layout in the .xml file and then add text to that when the add button is pressed.
This needs a bit of styling, but it's functional. You'll likely need a scroll view in there when the list becomes too long.
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_fragment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/dialog_textview_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Add Ingredients" />
<EditText
android:id="#+id/dialog_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left" />
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button_add"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="#+id/button_finish"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish" />
</LinearLayout>
<TextView
android:id="#+id/dialog_textview_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="" />
</LinearLayout>
DialogFragment class:
public class TestDialogFragment extends DialogFragment {
private Button buttonAdd;
private Button buttonFinish;
private TextView addTextView;
private EditText editText;
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_fragment, null);
buttonAdd = (Button)dialoglayout.findViewById(R.id.button_add);
buttonFinish = (Button)dialoglayout.findViewById(R.id.button_finish);
editText = (EditText)dialoglayout.findViewById(R.id.dialog_edittext);
addTextView = (TextView)dialoglayout.findViewById(R.id.dialog_textview_2);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text = addTextView.getText().toString();
text = text + "\n" + editText.getText().toString();
addTextView.setText(text);
editText.setText("");
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout)
.setTitle("Ingredients")
.setCancelable(false)
.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
return dialog;
}
}
I have two buttons in ListView Header and I want to detect button is clicked from the header. how can I do this..
here is my code:
header_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b2" />
</LinearLayout>
in java code I have done this
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
You can add listener to these buttons.
btnB1 = (Button) mTop.findViewById(R.id.b1);
btnB1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
})
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
Button _btnb1 = (Button) mTop.findViewById(R.id.b1);
_btnb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
Button _btnb2 = (Button) mTop.findViewById(R.id.b2);
_btnb2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
Try this code
I have:
popUp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
....
<Button
android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:gravity="left"
/>
</LinearLayout>
main.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
popUp();
}
});
and I get force close here
public void popUp(){
LayoutInflater inflater = (LayoutInflater) project.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup,null, false),300,400,true);
ok_button = (Button) findViewById(R.id.ok_button);
pw.showAtLocation(findViewById(R.id.list), Gravity.BOTTOM, 0,10);
ok_button.setOnClickListener(new OnClickListener() { //here I get FC
#Override
public void onClick(View v) {
pw.dismiss();
}
});
}
}
When I use button (in popUp();) from main.xml instead of popUp.xml everything is working.
What is wrong with using the button from not main.xml
I think that the problem is here:
ok_button = (Button) findViewById(R.id.ok_button);
it is looking for the button view inside the main layout instead of looking inside the popup layout. So probably ok_button is null.
Try to move out the inflate call from the constructor, like this:
View v = inflater.inflate(R.layout.popup,null, false);
final PopupWindow pw = new PopupWindow(v,300,400,true);
and then:
ok_button = (Button) v.findViewById(R.id.ok_button);