Dialog with multiple textview - java

i need to add 5 text view in my code. For now there is only one and it is used to put a marker in google maps. I would like that in addition to this you could put another 4 data insertions like (description, age, work, hobbys).Below I also put the custom dialog I made can you check if it's okay? Feel free to ask for anything. How can I do?
add_address.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View viewcustom = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText edt1 = view.findViewById(R.id.Description);
EditText edt2 = view.findViewById(R.id.Age);
EditText edt3 = view.findViewById(R.id.Hobby);
EditText edt4 = view.findViewById(R.id.City);
EditText edt5 = view.findViewById(R.id.address);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(requireContext())
.setView(viewcustom)
.setPositiveButton("Ok", (dialogInterface, i) -> {
String description = edt1.getText().toString();
String age = edt2.getText().toString();
String Hobby = edt3.getText().toString();
String City = edt4.getText().toString();
String address = edt5.getText().toString();
Toast.makeText(requireContext(), description, Toast.LENGTH_SHORT).show();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid()).child("address");
reference.setValue(address);
});
alertDialog.setNegativeButton("Cancel", null);
alertDialog.create().show();
//startAutoCompleteIntent();
}
});
Customdialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 1"
android:id="#+id/Description" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 2"
android:id="#+id/Age" />
<EditText
android:id="#+id/Hobby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Text 3" />
<EditText
android:id="#+id/City"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 4" />
<EditText
android:id="#+id/Marker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text 5" />
</LinearLayout>

You have to provide your own custom view and then you have to retrieve your EditTexts with findViewByID()
View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText edt1 = view.findViewById(R.id.Description);
// get other EditTexts here
final Dialog alertDialog = new Dialog(activity);
alertDialog.setView(view);
alertDialog.setPositiveButton("Ok", (dialogInterface, i) -> {
String description = edt1.getText().toString();
//other stuff
});
Entire sample inspired from your code
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(LayoutInflater.from(this));
setContentView(binding.getRoot());
binding.fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View viewcustom = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText edt1 = viewcustom.findViewById(R.id.Description);
EditText edt2 = viewcustom.findViewById(R.id.Age);
EditText edt3 = viewcustom.findViewById(R.id.Hobby);
EditText edt4 = viewcustom.findViewById(R.id.City);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this)
.setView(viewcustom)
.setPositiveButton("Ok", (dialogInterface, i) -> {
String description = edt1.getText().toString();
String age = edt2.getText().toString();
String Hobby = edt3.getText().toString();
String City = edt4.getText().toString();
Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
});
alertDialog.setNegativeButton("Cancel", null);
alertDialog.create().show();
}
});
}
}

Related

Android java code to add + and - button beside edittext

for(int i=0;i<j.size();i++)
{
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
JsonObject jb = (JsonObject) j.get(i);
String item = jb.get("item").getAsString();
String unit = jb.get("unit").getAsString();;
String price = jb.get("price").getAsString();;
TableRow tbrow1 = new TableRow(this);
TextView tv01 = new TextView(this);
tv01.setText(item);
tv01.setTextColor(Color.BLACK);
tbrow1.addView(tv01);
TextView tv11 = new TextView(this);
tv11.setText(unit);
tv11.setTextColor(Color.BLACK);
tbrow1.addView(tv11);
TextView tv21 = new TextView(this);
tv21.setText(price);
tv21.setTextColor(Color.BLACK);
tbrow1.addView(tv21);
Button button = new Button(this);
button.setText("+");
button.setTag(item);
tbrow1.addView(button);
final String id_ = (String) button.getTag();
EditText edText = new EditText(this);
edText.setInputType(InputType.TYPE_CLASS_NUMBER);
//edText.setText(0);
tbrow1.addView(edText);
Button button1 = new Button(this);
button1.setText("-");
button.setTag(item);
tbrow1.addView(button1);
final String id1_ = (String) button.getTag();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "button clicked for "+id_, Toast.LENGTH_LONG).show();
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "button clicked for "+id1_, Toast.LENGTH_LONG).show();
}
})
}
I'm developing Android app for grocery shop . I'm getting itms details in json
Depending on items number I'm creating dynamic rows.
Now I want to add + and - button in each row .
I want it programmatically
How to to do it.
Example: + - button in zomato while adding food to cart .
As i click + or - button that corresponding item number should increment/decrement
You can use this library. It is a simple Android library to implement a number counter with increment and decrement buttons.
https://github.com/ashik94vc/ElegantNumberButton
try this,
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView textViewCount;
int count=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewCount=findViewById(R.id.textViewCount);
textViewCount.setText(""+count);
}
public void itemSubtractClick(View view) {
if (count>0)
textViewCount.setText(""+count--);
else
Toast.makeText(this, "not allowed", Toast.LENGTH_SHORT).show();
}
public void itemAddClick(View view) {
textViewCount.setText(""+count++);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:onClick="itemSubtractClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_subtract" />
<TextView
android:id="#+id/textViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#17F44336"
android:padding="10dp"
android:text="2"
android:textSize="18sp" />
<ImageView
android:onClick="itemAddClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_add" />
</LinearLayout>

How get a value from AlertDialog in Preference

I have Settings class. And I use preference inside. In the preference I use AlertDialog which has EditText and radioButton in it.
I want to get the values of the edit text and the radio button from my settings activity, but everything I see online is just about preference OR AlertDialog and I don't understand what to do if they are combined.
my Settings.java:
public class Settings extends AppCompatActivity {
private RadioGroup radioGroup;
AlertDialog.Builder builder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_setting, new MySettingsFragment())
.commit();
}
public static class MySettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preference, rootKey);
}
#Override
public boolean onPreferenceTreeClick(Preference p){
final EditText input = new EditText(this.getContext());
if(p.getKey().equals("appointment type")){
return true;
}
if(p.getKey().equals("time frame")){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.set_time, null)).
setPositiveButton(R.string.save_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
**Here i dont know how to take the value**
}
})
.setNegativeButton(R.string.cancel_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
my Prefernce.xml :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="appointment type"
android:title="Set appointment types"
android:icon="#drawable/appoitment_type"
/>
<Preference
android:id="#+id/timeFrame"
android:key="time frame"
android:title="Allow change meeting"
android:summary="Time frame where clients can't change a meeting"
android:onClick = "popUpWindow"
android:icon="#drawable/no_meetings_changes"
/>
<Preference
android:id="#+id/remindTime"
android:key="remind time"
android:title="Meetings reminder"
android:summary="How much time to remind client before a meeting"
android:icon="#drawable/send_reminder" />
</PreferenceScreen>
my setTime.xml (the dialog):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryLight">
<EditText
android:id="#+id/num_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/default_num"
android:inputType="text" />
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/minutes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/minutes" />
<RadioButton
android:id="#+id/hours_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "#string/hours"/>
<RadioButton
android:id="#+id/days_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "#string/days"/>
<RadioButton
android:id="#+id/weeks_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "#string/weeks"/>
</RadioGroup>
</LinearLayout>
In your OnPreferenceTreeClick function in MySettingsFragment:
#Override
public boolean onPreferenceTreeClick(Preference p){
final EditText input = new EditText(this.getContext());
if(p.getKey().equals("appointment type")){
return true;
}
if(p.getKey().equals("time frame")){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.set_time, null) // add this line
builder.setView(dialogView).
setPositiveButton(R.string.save_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// take the value from selected radio button
RadioGroup radioGroup = (RadioGroup) dialogView.findViewById(R.id.radio_group);
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton radioButton = (RadioButton) findViewById(selectedId);
// get edittext value
EditText edittext = (EditText) dialogView.findViewById(R.id.num_input);
String edittextValue = edittext.getText().toString();
}
})
.setNegativeButton(R.string.cancel_settings, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}

Country code is not getting from country code picker in Android

I am developing an signup activity in android app. I want show the country telephone code(get the country from device). It may show the list of countries containing with flag, country telephone code, country name. I get the code of country code picker from https://github.com/mukeshsolanki/country-picker-android . It contain the complete code. I want to set the default country is in the phone.
TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
System.out.println("country = "+country);
When i using this code i get the country code "in". But i want to display telephone code and flag, name. When i open the screen. I want to display it automatically.
My code is shown below
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorBackground"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<ImageView
android:background="#drawable/logo"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp" />
<Button
android:text="Country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonCountry" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:backgroundTint="#d11f08"
android:entries="#array/android_dropdown_arrays"
android:padding="5dp" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="44dp"
android:inputType="phone"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="#+id/buttonSubmit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:textColor="#color/colorAccent"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="Login"/>
</LinearLayout>
</ScrollView>
Main.Activity
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
Button buttonCountry;
private Spinner spinner1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editTextPhone);
button = (Button) findViewById(R.id.buttonSubmit);
buttonCountry = (Button) findViewById(R.id.buttonCountry);
spinner1 = (Spinner) findViewById(R.id.spinner1);
TelephonyManager tm = (TelephonyManager)this.getSystemService(this.TELEPHONY_SERVICE);
String countryCodeValue = tm.getNetworkCountryIso();
System.out.println("country = "+countryCodeValue);
//String mPhoneNumber = tm.getLine1Number(); //not getting phone number
//System.out.println("phone no = "+mPhoneNumber);
buttonCountry.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinner1.setOnItemSelectedListener(new ItemSelectedListener());
final CountryPicker picker = CountryPicker.newInstance("Select Country");
picker.show(getSupportFragmentManager(), "COUNTRY_PICKER");
picker.setListener(new CountryPickerListener() {
#Override
public void onSelectCountry(String name, String code, String dialCode, int flagDrawableResID) {
// Implement your code here
Log.d("LOGTAG", "output1 : name = "+name+" code = "+code+" dialcode = "+dialCode+" flag = "+flagDrawableResID);
picker.dismiss();
}
});
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
public class ItemSelectedListener implements AdapterView.OnItemSelectedListener {
//get strings of first item
String firstItem = String.valueOf(spinner1.getSelectedItem());
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (firstItem.equals(String.valueOf(spinner1.getSelectedItem()))) {
// ToDo when first item is selected
} else {
Toast.makeText(parent.getContext(),
"You have selected : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
// Todo when item is selected by the user
}
}
#Override
public void onNothingSelected(AdapterView<?> arg) {
}
}
}
Reference : https://github.com/hbb20/CountryCodePickerProject
try to get country code using Lat, long
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> addressList = geocoder.getFromLocation(
lati, longi, 1); // lati : Latitude ,longi : Longitude
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
code=addressList.get(0).getCountryCode();
System.out.println("code :: "+addressList.get(0).getCountryCode());
}
} catch (IOException e) {
Log.e(TAG, "Unable connect to Geocoder", e);
}
String locale = context.getResources().getConfiguration().locale.getCountry();
You can use this library
Click here
by then you can do this
new DialogPlusBuilder().blurBackground()
.buildCountriesListDialog(true,new DialogPlus.CountriesDialogListener() {
#Override
public void onItemClicked(CountryDataModel countryDataModel, DialogPlus dialogPlus) {
super.onItemClicked(countryDataModel, dialogPlus);
Toast.makeText(MainActivity.this,countryDataModel.getName()+ countryDataModel.getPhone_code(), Toast.LENGTH_SHORT).show();
}
})
.show(this.getSupportFragmentManager(), "Countries List Dialog");

onClick not working in Fragment

Basically I have listview in my fragment layout. And another layout called item_todo.xml which contains my button.
The onClick() wasn't working while clicking the delete_task button in my layout.
It would be great if you could help me out !
Here is my Class ToDoFragment
public class ToDoFragment extends Fragment {
private static final String TAG = "MainActivity";
private TaskDbHelper mHelper;
private ListView mTaskListView;
private ArrayAdapter<String> mAdapter;
Button myButton;
FloatingActionButton btnadd;
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
mHelper = new TaskDbHelper(getActivity());
mTaskListView = (ListView) view.findViewById(R.id.list_todo);
//Floating action button
btnadd = (FloatingActionButton) view.findViewById(R.id.btnadd);
btnadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText taskEditText = new EditText(getActivity());
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle("Add a new task")
.setMessage("What do you want to do next?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
updateUI();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
updateUI();
return view;
}
#Override
public void onActivityCreated(Bundle saved){
super.onActivityCreated(saved);
final LayoutInflater factory = getActivity().getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.item_todo, null);
myButton = (Button) textEntryView.findViewById(R.id.task_delete);
myButton.setOnClickListener(new View.OnClickListener() {
// OnClick() Not Working !!
#Override
public void onClick(View view) {
deleteTask(view);
}
});
}
public void deleteTask(View view) {
View parent = (View) view.getParent();
TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
String task = String.valueOf(taskTextView.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(TaskContract.TaskEntry.TABLE,
TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
new String[]{task});
db.close();
updateUI();
}
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
null, null, null, null, null);
while (cursor.moveToNext()) {
int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(idx));
}
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getActivity(),
R.layout.item_todo,
R.id.task_title,
taskList);
mTaskListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(taskList);
mAdapter.notifyDataSetChanged();
}
cursor.close();
db.close();
}}
Fragment_two XML
<ListView
android:id="#+id/list_todo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal" >
<android.support.design.widget.FloatingActionButton
android:layout_width="50dp"
android:id="#+id/btnadd"
android:layout_height="50dp"
app:fabSize="normal"
android:clickable="true"
android:src="#drawable/red"
android:scaleType="center"
/>
</LinearLayout>
</RelativeLayout>
item_todo 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"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Hello"
android:textSize="20sp" />
<Button
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Done" />
</RelativeLayout>
You inflate a new View in your onActivityCreated But you never used that view !!! myButton refer to a button which its view has not been used !
Edit :
There are lots of ways you can reach your goal:
Interfaces
You can initialize an interface and pass it to fragment, so whenever something happen in your activity (button click for example), you will understand
BroadcastReceivers
You can register local broadcast receivers in your fragment and when user click on the button, you send that broadcast, so fragment will understand
i suggest the second one and using EVENTBUS library

How to show the LayoutInflater in alert message?

This is my code for layoutInflater in alert message. When running on device, only OK and CANCEL buttons appear. The popup.xml (for layout) does not exist.
This is my code
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null, false);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
agree.class);
startActivity(intObj);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
IntentExampleActivity.class);
startActivity(intObj);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
this is my popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="166dp"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:text="By using any of the websites" />
<TextView
android:id="#+id/more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.12"
android:linksClickable="true"
android:text="More" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="92dp"
android:layout_weight="0.21"
android:text="I have read and agree to the Terms and Conditions." />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="114dp"
android:layout_margin="20dp"
android:orientation="vertical" >
</LinearLayout>
Possible duplicate: Using LayoutInflater and AlertDialog
It seems you are trying to load text from static TextView. May be it should be EditText?
TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
EDITED:
Try this code:
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v)
{
LayoutInflater myLayout = LayoutInflater.from(context);
final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setView(dialogView);
AlertDialog alertDialog = alertDialogBuilder.create();
Button button1 = (Button) alertDialog.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
double sx1 = Double.parseDouble(sax1);
TextView textviewlay1 =(TextView)dialogView.findViewById(R.id.m1ab);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)dialogView.findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
double resultl1 = intp1 - (doubl1*sx1);
int precision21t = 100; //keep 4 digits
resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
textViewtotalproduct.setText(String.valueOf(resultl1));
}
});
return false;
}});
Try this
Dialog dialog = new Dialog(YourActivtyclass.this);
dialog.setContentView(R.layout.yourpopuplayout);
final TextView textView1= (TextView) dialog.findViewById(R.id.textView1);
dialog.show();
you can use this anywhere.And you can fully customize your alert dialog.

Categories