ANDROID - How to search with EditText - java

I am working on an application where I would like to do a search via EditText. I have a few buttons and I would like to make it so that when text is entered into the EditText field, it automatically searches for the Text in that button. I'm using navigation_drawer, so the code is again a bit more complicated. I just wanted to ask if it's possible to search the text of a button and if so how.
What I want to do is that the text that is entered into EditText will act as a search, that is, the buttons with text that match the text that is in EditText will show up and the rest will disappear.
fragment_morning.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:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:id="#+id/fragment_morning"
tools:context="com.example.mcdonaldscoupons.MorningFragment">
<EditText
android:id="#+id/text_input_search"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="125dp"
android:hint="Zadej název kupónu..."
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLength="100"
android:maxLines="1" />
<Button
android:id="#+id/morning1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_input_search"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="McMuffin"
android:textAllCaps="false" />
<Button
android:id="#+id/morning2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/morning1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:textAllCaps="false"
android:text="McMuffin Menu" />
</RelativeLayout>
MorningFragment.java
package com.example.mcdonaldscoupons;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class MorningFragment extends Fragment {
private EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_morning, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editText = view.findViewById(R.id.text_input_search);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
//Here goes code, right?
}
});
view.findViewById(R.id.morning1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Typ kupónu");
builder1.setMessage("Jak chceš kupón zobrazit?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Kód (Jen u tabletu)",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Kód");
builder1.setMessage("Zadej prosím tento kód:\n132 900 23");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Hotovo!",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "GRATULUJU a přeji dobrou chuť :P",Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton(
"Nefunguje",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Smůla :/\nMůžeš zkusit McDonald's aplikaci!",Toast.LENGTH_LONG).show();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
builder1.setNegativeButton(
"Kupón z aplikace",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Coming soon...",Toast.LENGTH_SHORT).show();
}
});
builder1.setNeutralButton(
"Zrušit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
view.findViewById(R.id.morning2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Typ kupónu");
builder1.setMessage("Jak chceš kupón zobrazit?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Kód (Jen u tabletu)",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Kód");
builder1.setMessage("Zadej prosím tento kód:\n132 852 34");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Hotovo!",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "GRATULUJU a přeji dobrou chuť :P",Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton(
"Nefunguje",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Smůla :/\nMůžeš zkusit McDonald's aplikaci!",Toast.LENGTH_LONG).show();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
builder1.setNegativeButton(
"Kupón z aplikace",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Coming soon...",Toast.LENGTH_SHORT).show();
}
});
builder1.setNeutralButton(
"Zrušit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
}
}

List<String> textContent = new ArrayList();
Button button1 = findViewById(R.id.morning1);
Button button2 = findViewById(R.id.morning2);
textContent.clear();
textContent.add(button1.getText());
textContent.add(button2.getText());
And then in your
#Override
public void afterTextChanged(Editable s) {
if(textContent.contains(editText.getText().toString().trim().toLowerCase()))
//do your work
}

Related

How to create a new card after taking input from dialog box

Basically on clicking a button a dialog box opens from which I am capturing information. Once I click on "Done" option present in the Dialog box, I want a new card to be created comprising of that information. I have implemented recycler view for achieving the above but for some reason it is not working. Could someone tell me what's wrong?
Here's the code of my Adapter
package com.example.android.teleconsultation_doctor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class AdapterManageSlots extends RecyclerView.Adapter<AdapterManageSlots.ManageSlotsViewHolder> {
private ArrayList<CardViewManageSlots> mManageSlots;
public static class ManageSlotsViewHolder extends RecyclerView.ViewHolder{
public TextView mSlots, mTiming;
public ManageSlotsViewHolder(#NonNull View itemView) {
super(itemView);
mTiming = itemView.findViewById(R.id.textViewTimingValue);
mSlots = itemView.findViewById(R.id.textViewSlotValue);
}
}
public AdapterManageSlots(ArrayList<CardViewManageSlots> manageSlots){
mManageSlots = manageSlots;
}
#NonNull
#Override
public ManageSlotsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview_manage_slots, viewGroup, false);
ManageSlotsViewHolder evh = new ManageSlotsViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(#NonNull ManageSlotsViewHolder manageSlotsViewHolder, int i) {
CardViewManageSlots currentItem = mManageSlots.get(i);
manageSlotsViewHolder.mSlots.setText(currentItem.getSlot());
manageSlotsViewHolder.mTiming.setText(currentItem.getTiming());
}
#Override
public int getItemCount() {
return mManageSlots.size();
}
}
Here is the JAVA code of the activity
package com.example.android.teleconsultation_doctor;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class ManageSlots extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
ImageView imageViewCalender;
TextView textViewDateValue;
String date, spinnerSlotValue, spinnerEndTimeValue, spinnerStartTimeValue;
Button buttonAddSlot;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_slots);
ArrayList<CardViewManageSlots> slotDetails = new ArrayList<>();
imageViewCalender = findViewById(R.id.imageViewCalander);
textViewDateValue = findViewById(R.id.textViewDateValue);
buttonAddSlot = findViewById(R.id.buttonAddSlot);
imageViewCalender.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDatePickerDialog();
}
});
buttonAddSlot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(ManageSlots.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_manage_slots, null);
Spinner mSpinnerSlots = mView.findViewById(R.id.spinnerSlots);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ManageSlots.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.slot_names));
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mSpinnerSlots.setAdapter(adapter);
spinnerSlotValue = mSpinnerSlots.getSelectedItem().toString();
Spinner mSpinnerStartTime = mView.findViewById(R.id.spinnerStartTime);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(ManageSlots.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time));
adapter1.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mSpinnerStartTime.setAdapter(adapter1);
spinnerStartTimeValue = mSpinnerStartTime.getSelectedItem().toString();
Spinner mSpinnerEndTime = mView.findViewById(R.id.spinnerEndTime);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(ManageSlots.this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.time));
adapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
mSpinnerEndTime.setAdapter(adapter2);
spinnerEndTimeValue = mSpinnerEndTime.getSelectedItem().toString();
mBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//Toast.makeText(ManageSlots.this, "Slot Created!" + spinnerSlotValue + spinnerEndTimeValue + spinnerStartTimeValue, Toast.LENGTH_LONG).show();
dialogInterface.dismiss();
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
}
});
String finalTime = spinnerStartTimeValue + "-" + spinnerEndTimeValue;
Toast.makeText(this, finalTime, Toast.LENGTH_SHORT).show();
slotDetails.add(new CardViewManageSlots(spinnerSlotValue,finalTime));
//slotDetails.add(new CardViewManageSlots("Morning","diwwodmw"));
mRecyclerView = findViewById(R.id.recyclerViewSlots);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new AdapterManageSlots(slotDetails);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
private void showDatePickerDialog() {
DatePickerDialog datePickerDialog = new DatePickerDialog(
this,
this,
Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH),
Calendar.getInstance().get(Calendar.DAY_OF_MONTH)
);
datePickerDialog.show();
}
#Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
int i3=i1;
i3=i3+1;
date = i2 + "/" + i3 + "/" + i;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,i);
cal.set(Calendar.MONTH,i1);
cal.set(Calendar.DAY_OF_MONTH,i2);
String selectedDate = DateFormat.getDateInstance().format(cal.getTime());
textViewDateValue.setText(selectedDate);
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Here is the XML code of the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ManageSlots">
<include
layout="#layout/toolbar_layout"
android:id="#+id/mytoolbar"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date:"
android:id="#+id/textViewSelectDate"
android:layout_below="#id/mytoolbar"
android:layout_margin="10dp"
android:textSize="20dp"
android:textColor="#000"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewCalander"
android:src="#drawable/round_today_black_18dp_2x"
android:layout_toRightOf="#id/textViewSelectDate"
android:layout_below="#id/mytoolbar"
android:layout_marginTop="5dp"
android:clickable="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewDateValue"
android:layout_below="#id/imageViewCalander"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:text="Aug 21, 2020"
android:textColor="#000"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textViewDateValue"
android:id="#+id/recyclerViewSlots"
android:padding="4dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonAddSlot"
android:text="Add Slot"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:background="#color/colorPrimaryDark"
android:textColor="#fff"/>
</RelativeLayout>
If I understand it correctly, you want something like this:
mBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
spinnerSlotValue = mSpinnerSlots.getSelectedItem().toString();
spinnerStartTimeValue = mSpinnerStartTime.getSelectedItem().toString();
spinnerEndTimeValue = mSpinnerEndTime.getSelectedItem().toString();
String finalTime = spinnerStartTimeValue + "-" + spinnerEndTimeValue;
slotDetails.add(new CardViewManageSlots(spinnerSlotValue,finalTime));
mAdapter.notifyDataSetChanged();
dialogInterface.dismiss();
}
});

How to access another class function from another class (java)

I'm new to java and I'm currently testing it in android studio.
I'm trying to access function showMessage() in TEST class from TEST2 class.
There is no problem if showMessage() function is called within the same class but the code stopped if I tried to access it from another class.
Maybe there is something wrong with my logic or syntax, but I'm not entirely sure which one is it. I already tried to search for some solution but there is no answer that I can find to help with my situation.
Here is my TEST.java code
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class TEST extends AppCompatActivity {
public TEST2 ab;
Button button;
private final String text ="Testing test test test testing test";
public void showMessage(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button = (Button) findViewById(R.id.buttontest);
ab = new TEST2(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showMessage(text);
}
});
}
}
class TEST2 extends View{
public TEST t = new TEST();
public TEST2(Context context) {
super(context);
//there is error if I tried this 2 line code
//onTest();
//t.showMessage("TESTING FROM TEST2 CLASS");
}
public void onTest(){
t.showMessage("TESTING FROM TEST2 CLASS");
}
}
and this is my activity_test.xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/buttontest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
You can do this by passing parameter context
public void showMessage(String message, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
Alert Dialog is a UI element it will not run from another class if activity is changed. if activity is not change try this...
public void showMessage(final String message,final Context mContext) {
runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setCancelable(false) // cancel with button only
.show();
}
});
}
public void onTest(){
if (getContext() instanceof Test)
((Test)getContext()).showMessage("TESTING FROM TEST2 CLASS");
}
Just pass the Context of your TEST2 class.
public void showMessage(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
.............
......................
}
TEST2:
public TEST2(Context context) {
super(context);
t.showMessage(context, "TESTING FROM TEST2 CLASS");
}

Android AlertDialog with a common message for 3 buttons

Please help me with my code
I need to show an Android AlertDialogBox with a common message for 3 buttons, i.e Yes, No, And Cancel. As in this I have used simple Android AlertDialog with a message for individual 3 buttons, i.e. Yes, No and Cancel.
So I think there is need of function to be call.
The layout and Java part are given below
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.talent.simpledialogbox.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="/hello_world"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:layout_above="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/Yes"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_above="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/Cancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:layout_above="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:id="#+id/No"/>
</RelativeLayout>
Java Part
package com.talent.simpledialogbox;
import android.os.Bundle;enter code here
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.RequiresPermission;
import android.view.Menu;
import android`enter code here`.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Yes,No, Cancel;
Object myObject = new Object();
RequiresPermission.Read FunctionCalling;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes = (Button)findViewById(R.id.Yes);
No = (Button)findViewById(R.id.No);
Cancel= (Button)findViewById(R.id.Cancel);
Yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Delete...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
}
});
No.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm No)...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Cancel...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on Cancel", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
builder.show();
}
});
}
}
Assign AlertDialog object and assign it to the buider.create()
package com.talent.simpledialogbox;
import android.os.Bundle;enter code here
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.RequiresPermission;
import android.view.Menu;
import android`enter code here`.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Yes,No, Cancel;
Object myObject = new Object();
RequiresPermission.Read FunctionCalling;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes = (Button)findViewById(R.id.Yes);
No = (Button)findViewById(R.id.No);
Cancel= (Button)findViewById(R.id.Cancel);
Yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Delete...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
No.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm No)...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Cancel...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on Cancel", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}

Want to Ask users to confirm his info is correct before startActivity, android

Hello I m newbie in Android development.
So i practiced with one sample which is sending ussd code to operator such as recharging bill. I am okay with that.
But my main problem is to show dialog box when clicking button1 after filling PIN code to examine the user correct or not.
I don't know how to add and what to add.
My codes are -
for MainActivity.java
package com.messagealert;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
}
[MENTION=439709]override[/MENTION]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
for activity_main.xml
<RelativeLayout xmlns:android="h t t p://schemas. android. com/apk/res/android"
xmlns:tools="h t t p:// schemas. android. com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="22dp"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number"
android:maxLength="6"
android:hint="Enter 6 numbers here"
>
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginTop="31dp"
android:layout_centerHorizontal="true"
android:text="#string/button1" />
</RelativeLayout>
Please guide what i need to put & where exactly to show a dialog box "Is that your PIN code - xxxxxx" & Yes or No
Sorry for my bad English.
With Best Regards,
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("PIN VERIFICATION");
dlgAlert.setTitle("Is that your PIN code -"+pin );
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
dlgAlert.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
editText1 .setText(" ");
}
});
For this you have to add alert box Just like the below code
new AlertDialog.Builder(this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
or try this
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
Just add dialog in your button click only
EditText editText1 =(EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
//call dialog here and on dialog ok press start your activity
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
Use code below, this will help you:
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
dialog();
}
});
public void dialog()
{
AlertDialog.Builder dialog = new Builder(con);
dialog.setTitle("Message:");
dialog.setMessage("Is that your PIN code - xxxxxx");
dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
dialog.dismiss();
}
});
dialog.show();
}
use variable global if needed.
use an alertDialog for that inside you button onClick
AlertDialog.Builder dialog = new Builder(con);
dialog.setTitle("Confirm PIN");
dialog.setMessage("You have entered pincode as"+pin+"\n Please Confirm to Continue");
dialog.setNegativeButton("Edit Pin", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
dialog.dismiss();
}
});
dialog.show();
Firstly, I am very thank you for your answers.
Sorry i can't vote up your answers because i haven't enough reputations.
By adapting MSS's answers i changed a bit at MainActivity.java
package com.messagealert;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);
dlgAlert.setTitle("PIN VERIFICATION");
dlgAlert.setMessage("Is that your PIN code -" + pin );
dlgAlert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String encodedHash = Uri.encode("#");
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
dlgAlert.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EditText editText1 =(EditText) findViewById(R.id.editText1);
editText1 .setText(" ");
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
};
});
}}
You guys did a great help on me!
With Regards,

How can I target an EditText on a DialogFragment

I created a DialogFragment that looks like this:
package com.name.test;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.name.test.MainActivity.DialogType;
public class AlertDialogFragment extends DialogFragment {
public static DialogType type;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.search_fragment, null);
final EditText searchField = (EditText)v.findViewById(R.id.searchEditTxt);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
switch(type) {
case SEARCH:
builder.setView(inflater.inflate(R.layout.search_fragment, null))
.setTitle("Search Dialog")
.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the search button
// Toast.makeText(getActivity().getBaseContext(), "Search Dialog", Toast.LENGTH_SHORT).show();
String tempString = searchField.getText().toString();
System.out.println(tempString);
}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});
break;
case PREFERENCES:
builder.setView(inflater.inflate(R.layout.preferences_fragment, null))
.setTitle("Preferences")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the search button
Toast.makeText(getActivity().getBaseContext(), "Preferences", Toast.LENGTH_SHORT).show();
}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});
break;
default:
break;
}
return builder.create();
}
public static AlertDialogFragment newInstance(DialogType dialogType) {
type = dialogType;
return new AlertDialogFragment();
}
}
In the XML file for this fragment I have this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="search_fragment"
android:orientation="vertical" >
<EditText
android:id="#+id/searchEditTxt"
android:hint="#string/search_menu_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm trying to target that EditText in the DialogFragment so when the user types something and clicks on Save or OK I can store its value. I can't seem to be able to target the EditText properly. My System.out.println(tempString); is empty. What am I doing wrong? Thanks much.
try to change
builder.setView(inflater.inflate(R.layout.search_fragment, null));
to
builder.setView(v);
You just need to set your inflated view to your builder.

Categories

Resources