Own layout in Dialog Fragment - android

Well, I'm testing "Dialog Fragment" and I've done a little program that when it starts show a Dialog Fragment with 3 options, the TextView on the main activity will show which option was selected. So basicly it's communication between Dialog and Activity. I was following an example with 2 buttons(positive and negative) but now I'm testing with my own layout with 3 button and I don't know how to continue...
Let's see the code:
3 Button in dialog_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</LinearLayout>
Then the DialogFragment class
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.webkit.WebView.FindListener;
import android.widget.Button;
public class TwoActionButtonsDialog extends DialogFragment {
private DialogListener listener;
public interface DialogListener {
public void onDialogOption1(DialogFragment dialog);
public void onDialogOption2(DialogFragment dialog);
public void onDialogOption3(DialogFragment dialog);
}
// Override the Fragment.onAttach() method to instantiate the
// NoticeDialogListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the
// host
listener = (DialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement DialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setTitle("Test").setView(
inflater.inflate(R.layout.dialog_layout, null));
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
// Create the AlertDialog object and return it
return builder.create();
}
}
And the main activity
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements
TwoActionButtonsDialog.DialogListener {
private static final String TAG = "dialog";
private TextView texto = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView) findViewById(R.id.textView1);
showTwoActionButton();
}
public void showTwoActionButton() {
DialogFragment dialog = new TwoActionButtonsDialog();
dialog.show(getSupportFragmentManager(), TAG);
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the NoticeDialogFragment.NoticeDialogListener interface
#Override
public void onDialogOption1(DialogFragment dialog) {
// User touched the dialog's positive button
texto.setText("1");
}
#Override
public void onDialogOption2(DialogFragment dialog) {
// User touched the dialog's negative button
texto.setText("2");
}
#Override
public void onDialogOption3(DialogFragment dialog) {
// TODO Auto-generated method stub
texto.setText("3");
}
}
I'm not sure how to manage the buttons beacause I can't do:
Button btn1 = ((Button) findViewById(R.id.button1));
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
it throws an error: "The method findViewById(int) is undefined for the type TwoActionButtonsDialog". If I've inflated the layout why I can't acces to them?
What should I do?

After you inflate your view for a Dialog, save a result:
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_layout, null)
builder.setTitle("Test").setView(v);
After that you can traverse your view for buttons:
Button btn1 = (Button) v.findViewById(R.id.button1);

Related

Fragment via button

I have a fragment defined in my project plus an activity with a button. What I want is to launch the fragment with that button onclick listener, enter a value in the edit text defined in the fragment's xml layout file and press another button2 present in the fragment. Now what I want is that the button2 return's the text value and open's the activity again. I can then handle what happens to that value.
Java
package com.dreamgoogle.gihf;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Kahinsebhii extends Fragment {
Button ok;
EditText number;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.kahin, container, false);
ok = (Button) view.findViewById(R.id.button1);
number = (EditText) view.findViewById(R.id.editText1);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
number.getText();
}
});
return view;
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/kahinbhiedit"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok" />
</FrameLayout>
Java file to launch the fragment is some other java file.
It sound like what you need is a Dialog. Have a look at the development site: http://developer.android.com/guide/topics/ui/dialogs.html
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();

Using a list view in a custom dialogue

How would I use a list in this custom dialogue? ive set a list int the .xml of the listview but am unfamiliar with how to use it properly (I want the values for the list to come from a string array)
My main java:
package custom.dialouge.list;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
My custom dialouge .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Back" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
</ListView>
</RelativeLayout>
Try something like this:
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ListView view = dialog.findViewById(R.id.listview1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
view.setAdapter(listAdapter);
Like so:
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
ListView view = dialog.findViewById(R.id.listview1);
view.setAdapter(listAdapter);
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}

Android close custom dialog within layout

I am looking for a way to close a custom dialog with a button that is inside the xml used in the dialog, alternatively closing it by pressing anywhere on the dialog. What I have is this; a layout with a Image Button that brings up the custom dialog with the content. I have setCanceledOnTouchOutside(true); and that works, but I need the dialog to fill up most of the screen and it can be hard for the user to click in the small space that is available. So how do I do this?
My java code:
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class Rose extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.rose);
ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Dialog d1 = new Dialog(Rose.this);
d1.setContentView(R.layout.tariquet);
d1.setCanceledOnTouchOutside(true);
d1.show();
}
});
}
}
And my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:isScrollContainer="true"
android:minHeight="1100dp"
android:minWidth="650dp">
<ImageView
android:src="#drawable/rose_tariquet"
android:id="#+id/imageView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageView>
<Button android:text="X"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:layout_width="55dp"
android:layout_gravity="right"></Button>
</FrameLayout>
public class CustomizeDialog extends Dialog implements OnClickListener {
Button close;
TextView tv;
public CustomizeDialog(Context context,String Stringcontent) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_diolog_main);
tv=(TextView) findViewById(R.id.content);
tv.setText(Stringcontent);
close = (Button) findViewById(R.id.close);
close.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == close)
dismiss();
}
}
called:
CustomizeDialog customizeDialog = new CustomizeDialog(CustomDialog.this,"clickme");
customizeDialog.show();

EditText.getText Java.lang nullpointer exception

I'm having a problem with my android program.
I have about every thing working what i want, but now im trying to add a password dialog.
I want to show a Dialog with a EditText(enter password) and two buttons (ok, cancel).
(working fine till so far)
When clicking on the OK button the password should be saved in mij SendData Class.
But every time I try to get the content of the EditText it gives a Java.lang NullPointerException.
Can some one help me please?
(I don't have a stack trace as i don't know where to find it :( because i'm testing on android emulator.)
If there any thing else you need, please feel free to ask.
Here is the XML-FILE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<requestFocus />
<EditText
android:id="#+id/txt_wachtwoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/DialogOK"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txt_wachtwoord"
android:layout_below="#+id/txt_wachtwoord"
android:layout_marginTop="22dp"
android:text="OK" />
<Button
android:id="#+id/Cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/DialogOK"
android:layout_alignBottom="#+id/DialogOK"
android:layout_toRightOf="#+id/DialogOK"
android:text="Cancel" />
</RelativeLayout>
Here is the code:
package SebApp.Phone.Remote;
import java.net.DatagramSocket;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Vibrator;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class PhoneRemote extends Activity {
private static final int VIBRATE_TIME = 100;
/** Called when the activity is first created. */
public Vibrator vibrator = null;
public sendData sd = new sendData();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
//button to call the Dialog
Button ww = (Button) findViewById(R.id.ww);
//listener password button
ww.setOnClickListener(wwOnClickListener);
}
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
//set up dialog
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, cheout!
//set up button
Button button = (Button) dialog.findViewById(R.id.DialogOK);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try{
Toast.makeText(v.getContext(),"PWfield contains: " + ww.getText().toString(), Toast.LENGTH_SHORT).show();
//finish();
}catch(Exception e)
{
Toast.makeText(v.getContext(),e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
}catch(Exception E){
Toast.makeText(v.getContext(),E.toString(), Toast.LENGTH_SHORT).show();
}
}
};
}
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
//set up dialog
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
....
instead use
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
final EditText ww = (EditText) dialog.findViewById(R.id.txt_wachtwoord);
//set up dialog
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
....
you had declared EditText before setting the dialog view
I have yet to run your code. However, I would say the variable wwOnClickListener is being initialized before the onCreate method.
Therefore the layout is not set, as the layout is set within the onCreate method. To fix this I would pull out the following
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
From the buttonlistener and make ww a global variable which is initialized in the onCreate method.
I would expect that will resolve the issue.

codes in onClickListener can't be executed

I'm experiencing problem with the following code, I created two buttons with onClickListener added.
It works fine for the Button -- btnAbout, I'm trying to use another way to handle btnIntro, instead of by using findViewById(...) method, but the statement will not be executed when I click btnIntro.
Why does this happen?
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TestActivity extends Activity {
private final String TAG = "tag";
private Button btnIntro;
private Button btnAbout;
private View layoutView;
private ViewWrapper wrapper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnAbout = (Button) findViewById(R.id.btnAbout);
if (btnIntro == null) {
LayoutInflater inflator = getLayoutInflater();
layoutView = inflator.inflate(R.layout.main, null, false);
wrapper = new ViewWrapper(layoutView);
layoutView.setTag(wrapper);
} else {
wrapper = (ViewWrapper) layoutView.getTag();
}
btnIntro = wrapper.getButton();
Log.e(TAG, Integer.toHexString(layoutView.getId()) + "");
Log.e(TAG, btnIntro.getText().toString());
btnIntro.setOnClickListener(new OnClickListener() {
{
Log.e(TAG, "static");
}
#Override
public void onClick(View arg0) {
Log.e(TAG, "btnIntro clicked");
Toast.makeText(TestActivity.this, "btnIntro", Toast.LENGTH_SHORT)
.show();
}
});
btnAbout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.e(TAG, "btnAbout clicked");
Toast.makeText(TestActivity.this, "about", Toast.LENGTH_SHORT).show();
}
});
}
class ViewWrapper {
View base;
Button btn1;
ViewWrapper(View base) {
this.base = base;
}
Button getButton() {
if (btn1 == null) {
btn1 = (Button) base.findViewById(R.id.btnIntro);
Log.e(TAG, btn1.getText().toString());
}
return btn1;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MAIN_LAYOUT_XML"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test" />
<Button
android:id="#+id/btnIntro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="btnIntro" />
<Button
android:id="#+id/btnAbout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="btnAbout" />
</LinearLayout>
Problem is that you use to different content views to obtain buttons, one is created when you invoke setContentView() and second you create by invoking inflator.infate(). Thus buttons you get are placed in different content views (only one of which is shown - that created by setContentView). Try that instead:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflator = getLayoutInflater();
layoutView = inflator.inflate(R.layout.main, null, false);
setContentView(layoutView);
//..anything you need..
}

Categories

Resources