Scroll View not working in Alert Dialog - android

Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>

You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you

class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");

Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});

Related

call another java activity from fragment class

Hey i have a simple question. i have a mobile_navigation that contain code to call a fragment class, and i have the fragment class but i want to call / pass to another java class. for example i have Inputdata.xml that when tap will go to fragment class nah i want when i tap the input xml it will instantly go to another java class not stuck in fragment class
here is the example code for mobile_navigation
<fragment
android:id="#+id/nav_pantuan"
android:name="com.joshua.r0th.crud2.ui.gallery.GalleryFragment"
android:label="#string/pantauan"
tools:layout="#layout/fragment_pantauan" />
and here is the GalleryFragment code
public class GalleryFragment extends Fragment {
private GalleryViewModel GalleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
GalleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_pantauan, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
GalleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
and this is the another java class that i willing to reach
public class pantauan extends AppCompatActivity {
database1 myDb;
EditText editNomorRumah,editJentikDalam,editJentikLuar;
Button btnAddData;
Button btnViewAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pantauan);
myDb = new database1(this);
editNomorRumah = (EditText)findViewById(R.id.nomorrmh);
editJentikDalam = (EditText)findViewById(R.id.jentikdirumah);
editJentikLuar = (EditText)findViewById(R.id.jentikdiluarrumah);
btnAddData = (Button)findViewById(R.id.tambahdata);
btnViewAll = (Button)findViewById(R.id.lihatdata);
AddData();
viewAll();
}
//fungsi tambah
public void AddData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editNomorRumah.getText().toString(),
editJentikDalam.getText().toString(),
editJentikLuar.getText().toString() );
if(isInserted == true)
Toast.makeText(pantauan.this,"Data Iserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(pantauan.this,"Data Not Iserted",Toast.LENGTH_LONG).show();
}
}
);
}
//fungsi menampilkan data
public void viewAll() {
btnViewAll.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 0) {
// show message
showMessage("Error","Noting Found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext() ) {
buffer.append("NomorRumah :"+ res.getString(0)+"\n");
buffer.append("JentikDalam :"+ res.getString(1)+"\n");
buffer.append("JentikLuar :"+ res.getString(2)+"\n");
}
// show all data
showMessage("Data",buffer.toString());
}
}
);
}
//membuat alert dialog
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
and here is the Input.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/gradient"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="70dp"
android:background="#drawable/gradient"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="30dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Nomor Rumah" />
<EditText
android:id="#+id/nomorrmh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Dalam Rumah" />
<EditText
android:id="#+id/jentikdirumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jentik Di Luar Rumah" />
<EditText
android:id="#+id/jentikdiluarrumah"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/roundtext"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/tambahdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="Tambah Data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
<Button
android:id="#+id/lihatdata"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="#d67601"
android:text="contoh lihat data"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:id="#+id/textviewadd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="26dp"
android:gravity="center_horizontal"
android:text="Input Data"
android:textColor="#fff"
android:textSize="26sp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/text_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
My goal is to pass the fragment into the java class that i want, because i dont know how to put that input class to the fragment always get cannot be cast to androidx.fragment.app.Fragment when i replace the GalleryFragment with pantauan.java code. thank you

how to show fragment from custom alertdialog, alert dialog is in adapter

i am trying to show fragment from a custom alertdilog's button click event, alert dialog is in fragment's adapter, i wanted to show fragment from alertdialog button click but i am unable to do so.
alertdialog code:
holder.commentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(context);
final AlertDialog alertDialog=builder.create();
View viewTmp_comment=LayoutInflater.from(context).inflate(R.layout.comment_show_layout,null,false);
RecyclerView recyclerView_commentShow=(RecyclerView)viewTmp_comment.findViewById(R.id.recyclerViewId_commentShow);
TextView textViewId_commentShow_layout=(TextView)viewTmp_comment.findViewById(R.id.textViewId_commentShow_layout);
ImageButton commentSendButtonId_Post=(ImageButton)viewTmp_comment.findViewById(R.id.commentSendButtonId_Post);
ImageButton imageUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.imageUploadButtonId_comment);
ImageButton smileyUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.smileyButtonId_comment);
editTextId_CommentPost=(EditText) viewTmp_comment.findViewById(R.id.editTextId_CommentPost);
// RelativeLayout smileyLoadLayoutId_CommentAdapter=(RelativeLayout)
final CommentShowAdapter commentShowAdapter=new CommentShowAdapter(context,newsFeedClassArrayList.get(position).getCommentShowClassArrayList(),"NewsFeed");
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
recyclerView_commentShow.setLayoutManager(linearLayoutManager);
if(newsFeedClassArrayList.get(position).getCommentShowClassArrayList()!=null) {
recyclerView_commentShow.setAdapter(commentShowAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, linearLayoutManager.getOrientation());
recyclerView_commentShow.addItemDecoration(dividerItemDecoration);
commentShowAdapter.notifyDataSetChanged();
recyclerView_commentShow.onChildDetachedFromWindow(null);
}else {
builder.setMessage("No comments yet");
}
//comment show alert dismiss
textViewId_commentShow_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
if(alertDialog.isShowing()){
alertDialog.dismiss();
}
}catch (Exception e){}
}
});
**//here i am trying to lauch fragment**
smileyUpload_CommentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ServiceClass.setSmileyKey("comment");
smileyFragment=new SmileyShowFragment();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.smileyLoadLayoutId_CommentAdapter);
if (!(fragment instanceof SmileyShowFragment)) {
fragmentTransaction.replace(R.id.smileyLoadLayoutId_CommentAdapter, smileyFragment, "Smiley").commit();
} else {
fragmentTransaction.remove(smileyFragment).commit();
}
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow(); lp.copyFrom(window.getAttributes());
lp.width = context.getResources().getDisplayMetrics().widthPixels;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity=Gravity.BOTTOM; window.setAttributes(lp);
alertDialog.setView(viewTmp_comment);
alertDialog.show();
}
});
alert xml code:
<?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="wrap_content"
android:background="#ddd"
>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/textViewId_commentShow_layout"
android:text="Comments"
android:textSize="16sp"
android:paddingLeft="20dp"
android:textStyle="bold"
android:paddingTop="10dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/underline"/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/recyclerViewId_commentShow"
>
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:weightSum="4"
android:background="#1470a6"
>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/camera"
android:id="#+id/imageUploadButtonId_comment"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="5dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/smileyButtonId_comment"
android:src="#drawable/smiley"
android:layout_gravity="center"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="60dp"
android:id="#+id/editTextId_CommentPost"
android:layout_weight="3"
android:background="#FFFFFF"
android:hint="Your comment"
android:textSize="16sp"
/>
<ImageButton
android:layout_width="30dp"
android:layout_height="match_parent"
android:id="#+id/commentSendButtonId_Post"
android:src="#drawable/send_comment"
android:background="#1470a6"
android:gravity="right"
android:layout_gravity="right"
android:layout_weight="1"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/smileyLoadLayoutId_CommentAdapter"
>
</FrameLayout>
</LinearLayout>
how to solve this problem? is it possible to show fragment inside alertdialog.i have searched on google but no similar solution
You should create a separate customized dialog. To do so you can extend DialogFragment and on onCreateDialog() set up your views and proper assignments. Create a layout for your customized dialog fragment class with a fragment inside. You can create an interface on your customized dialog to listen for actions like button clicks or others. Then, onClick of your button use fragment manager to show the fragment within your dialogfragment. Something like below:
//Give it a fragment with a FrameLayout to hold the fragment if you
need to hold more than one or with just add a fragment in your layout
if you do not need a holder.
public class MyCustomizedDialog extends DialogFragment{
private TextView dialogTitle;
private TextView dialogText;
private FrameLayout myFragmentContainer;
private Button myButton;
private CustomDialogListener customDialogListener;
public interface CustomDialogListener{
//set listeners here, i.e. for buttons clicked by user
public void onBtnClick();
}
public setDialogFragmentListener(CustomDialogListener customDialogListener){
this.customDialogListener = customDialogListener;
}
#override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//initialize dialog
setUpMyCustomDialog(dialog);
return dialog;
}
public void setUpMyCustomDialog(Dialog dialog){
dialogTitle = (TextView)dialog.findViewById(R.id.dialog_title_tv);
dialogText = (TextView)dialog.findViewById(R.id.dialog_text_tv);
myFragmentContainer=(FrameLayout)dialog.findViewById(R.id.child_fragment_container);
myButton = (Button)dialog.findViewById(R.id.myButton);
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
//initialize your fragment here.
}
}
}
}

Filter from Android onCreateOptionsMenu Item

I want to add filter button to my onCreateOptionsMenu. When button was clicked i need to show layout with checkboxes and apply button.
Only way that i know how to do this - start new activity from onOptionsItemSelected and do all operations there. But is there more efficient way? Without leaving current activity. Something like pop-up window etc.
You can do it By Using this Material Dialog
https://github.com/drakeet/MaterialDialog Library and Below code, Just
put the gravity of Dialog TOP|RIGHT.
private void showMaterialDialog() {
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice);
for (Card card : cards) {
arrayAdapter.add(card.getName());
}
alert = new MaterialDialog(this);
View mView = getLayoutInflater().inflate(R.layout.custom_view_virtual_card, null);
ListView listView = (ListView) mView.findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
AppCompatButton btnCancel = (AppCompatButton) mView.findViewById(R.id.btnCancel);
AppCompatButton btnOk = (AppCompatButton) mView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAsyncForRequestVirtualCard(selectedCardTypeId);
alert.dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCardTypeId = cards.get(position).getId();
Log.d("request", cards.get(position).getId() + " " + cards.get(position).getName());
}
});
alert.setView(mView);
alert.show();
}
custom_view_virtual.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Select Card Type"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/colorAccent" />
<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:background="#color/divider" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:divider="#color/transparent"
android:theme="#style/ProgressBarStyle"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnCancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="#string/cancel"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnOk"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Don't create a new activity instead create a new java class in that apply your sorting logic to filter the content and when your button is pressed that activity should return all your sorted content using List class or ArrayList class or anything else then display it.

Android dialog layout broken how to fix?

I had problems with this when I was programming it. I thought I'd cured it by specifying a fixed percentage height for the body of the dialog. I thought this was bad form, because the user might have a slim display, or set large fonts, which would cut of the EditText box.
Anyway that solution has failed beyond the emulator.
It looks like Android is assigning a fixed height to a dialog, and if my custom title has devoured too much of this height, squeezing everything else off. Is this correct and how do I fix it?
Problem
public class GetUserNameDialogFragment extends DialogFragment {
final String TAG = "GetUserNameDialog";
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle);
//TODO getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = getActivity().getLayoutInflater();
final View sunburst =inflater.inflate(R.layout.dialog_user_name, null);
builder.setView(sunburst);
builder.setCancelable(false)
.setPositiveButton("Let's go!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.wtf(TAG, "button press");
EditText name = (EditText) sunburst.findViewById(R.id.nameEditText);
String userName = name.getText().toString().trim();
//TODO needs to be validated
SharedPreferences sharedPref = getActivity().getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", userName );
editor.commit();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here's the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
app:srcCompat="#drawable/ic_ball_sunburst_classic"
android:background="#color/colorAccent"
/>
<LinearLayout
android:layout_width="250dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_height="125dp">
<EditText
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:hint="Enter your first name"/>
</LinearLayout>
</LinearLayout>
All help very much appreciated, the first impression of my app - what a disaster!
Why don't you use Dialog instead of AlertDialog ?
I built the example using a Dialog and it works perfectly
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_width=" 290dp"
android:layout_height="wrap_content"
android:background="#4CAF50">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="#drawable/base"
android:background="#color/colorAccent"
/>
<EditText
android:textColor="#fff"
android:textColorHint="#fff"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:id="#+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your first name"/>
<Button
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:id="#+id/letsGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Let's Go"
android:textColor="#fff"
android:background="#android:color/transparent"/>
final Dialog dialog = new Dialog(context);
if(dialog.getWindow() != null){
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
dialog.setContentView(R.layout.dialog);
Button letsGO = (Button) dialog.findViewById(R.id.letsGo);
EditText nameEditText = (EditText) dialog.findViewById(R.id.nameEditText);
letsGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Lets GO!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.show();

How to get onclick of a view in custom dialog in an activity in android?

How to get onclick of a view in custom dialog in an activity in android?
My 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:background="#color/default_green"
android:orientation="vertical" >
<TextView
android:id="#+id/tvSetNotificationsHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:text="Set Notifications On"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<RadioButton
android:id="#+id/rbFriendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Friend Request"
android:textColor="#android:color/white" />
<RadioButton
android:id="#+id/rbMessages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="Messages"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="saveSetNotificationsDialog"
android:text="Save" />
</LinearLayout>
My code is:
public void showSetNotificationsDialog(View v) {
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_setnotificationsdialog);
rbFriendRequest = (RadioButton) findViewById(R.id.rbFriendRequest);
rbMessages = (RadioButton) findViewById(R.id.rbMessages);
dialog.show();
}
I am able to show dialog view but no able to get on click of dialog save button in activity class. Error was no method found exception.
use instance of dialog for findViewById()
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
declineButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
use as following code
dialog = new Dialog(Activity_Settings.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = View.inflate(getActivity(), R.layout.dialog_setnotificationsdialog, null);
dialog.setContentView(view);
//always find your view view.findViewbyid
yourView= (RadioButton) view.findViewById(R.id.rbFriendRequest);
//then you can add on click listner to any of your view i.e.
yourView.setonClickListener(this);(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

Categories

Resources