How to check the password entered is right or not? - android

I tried to return it in dialog layout, but the debug give me null of the variable
I'm trying to check if the password is right or not, but it gave me an error of null object
Here is the function where I return the password from the database to know this function is in the main activity and the password confirm is not the context of this activity
public void showDialog(final Intent i){
LayoutInflater factory = LayoutInflater.from(this);
final View deleteDialogView = factory.inflate(R.layout.password_confirm, null);
final AlertDialog deleteDialog = new AlertDialog.Builder(this).create();
deleteDialog.setView(deleteDialogView);
deleteDialogView.findViewById(R.id.confirmPasswordCheck).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.collection("Departments").document(DeparmentName).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
TextInputLayout textInputLayout = findViewById(R.id.text_input_password_confirm);
EditText editText = findViewById(R.id.edit_password_check);
passwordReturn = task.getResult().getString("Password");
if(editText.getText().equals(passwordReturn)){
startActivity(i);
}else{
textInputLayout.setError("Password is wrong");
}
}
});
}
});
deleteDialogView.findViewById(R.id.cancelPasswordCheck).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deleteDialog.dismiss();
}
});
deleteDialog.show();
}
Here is the where i call the function and what i send to it
Intent i = new Intent(MainActivity.this, AddPatientActivity.class);
i.putExtra("SuperSession", SuperKey);
i.putExtra("DepartmentName", DeparmentName);
showDialog(i);
and this is the 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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
tools:context=".MainActivity"
android:background="#color/blueState">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/txt_password_confirm"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="24sp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:theme="#style/MyEditText"
app:errorEnabled="true"
app:errorTextAppearance="#style/MyErrorText"
app:passwordToggleEnabled="true">
<EditText
android:id="#+id/edit_password_check"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/century_gothic"
android:hint="#string/password"
android:imeOptions="flagNoExtractUi"
android:inputType="textPassword"
android:theme="#style/MyEditText" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/confirmPasswordCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_green_btn"
android:text="Confirm"/>
<Button
android:id="#+id/cancelPasswordCheck"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_red_btn"
android:text="#string/cancelBtn"/>
</LinearLayout>

You should declare view bindings before setOnClickListener().
TextInputLayout textInputLayout = findViewById(R.id.text_input_password_confirm);
EditText editText = findViewById(R.id.edit_password_check);
Then you should use editText.getText().toString().equals(passwordReturn) instead of editText.getText().equals(passwordReturn) . Because editText.getText() returns Editable

Related

Why is EditText.getText() returning null?

So while I was making my first project in android studio, I tried making an alert box where I can take inputs from 2 EditTexts and take them as arguments for creating an RecyclerView Element, but it turns out it's returning null (even though IDs of the EditTexts are mentioned correctly)
fa = findViewById(R.id.add_alarm_fab);
fa.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this);
builder.setTitle("Add slots");
View customLayout = getLayoutInflater().inflate(R.layout.alert_dialog_layout,null);
builder.setView(customLayout);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText editText1 = findViewById(R.id.edCentre);
EditText editText2 = findViewById(R.id.edSlots);
String centreName = editText1.getText().toString();
Integer slots = Integer.parseInt(editText2.getText().toString());
if (centreName!=null && slots!=null)
medCentreArrayList.add(new MedCentre(centreName,slots));
ad.notifyDataSetChanged();
}
});
builder.create().show();
}
});
This is my XML file for the alert dialog:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<EditText
android:id="#+id/edSlots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Slots available"
android:inputType="textPersonName"
android:padding="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/edCentre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:padding="20dp"
android:ems="10"
android:hint="Centre Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/edSlots"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Image of alert dialog layout:
And Image of alert dialog:
Looks like you need to find your view in the dialog container, something like this:
EditText editText1 = customLayout.findViewById(R.id.edCentre);

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

Scroll View not working in Alert Dialog

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();
}
});

How to dynamically change content inside Modal Bottomsheet android

I have a Bottomsheet xml which contains two elements, Follow and Copy link.. when I click on Follow, it should be changed to Unfollow dynamically. I tried using setText, not worked.
Please guide me how to change text of bottomsheet dialogues.
Here is my Bottomsheet xml file
<LinearLayout app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="180dp"
android:orientation="vertical"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/bottom_sheet_follow"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_mode_follow_grey_24dp"
/>
<TextView
android:id="#+id/followTView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Follow"/>
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_sheet_copy"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:foreground="?android:attr/selectableItemBackground"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_copy_grey_24dp"
/>
<TextView
android:id="#+id/copyLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:text="Copy link"/>
</LinearLayout>
Bottomsheet Java code
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
But, the Follow Textview (I mean followTView) is not changing to Unfollow.
Please tell me where I have gone wrong...
Thanks in advance...
You can do it with boolean i have done it with my imageview.
Might help you
//Initialize boolean as true by default
public boolean showingFirst = true;
holder.ivFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (showingFirst){
Toast.makeText(mActivity, "Item added to favorite", Toast.LENGTH_SHORT).show();
holder.ivFavorite.setImageResource(R.drawable.filled_heart);
showingFirst = false;
}else {
holder.ivFavorite.setImageResource(R.drawable.ic_favorite);
Toast.makeText(mActivity, "Item removed from favorite", Toast.LENGTH_SHORT).show();
showingFirst = true;
}
}
});
Just a guess. Did you try to re-arrange the code? maybe the the click listener should load first before calling "mDialog.show()".
View bottomSheetView = View.inflate(mContext, R.layout.bottom_sheet_dialog_for_sharedposts, null);
followBtnLayout = bottomSheetView.findViewById(R.id.bottom_sheet_follow);
followTView = bottomSheetView.findViewById(R.id.followTView);
followBtnLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
followTView.setText("Unfollow");
}
});
mDialog = new BottomSheetDialog(mContext);
mDialog.setContentView(bottomSheetView);
mDialog.show();

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();

Categories

Resources