i want to put some string of a bundle into an custom dialog. So far i figured out Dialog doesnt handle bundles. I tried to create an onCreate Method with getIntent().getExtras(), but it doesnt work.
Can someone give me an advise?
package com.droidfish.apps.acli;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class ShowDetails extends Activity {
TextView tvShowDetailsContent1, tvShowDetailsContent2,
tvShowDetailsContent3;
public String sDetailText1, sDetailText2, sDetailText3;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/** Display Custom Dialog */
// CustomizeDialog customizeDialog = new CustomizeDialog(this);
CustomizeDialog customizeDialog = new CustomizeDialog(this);
tvShowDetailsContent1 = (TextView) findViewById(R.id.tvShowDetailText1);
tvShowDetailsContent2 = (TextView) findViewById(R.id.tvShowDetailText2);
tvShowDetailsContent3 = (TextView) findViewById(R.id.tvShowDetailText3);
savedInstanceState = this.getIntent().getExtras();
sDetailText1 = savedInstanceState.getString("param1");
sDetailText2 = savedInstanceState.getString("param2");
sDetailText3 = savedInstanceState.getString("param3");
tvShowDetailsContent1.setText(sDetailText1);
tvShowDetailsContent2.setText(sDetailText2);
tvShowDetailsContent3.setText(sDetailText3);
customizeDialog.show();
}
class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
ShowDetails sh = new ShowDetails();
public CustomizeDialog(Context context) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.showdetails);
okButton = (Button) findViewById(R.id.bOkButton);
okButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == okButton)
dismiss();
}
}
}
You can implement your own constructor for the dialog that accepts a bundle
public CustomizeDialog(Context context, Bundle bundle) {
super(context);
/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.showdetails);
//do whatever with your bundle here
okButton = (Button) findViewById(R.id.bOkButton);
okButton.setOnClickListener(this);
}
Then in your onCreate you can call
CustomizeDialog customizeDialog = new CustomizeDialog(this, getIntent().getExtras());
Don't forget to check if your bundle is null when creating the dialog
Related
I try to develop a simple Android App with one Button which generates new TextViews on each click.
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(this);
}
});
}
}
My code is wrong because of:
TextView mTV1 = new TextView(this);
I could find some similar examples, which generate objects programmatically in onCreate(). But I want to generate and modify new objects in onClick().
Would anybody please help?
Change
TextView mTV1 = new TextView(this);
to
TextView mTV1 = new TextView(CreateTV.this);
Views can only be instantiated with a context as parameter
As you can see in the documentation a TextView needs the context to be created. TextView(Context context)
Since you are trying to create a TextView inside a ClickListener you can not use this as a reference to a Context-extending object.
As McAdam331 pointed out, use new TextView(getActivity), this works because Activity extends Context.
In addition to change TextView mTV1 = new TextView(this); to TextView mTV1 = new TextView(CreateTV.this);, you must add the TextView within a view like the following:
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(CreateTV.this);
addContentView(mTV1);
}
});
}
}
I would prefer adding a Context, setting it to final and then call the Textview using the Context.
Example:
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
final Context mContext = this;
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(mContext);
addContentView(mTV1);
}
});
}
}
If you want to use the Context outside the onCreate method (and within Listeners) you can define a Context.
private Context context;
public void onCreate(....) {
this.context = this;
}
private void aMethod() {
context....
}
Theres another way doing such cool stuff. Create a Class and extends it by Application.
public class MainApplication extends Application {
public static Context getContext() { return this; }
}
Then add the MainApplication to your Manifest.
<application
android:name=".MainApplication"
>
and access it from everywhere with MainApplication.getContext();
I wish to have questions that change to the next question in the sequence onClick but I'm having trouble creating the array of questions and writing the onClick code for that portion.
*Too add more context to the app it will be a questionaire app with a question above an editText field and the users inputs will show up in its respective box at the top of the screen.
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class menu extends Activity {
Button mButton;
EditText mEdit;
int questionNumber = 0;
String [] questions;
int numberOfQuestions = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.userAnswereditText);
questions=new String[numberOfQuestions];
questions[0]="This is first question?";
questions[1]="This is second question?";
final TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
mEdit.setText(null);
questionNumber++;
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
I have two activity "Data" and "OpenedClass" when i create an Intent to redirect me to an other activity it doesn't work
Data activity
package com.example.androidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Data extends Activity implements OnClickListener {
EditText sendET;
Button start,startFor;
TextView gotAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
}
private void initialize()
{
sendET= (EditText) findViewById(R.id.etSend);
startFor = (Button) findViewById(R.id.bSAFR);
start = (Button) findViewById(R.id.bSA);
gotAnswer = (TextView) findViewById(R.id.tvGot);
start.setOnClickListener(this);
startFor.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bSA:
String bread = sendET.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Data.this,OpenedClass.class);
a.putExtras(basket);
startActivity(a);
break;
case R.id.bSAFR:
break;
}
}
}
And Opened Class Activity
package com.example.androidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class OpenedClass extends Activity implements OnClickListener , OnCheckedChangeListener {
TextView question , test;
Button returnData;
RadioGroup selection;
String gotBread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
initialize();
Bundle b = getIntent().getExtras();
gotBread = b.getString("key");
question.setText(gotBread);
}
private void initialize()
{
question= (TextView) findViewById(R.id.tvQuestion);
test = (TextView) findViewById(R.id.tvText);
returnData = (Button) findViewById(R.id.bReturn);
selection = (RadioGroup) findViewById(R.id.rgAnswers);
returnData.setOnClickListener(this);
selection.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rCrazy:
break;
case R.id.rSerious:
break;
case R.id.rBoth:
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
here is the manifest
<activity
android:name=".Data"
android:label="#string/app_name" >
</activity>
<activity
android:name=".OpenedClass"
android:label="#string/app_name" >
</activity>
the button doesn't redirect me to the second activity , help me please
You forgot to call initialize method after setContentView on your Data Activity, so the Listener's are not being registered on Button's. Like:
public class Data extends Activity implements OnClickListener {
EditText sendET;
Button start,startFor;
TextView gotAnswer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
initialize();
}
// Rest of your code...
}
Try this from your Data Activity:
Intent intent = new Intent(mContext, OpenedClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtras(basket);
mContext.startActivity(intent);
Where mContext is defined in your Data Activity as:
private Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
initialize();
// Other codes here.
}
In your Data Activity,your initialize() did not call.
I am new in Android Development. I want to open the Alert Dialog for Number Picker from Main Activity, and then take input from Alert Dialog and show it in the Main view.
I have written code from taking some references and its working correct. But i don't want to use " implements NumberPickerFragment.NoticeDialogListener" in main activity. Please help me, how can i return the value to main activity.
My code for Main Activity is:
package com.pinnacleappdesign.pinnacleappdesign;
import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements NumberPickerFragment.NoticeDialogListener{
int memoryIndex = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Button firstPaneButton = (Button)findViewById(R.id.first_pane_button1);
firstPaneButton.setOnClickListener(new OnClickListener() {
public void onClick(View v){
DialogFragment newFragment = new NumberPickerFragment();
Bundle args = new Bundle();
args.putInt("currentMemoryIndex", memoryIndex);
newFragment.setArguments(args);
newFragment.show(getFragmentManager(), "numberPicker");
}
});
}
#Override
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;
}
public void onDialogPositiveClick(int newMemoryIndex) {
this.memoryIndex = newMemoryIndex;
/** Getting the reference of the textview from the main layout */
TextView tv = (TextView) findViewById(R.id.tv_android);
/** Setting the selected android version in the textview */
tv.setText("Your Choice : " + this.memoryIndex);
}
}
My code for NumberPickerFragment.java is:
package com.pinnacleappdesign.pinnacleappdesign;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.Toast;
public class NumberPickerFragment extends DialogFragment{
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NoticeDialogListener {
public void onDialogPositiveClick(int newMemoryIndex);
}
// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;
// 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
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = getArguments();
int currentMemoryIndex = bundle.getInt("currentMemoryIndex");
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
View DialogView = inflater.inflate(R.layout.number_picker, null);
final NumberPicker np = (NumberPicker)DialogView.findViewById(R.id.numberPicker1);
np.setMinValue(1);
np.setMaxValue(100);
np.setWrapSelectorWheel(false);
np.setValue(currentMemoryIndex);
builder.setTitle(R.string.dialog_title)
.setView(DialogView)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User confirmed the dialog
int position = np.getValue();
mListener.onDialogPositiveClick(position);
}
})
.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();
}
}
What is wrong with your current method? That appears to be the method Android encourages developers to use as seen here: Communicating with Fragments
You can also do something like this within your NumberPickerFragment:
((MainActivity) getActivity()).yourMethod();
But IMO it is much cleaner and re-usable to use the defined Interface method.
Here is my code:
package com.example.userpage;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class UserPage extends Activity {
AlertDialog.Builder builder;
private final static int EMPTY_TEXT_ALERT = 0;
#Override
public Dialog onCreateDialog(int id) {
switch(id) {
case EMPTY_TEXT_ALERT: {
builder = new AlertDialog.Builder(this);
builder.setMessage("Message:Fields Empty!!!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
}
return null;
}
String tv,tv1;
EditText name,pass;
TextView x,y;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.widget44);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent obj = new Intent(UserPage.this,UserPage.class);
startActivity(obj);
}
});
x = (TextView) findViewById(R.id.widget46);
y = (TextView) findViewById(R.id.widget47);
name = (EditText)findViewById(R.id.widget41);
pass = (EditText)findViewById(R.id.widget43);
Button button1 = (Button) findViewById(R.id.widget45);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//tv= name.getText().toString();
//tv1 = pass.getText().toString();
x.setText(tv);
y.setText(tv1);
tv = name.getText().toString();
if(tv.trim().equals("")) {
// text is empty
showDialog(EMPTY_TEXT_ALERT);
}
tv1 = pass.getText().toString();
if (tv1.trim().equals(""))
{
showDialog(EMPTY_TEXT_ALERT);
}
}
});
}
}
What is happening when you try to run it? Which part are you having a problem with?
From what I can tell your code is not going to display any dialogs because you've never called the dialog.show() method. You'd have to do something like this for the way you have it set up:
showDialog(EMPTY_TEXT_ALERT).show();
If you are trying to make it two separate dialogs, one for name, and one for pass then all you'd have to do is make another id variable and add a case: for it inside the switch statement that inside your showDialog(id) method.
You should also really consider using descriptive names for your variables. Your code would be easier to understand if you didn't use names like x,y, and widget#.