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);
Related
I have two activities main and settings. I want to change the background color of the main using a button that's on settings. I tried using RelativeLayout and get the layout of the main using the id and changing the color when touching the button but it doesn't work. I also tried doing with Intent but I don't know how to do it. Got the code for the settings activity below
public class Settings extends AppCompatActivity {
private Button changeColorButton;
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
changeColorButton = findViewById(R.id.changeColorButton);
layout = findViewById(R.id.mainLayout);
/** Called when the user taps the Settings button */
changeColorButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V){
Intent intent = new Intent(Settings.this, Main.class);
startActivity(intent);
layout.setBackgroundColor(Color.BLUE);
}
});
}
}
Any help please?
You cannot change views of another activity like that.
You can send an 'extra' parameter with your Intent and read it on Settings activity. You can change the color based on the value you receive via the intent.
Or if its a more permanent setting, you can save it on SharedPreferences and read it on Main.
I'm trying to make it so a clear button will open up an alert dialog which has a yes or no. When clicking the yes button, it should pass a bool value from the dialog frag to the other frag. If the value is true, which it should be when yes is clicked, it will call methods which will clear a database. Here is the dialog frag and the part of the frag where I'm trying to implement it. I can't get the dialog box to appear, but so far it does make the screen darker which I assume means I'm not hooking it up right.
Dialog frag:
public class DialogClear extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.clear_dialog, null);
final Button yes = dialogView.findViewById(R.id.yes);
final Button no = dialogView.findViewById(R.id.no);
no.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
}
});
yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
}
});
return builder.create();
}
}
Here is how I'm trying to call it from my frag
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogClear = new DialogClear();
dialogClear.setTargetFragment(BloodPressureFragment.this, 1);
dialogClear.show(getFragmentManager(),"");
dataManager.clearDatabase();
dataManager.createDatabase();
dataText.setText("");
dataText.setBackgroundResource(R.drawable.no_border);
updateList();
}
});
welcome to code party
you have many choices to do this job, but i will show you best way for using fragments.
As you know, all fragments changes in one activity and extend from it.
So the easy way is this:
1.build public fields in your activity
2.build a getInstant method on activity
3.fill and get values of that activity from any fragments that you want show on this
see this example:
public boolean isLocationOn;
this field is build in activity
now:
in any fragment:
MainActivity.getInstance().isLocationOn = true;
in other fragment:
if(MainActivity.getInstance().isLocationOn){
//todo show map or ...
}
in this way you can use anything in fragments
update me in comments
You should read and try using 3 thing's to solve this.
1. Navigation Component
easy to navigate to fragment's and DialogFragment
2. View Model
Share same View Model between different fragment's of an activity
Data is not lost on Orientation change's
All business logic at one place and easy to unit test
3. Live Data
recommended for responsive ui
Easy to understand Api's
i have dialog with onclick button and include final Dialog dialog = new Dialog(context); how i can add cancel button to my dialog for hide dialog
and my content layout is different from activity main layout
sorry for my language
my code :
public void showd(View view){
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
You should use a DialogBuilder instead of creating the Dialog yourself. You then can create a cancel button using the following code
// Create your dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Apply your custom view
builder.setView(R.layout.custom);
// A null clickListener will cancel the dialog
builder.setNegativeButton("Cancel" /*TODO Replace with string resources*/, null);
// Show the dialog
builder.show();
How do I add a popup dialog over the call screen? I'll be using the BroadcastReceiver to listen for incoming calls and show it. I need an idea about how to write an activity that allows a dialog over an incoming call. Also, how do I make the dialog movable to any part of the screen? I already have the BroadcastRceiver implemented and performing other functions, so I could just use an intent and start the activity from this BroadcastRceiver
start an activity, then use AlertDialog Builder from that activity to prompt a dialog
set custom view to customize the dialog appearence
Try this
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.caller_dialog, null);
ImageView button = dialogView.findViewById(R.id.close_btn);
builder.setView(dialogView);
final AlertDialog alert = builder.create();
alert.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
alert.setCanceledOnTouchOutside(true);
alert.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alert.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP);
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//close the service and remove the from from the window
alert.dismiss();
}
});
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);
}
});