I am trying to display two RecyclerViews inside alert dialog contents. Please Help what am I doing wrong?
The onCreateViewHolder function is not being called
Please note that the RecyclerViews are inside the ConstraintLayout in the xml. If that is causing them not to display what should be the correct procedure?
public class MainActivity extends AppCompatActivity {
private View inflatedView;
private RecyclerView fontList,sizeList;
private String[] fonts = {"normal","sans","serif","monospace"};
private String[] sizes = {"14","16","18","20","22","24"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);
builder.setView(inflatedView).setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
fontList = inflatedView.findViewById(R.id.fontlist);
sizeList = inflatedView.findViewById(R.id.sizelist);
fontList.setLayoutManager(new LinearLayoutManager(this));
fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));
sizeList.setLayoutManager(new LinearLayoutManager(this));
sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private List<String> itemList;
public MyAdapter(List<String> itemList) {
this.itemList = itemList;
Log.d("lol","MyAdapter");
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.design_fontcell,viewGroup);
Log.d("lol","onCreateViewHolder");
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.mtextView.setText(itemList.get(i));
Log.d("lol",itemList.get(i));
}
#Override
public int getItemCount() {
Log.d("lol","getItemCount: "+itemList.size());
return itemList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mtextView;
MyViewHolder(View cellView){
super(cellView);
mtextView = cellView.findViewById(R.id.textView_font);
}
}
}
}
Change your code like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);
//No need of setView() for buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
}
});
fontList = inflatedView.findViewById(R.id.fontlist);
sizeList = inflatedView.findViewById(R.id.sizelist);
fontList.setLayoutManager(new LinearLayoutManager(this));
fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));
sizeList.setLayoutManager(new LinearLayoutManager(this));
sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));
builder.setView(inflatedView); //Set the view at the end
AlertDialog dialog = builder.create();
dialog.show();
Related
I'm trying get item selected from user to fragment formulary on my Android app, following the google documentation this is possible using this methods from the library, i tried implement this methods and your Interface in my DialogFragment and get it on my fragment formulary, but, the error is returned when i click on the button necessary to open Dialog Fragment.
This is my Dialog Fragment class:
public class FiltroOpcao extends DialogFragment {
OnFiltroEscolhido listener;
private final String[] filtroAnuncio = getResources().getStringArray(R.array.filtro_array);
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
listener = (OnFiltroEscolhido) getTargetFragment();
}catch(ClassCastException e){
throw new ClassCastException(context.toString()+"Deve ser implementado");
}
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Filtrar por:")
.setItems(R.array.filtro_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.pegarFiltro(filtroAnuncio[which]);
}
});
return builder.create();
}
public interface OnFiltroEscolhido{
void pegarFiltro (String escolha);
}
}
And it is where i called the DialogFragment and the crash happens on my VendaFragment fragment class
public void onClick(View v) {
FiltroOpcao filtroOpcao = new FiltroOpcao();
filtroOpcao.setTargetFragment(VendaFragment.this, 1);
filtroOpcao.show(VendaFragment.this.getChildFragmentManager(), "FiltroOpcao");
}
private final String[] filtroAnuncio = getResources().getStringArray(R.array.filtro_array);
Probably, getResources() is the problem because you are using it before the fragment was attached.
Try to move the initialization of filtroAnuncio to onCreateDialog()
private String[] filtroAnuncio;
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
filtroAnuncio = getResources().getStringArray(R.array.filtro_array);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Filtrar por:")
.setItems(R.array.filtro_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.pegarFiltro(filtroAnuncio[which]);
}
});
return builder.create();
}
I want one word of the text in my Alert Dialog to be clickable and open an activity on click.
I tried to use a ClickableSpan in a SpannableString, but the click is not recognized at all...
public class TestDialog extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String text = "This is a test message.";
SpannableString ss = new SpannableString(text);
ClickableSpan cs = new ClickableSpan() {
#Override
public void onClick(#NonNull View view) {
System.out.println("Test");
}
};
ss.setSpan(cs, 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(ss)
.setTitle("Test Title")
.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
})
.setNegativeButton("No!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
});
return builder.create();
}
}
Thanks for your help!
Ok, I did it with the help of several other posts here and youtube videos.
I use a Custom Layout showing text combined with clickable spans which I then show in the Alert Dialog:
public class TestDialog extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String text = "This is a test message.";
SpannableString ss = new SpannableString(text);
ClickableSpan cs = new ClickableSpan() {
#Override
public void onClick(#NonNull View view) {
Intent testIntent = new Intent(getContext(), Test.class);
getContext().startActivity(testIntent);
}
};
ss.setSpan(cs, 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.test_view, null);
TextView message = (TextView) view.findViewById(R.id.textView);
message.setMovementMethod(LinkMovementMethod.getInstance());
message.setText(ss);
builder.setView(view)
.setTitle("Test Title")
.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
})
.setNegativeButton("No!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
});
return builder.create();
}
}
I hope I could also help you.
First, Make your text HTML links clickable by using Html.fromHtml:
.setMessage(Html.fromHtml(text))
and, Add this line to your Activity in Manifest:
<data android:host="my.app" android:scheme="http"></data>
Then, http://my.app should launch your activity.
In my application I want use custom dialog into DialogFragment . I can use AlertDialog into DialogFragment but I want use custom dialog instead of AlertDialog.
I write below codes into DialogFragment :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = LayoutInflater.from(mActivity).inflate(R.layout.fragment_dialog_comment, null);
initVar();
initView(rootView);
initFunctionality();
return new AlertDialog.Builder(mActivity)
.setView(rootView)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
authorName = edtAuthorName.getText().toString().trim();
authorEmail = edtAuthorEmail.getText().toString().trim();
authorComment = edtAuthorComment.getText().toString().trim();
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_EMAIL, authorEmail);
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_NAME, authorName);
if (commentId == AppConstant.THIS_IS_COMMENT) {
sendComment(authorName, authorEmail, authorComment);
} else {
sendReply(authorName, authorEmail, authorComment);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (dialogInterface != null) {
dialogInterface.dismiss();
}
}
})
.create();
}
I want use below code instead of above AlertDialog :
final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.fragment_dialog_comment);
edtAuthorName = dialog.findViewById(R.id.edt_author_name);
edtAuthorEmail = dialog.findViewById(R.id.edt_author_email);
edtAuthorComment = dialog.findViewById(R.id.edt_author_comment);
dialogComment_okBtn = dialog.findViewById(R.id.dialogComment_okBtn);
dialogComment_cancelBtn = dialog.findViewById(R.id.dialogComment_cancelBtn);
edtAuthorName.setText(AppPreference.getInstance(mActivity).getString(PrefKey.KEY_NAME));
dialogComment_cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (dialog != null) {
dialog.dismiss();
}
}
});
dialogComment_okBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
authorName = edtAuthorName.getText().toString().trim();
authorEmail = edtAuthorEmail.getText().toString().trim();
authorComment = edtAuthorComment.getText().toString().trim();
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_EMAIL, "noEmail#gmail.com");
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_NAME, authorName);
if (commentId == AppConstant.THIS_IS_COMMENT) {
sendComment(authorName, "noEmail#gmail.com", authorComment);
} else {
sendReply(authorName, "noEmail#gmail.com", authorComment);
}
dialog.dismiss();
}
});
dialog.show();
How can I it? Please don't give me negative points and please help me
You can extend your class with DialogFragment and rest will be like fragments like
public class YOurDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
//If don't want toolbar
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.customLayout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
I have this error:
The specified child already has a parent. You must call removeView()
on the child's parent first.
When I clicked buildNot.setPosiviteButton.
Help me please, thanks guys!
This is my Java source code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDersEkle = (Button) findViewById(R.id.btnDersEkle);
list = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
etDers = new EditText(MainActivity.this);
etNot = new EditText(MainActivity.this);
//Dialog
AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
build.setTitle("Ders Ekle");
build.setView(etDers);
//Dialog Not
final AlertDialog.Builder buildNot = new AlertDialog.Builder(MainActivity.this);
buildNot.setTitle("Not Ekle");
buildNot.setView(etNot);
build.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
adapter.add(etDers.getText().toString());
dialog.cancel();
}
});
final AlertDialog alertDers = build.create();
buildNot.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
adapter = (ArrayAdapter ) list.getAdapter();
String item = (String) list.getSelectedItem();
int position = list.getSelectedItemPosition();
item += "YourText";
adapter.insert(item, position);
dialog.cancel();
}
});
final AlertDialog alertNot = buildNot.create();
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
alertNot.show();
}
});
list.setAdapter(adapter);
btnDersEkle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDers.show();
}
});
}
That is because nothing is selected.
list.getSelectedItemPosition() will always return -1.
"Click" and "select" are separate things. "Select" in a ListView is done via the pointing device (D-pad, trackball, arrow keys, etc.).
list.getSelectedItemPosition() will always return -1 if you are not using D-pad, trackball or arrow keys.
You should store the position in onItemClick(AdapterView<?> parent, View view, int position, long id) as a class member and access it in buildNot.setPositiveButton onClick.
Coding below, where mPosition is a class member of your class.
buildNot.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
adapter = (ArrayAdapter ) list.getAdapter();
String item = (String) adapter.getItem(mPosition);
item += "YourText";
adapter.insert(item, mPosition);
dialog.cancel();
}
});
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mPosition = position;
alertNot.show();
}
});
Try change your code like below, maybe help
public class MainActivity extends ListActivity {
int posmy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainlistnew);
Button btnDersEkle = (Button) findViewById(R.id.btnDersEkle);
final ListView list = getListView();
final ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1);
final EditText etDers = new EditText(MainActivity.this);
final EditText etNot = new EditText(MainActivity.this);
//Dialog
AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
build.setTitle("Ders Ekle");
build.setView(etDers);
//Dialog Not
final AlertDialog.Builder buildNot = new AlertDialog.Builder(MainActivity.this);
buildNot.setTitle("Not Ekle");
buildNot.setView(etNot);
build.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
adapter.add(etDers.getText().toString());
dialog.cancel();
}
});
final AlertDialog alertDers = build.create();
buildNot.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//adapter = (ArrayAdapter ) list.getAdapter();
//String item = (String) list.getSelectedItem();
//int position = list.getSelectedItemPosition();
int position=posmy;
String item = "YourText" + etNot.getText().toString();
adapter.insert(item, position);
dialog.cancel();
}
});
final AlertDialog alertNot = buildNot.create();
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
posmy=position;
alertNot.show();
}
});
list.setAdapter(adapter);
btnDersEkle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
alertDers.show();
}
});
}
}
So this is my Dialog class:
public class SecondActivity extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.second, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}
}
In my dialog there are 2 edit texts which get 2 strings. I want to use those 2 strings in my MainActivity if the user presses on the save button. How do I do that?
The inflate method returns a view where you can execute a findViewById, like the following:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.second, null);
EditView editView = (EditView)v.findById(R.id.your_edit_view);
builder.setView(v)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}