how to set on click method for text inside dialog - java

I have dialog contain TextView. Inside the TextView I have number so i want to set onClick method for the TextView and I want to use call method to call the number inside the TextView
Inside the dialog xml file i set the textview as below
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#2d76ba"
android:layout_toRightOf="#+id/image"
android:onClick="no1"
android:clickable="true"/>
and here is my dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.pop); // pop is my dialog xml
dialog.setTitle("Phone Call");
TextView text = (TextView) dialog.findViewById(R.id.text1);
text.setText(values[0]); /// I'm calling the number from array
public void no1(View v)
{
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"+values[0]));
startActivity(dial);
}
but the onClick method (no1) is wrong it says "void is an invalid type for the variable no1"
how can i correct the method to work inside the dialog ?
I tried to make it outside the dialog but when i click the textview the logcat says
11-04 13:39:48.666: E/AndroidRuntime(26249): java.lang.IllegalStateException: Could not find a method no1(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.TextView with id 'text1'
so it looks like the function is not available

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.pop); // pop is my dialog xml
dialog.setTitle("Phone Call");
TextView text = (TextView) dialog.findViewById(R.id.text1);
text.setText(values[0]);
text.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"+Global.values[0]));
startActivity(dial);
}
});

Related

Custom dialog in launcher activity overwriting activity layout

I have a dialog in my launcher activity that requires some user input and to display it I have this code in onCreate()...
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
DialogBinding dialogBinding = DataBindingUtil.setContentView(this, R.layout.dialog);
popupBinding.dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
The dialog button has an onClick listener to close it, but in order to do that, the dialog must be declared final. Previously (when this activity was not the launcher), I had the dialog declared as an instance variable, but this causes an error now.
private Dialog dialog = new Dialog(this);
However, declaring the dialog in the onCreate method causes the launcher activity's layout to be replaced with the dialog's so that the dialog appears over a copy of itself.
I'm not sure why it is doing that, but I was wondering if there is a way to prevent this. Thanks!
This accidentily sets the dialog layout as the content view for your activity.
DialogBinding dialogBinding = DataBindingUtil.setContentView(this, R.layout.dialog);
So you're mixing your activity with the dialog.
For your Activity, this should be something like
MyActivityBinding activityBinding = DataBindingUtil.setContentView(this, R.layout.my_activity);

Set the value of a TextView, from an Alert

I've got a Fragment (ActionBar Activity with 3 tabs) with a TextView
There is also a Button which open an Alert, in which there is Button and an EditText
What i would like to do, is to set the value of the Fragment's TextView to the value of the Alert's EditText, when clicking on the Button.
Here is what i tried to do (the method i show you is in the MAinActivity and not in the Fragment) :
public void showTimePicker(Button but) {
final Dialog dialog = new Dialog(this);
final TimePicker t;
dialog.setContentView(R.layout.alert_time);
dialog.setTitle("Heure");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TimePicker t = (TimePicker) dialog.findViewById(R.id.time_cri);
/* THIS LINE MAKE A NULLPOINTEREXCEPTION */
time.setText(getSelectedTime(t)); /* GET THE TIME FROM TIMEPICKER */
dialog.dismiss();
}
});
CriFragment f = (CriFragment) mSectionsPagerAdapter.getItem(2);
time = f.getTextView(); /* GETTER OF THE TEXTVIEW I'D LIKE TO SET */
dialog.show();
}
The line i pointed out make a nullPointerException (at onClick())
Thank you for reading !
You have to get the TextView "time". Because the TextView "time" isn't initialized. You can use dialog.findViewById, not f.getTextView.

How to create a custom alertDialog with a list inside bound to an onClick event android?

I guess I am going slightly mad... But I can't figure it out. I would like to show a custom alert dialog bound to an Onclick event with a list of data which come from a database.
All works perfectly if I use a simple Dialog, but I would like to make it custom in order to set a title, some text views and buttons on the top of the alert dialog (instead of just show a title with the .setTitle() method).
Here is my code:
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
#Override
public void run() {
infoListButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom Dialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog_institutionalinfos, null);
TextView text = (TextView) layout.findViewById(R.id.custom_dialog_text);
text.setText("Hello, this is a custom dialog!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
// inflating the custom institutional expandable list layout
LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
alertDialog.setContentView(v);
InstitutionalInfoListView = (ListView) v.findViewById(android.R.id.list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
alertDialog.show();
}
});
}
});
}
the app crashes when I click on the infoListButton and I get A message in the LogCat:
03-16 19:25:22.905: E/AndroidRuntime(5301): FATAL EXCEPTION: main
03-16 19:25:22.905: E/AndroidRuntime(5301): android.util.AndroidRuntimeException:
requestFeature() must be called before adding content
and an error on the line where I call alertDialog.show(); method. Why this happens? How to resolve?
I just want to add a custom title, a button and a text view to my AlertDialog.
How to do that?
EDIT: how can I custom the title and add a button to the top-right of the dialog to bind it with an onclick event that will dismiss() the dialog???
EDIT: I have created a MyCustomDialog class with this code:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog_institutionalinfos);
TextView dialogTitle = (TextView) findViewById(R.id.custom_dialog_text);
dialogTitle.setText("My First Custom Dialog");
}
}
And this 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="fill_parent" >
<TextView
android:id="#+id/custom_dialog_text"
android:layout_width="80dp"
android:layout_height="wrap_content"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"/>
</RelativeLayout>
The code where I instantiate the dialog:
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
// inflating the custom institutional expandable list layout
LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
dialog.setContentView(v);
InstitutionalInfoListView = (ListView) v.findViewById(android.R.id.list);
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
But there's no Text displayed in the dialog. I only see the list:
What am I missing?
UPDATE: MyCustomDialog class:
public class MyCustomDialog extends Dialog {
public MyCustomDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_title);
TextView dialogTitle = (TextView) findViewById(R.id.custom_dialog_text);
dialogTitle.setText("Hello I am a dialog");
}
}
The custom_title.xml:
<?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="40dp"
android:background="#3FD100"
android:orientation="vertical" >
<TextView
android:id="#+id/custom_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The code where I instantiate MyCustomDialog class:
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
InstitutionalInfoListView = (ListView) dialog.findViewById(R.id.custom_dialog_list);
final MasterDetailArrayAdapter adapter = new
MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();
The app crashes with a null pointer exception at
InstitutionalInfoListView.setAdapter(adapter);
UPDATE: the TextView is inside the ListView, how to proceed?
Why do you use an AlertDialog? You can use a simple Dialog which generally causes less problems and is more appropriate for custom layouts.
EDIT: Details
First create an XML layout file, just like you would do for an Activity. Then create a class that extends Dialog. Say your custom dialog class is called CustomDialog. In public CustomDialog(Context context) call:
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
// find your views and customize them here
EDIT: HOW TO CREATE CUSTOM DIALOG INSTANCE
in your xml, also give the ListView a custom id: android:id="#+id/custom_dialog_list"
check the comments in the section below
// custom Dialog
MyCustomDialog dialog = new MyCustomDialog(thisContext);
// you DO NOT need to inflate a new view. by dialog.setContentView(v); you *replace* your XML file with another layout.
// inflating the custom institutional expandable list layout
//LayoutInflater li = (LayoutInflater) thisContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View v = li.inflate(R.layout.institutional_info_custom_list, null, false);
//dialog.setContentView(v);
// dont forget to check this line
InstitutionalInfoListView = (ListView) dialog.findViewById(R.id.custom_dialog_list); // set a custom id for your list in the layout
final MasterDetailArrayAdapter adapter = new MasterDetailArrayAdapter(HomeComune.this, MasterDetailInstitutionalInfoList);
InstitutionalInfoListView.setAdapter(adapter);
dialog.show();

Simple validation of editText not working (Eclipse)

I'm a beginner to Java and I want to validate an EditText. What I have in mind: my editText has to match "helloworld". When you press a button this has to be validated. If this is true--> go to a new class in which I have a setContentView to display a new layout.
If the text which I have just typed does not match "helloworld", it should do nothing. It seems very easy but since I'm a beginner you would help me BIGTIME!
Here's most of the logic handled. You will need to fill in your actual layout id's and make your launch intent. Put this code in your onCreate method in the activity with the layout that contains the edit text box
EditText editText = (EditText)findViewById(R.id.editTextBox);
Button btn = (Button)findViewById(R.id.checkBtn);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
if(editText.getText().toString().equalsIgnoreCase("helloworld")){
//Launch activity with new view
}
}
});
In an activity (or android class) you have to get the instance of your EditText. Your edit text has an id, and you can get it using R. R is the resources for your app.
EditText t = (EditText)findViewById(R.id.<Name of your textfield>);
Then you can get the value of that textfield and compare it
t.getText().toString().equals("helloworld");
will return true or false. If you dont care about the case of the letters use
t.getText().toString().toLowerCase().equals("helloworld");
you will need an onClickListener for your button, check out the android api
http://developer.android.com/reference/android/view/View.OnClickListener.html
in your onCreate, when declaring your submit button, add a listener
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(submitListener);
make a new onClick listener and fire an Intent to start a new activity
View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View v) {
//if string matches helloworld fire new activity
Intent newActivity = new Intent();
startActivity(newActivity);
}
};
// create a reference to the EditText in your layout
EditText editText = (EditText)findViewById(R.id.editTextIdInLayout);
// create a reference to the check Button in your layout
Button btn = (Button)findViewById(R.id.buttonIdInLayout);
// set up an onClick listener for the button reference
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String userInput = editText.getText().toString(); // get the user input
if (userInput.equals("helloworld") // see if the input is "helloworld"
{
setContentView(R.layout.newLayout); // change the content view
}
}
});

App chrashes when I click button and select item on spinner

I want to do a certain action on button press according to which item on spinner is selected.
This is what I've got so far:
public void submitButton (View v){
Button b1 = (Button)findViewById(R.id.submitButton);
final Spinner s1 = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int position = s1.getSelectedItemPosition();
switch (position){
case 0:
AlertDialog.Builder spinnerErrorBuilder = new AlertDialog.Builder(context);
spinnerErrorBuilder.setTitle("Warning");
spinnerErrorBuilder.setMessage("Please choose an item from the list");
spinnerErrorBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog spinnerError = spinnerErrorBuilder.create();
spinnerError.show();
break;
case 1:
break;
}
}
});
}
When I compile my app and click the button, the app crashes and returns to main activity. It doesn't matter which item I have selected (0 or 1) the app still crashes. Could someone tell me where I went wrong?
XML code for the button:
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/checkBox25"
android:text="#string/addMaterial"
android:onClick="onClick" />
Logcat file:
06-22 15:00:13.455: E/AndroidRuntime(23409): java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.example.gw2legendary.Bifrost for onClick handler on view class android.widget.Button with id 'submitButton'
Simply delete this line:
android:onClick="onClick"
within your xml. Be sure to call submitButton from your onCreate without passing in a view as this is not needed.
You can either set an onclicklistener in code as you have done by
b1.setOnClickListener...
OR just have a method such as:
public void method { //This is a method so do stuff here }
And set it in your xml as follows
android:onClick="method"
In your above example changing method to submitButton would work.
Your method name is submitButton but your onClick method in xml is onClick
Change it to submitButton and your problem is solved
Xml Should be
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignLeft="#+id/checkBox25"
android:text="#string/addMaterial"
android:onClick="submitButton " />

Categories