I have a maintenance application, where the operator does a checklist, and the previous steps are disabled, but I need to be able to go back to the previous step, and I would like to do this in an alertDialog
I created a setOnLongClickListener and I'm getting the View, and trying to add in alertDialog, but I get the exception that the layout has a parent (), when I remove View with, removeView (), it works, but I can't remove the View of princiapal activity
passo1.setOnLongClickListener(v -> {
dialogBuilder = new AlertDialog.Builder(initManutencao.this);
if (v.getParent() != null){
((ViewGroup) v.getParent()).removeView(v);
}
dialogBuilder.setView(v);
alertDialog = dialogBuilder.create();
alertDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
alertDialog.show();
//disableItens.ShowItensScren(0, initManutencao.this);
return false;
});
In short, I need to copy a LinearLayout with all its contents to an alertDialog, without me removing it from the main activity when I press it.
Main Activity, should not be changed
wanted but without removing LinearLayout from the main activity
Related
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 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);
I have a GridView in my activity. I am having 2 elements in the GridView. One is an ImageView and the other is a TextView.
I want to perform an action when clicking the ImageView only, but I want this to happen in the activity and not in the GridView adapter.
I handle the ImageView click in the getView() of my adapter, but I do not want it that way. I want to handle it in the activity when calling:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this, items));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//THIS WORKS FINE
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
});
//THE PROBLEM OCCURS HERE
The action is supposed to happen the first time I click the ImageView, but it only happens on the second click and further.
This is where I am stuck with the issue. I want this action to occur on the first click and not on the second click.
You can see the code block-
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String string = ((TextView) v.findViewById(R.id.text)).getText().toString();
Log.d("string",string);
//THE PROBLEM OCCURS HERE
ImageView capture = (ImageView) v.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
Your imageview capture is adding any action after gridview is being clicked. So, after clicking first time it is executing and then setting click listener to imageview and then next click its executing its onClick block. So better, handle imageview click event inside adapter.
You can call method inside your activity from adapter class but you should implement setOnClickListener inside your adapter class.
with the help of interface, you can do that do one thing create an interface and implement it on your activity and get imageview click on the adapter and here initiate the interface and you will get the click inside the activity
I think the problem is that on the first click you just set OnClickListener on your imageView, and so its onClick() method is not getting called. On the second click though, as the listener is already set, onClick() is invoked.
Instead of setting new Listener to your imageView each time an item in the grid clicked (which does not make any sense by the way), you should do either of these:
If imageView in each item has to be handled the same way, set OnClickListener to the imageView in the adapter, when creating a view.
If not, pass the interface for handling imageView clicks to the constructor of the adapter, and then, in the activity, implement this interface when creating an adapter.
Create a method in you activity.
Now, in adapter, onClick call that method using
((Activityname)context).methodname(parameters);
In the code below, the first TextView symbol cannot be resolved, and the findById method cannot be resolved. Can someone explain to me what the problem is and how I can fix it?
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String fact = "";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
You need to give more background. Are you in a fragment, or an activity? How was the layout set? I'm guessing the answer is that you need to be calling findViewById() on the View of your layout - e.g. view.findViewById() but it depends on how your layout was set.
If you're in an activity, calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If your layout was set a different way, then you need to get the View object of the layout and call findViewById() on it. If you're in a fragment, and you're in onCreateView() then the view has been passed in for you and you just need to call view.findViewById()
I am asuming that you want some text to set on text view on click of button try this
final TextView factLabel = (TextView)findViewById(R.id.factTextView);
Button showFactButton = (Button)findViewById(R.id.showFactButton);
showFactButton.setOnClickListener(new View.onClickListener()
{
#Override
public void onClick(View v) {
String fact = "Your Text Here";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
});
Make sure you are importing all the packages you need. Also make sure all the dependancies are working. Lastly make sure that there is a text view in the XML for the activity. Hooe some of this is helpful.
I just started learning android programming and can't seem to work out how to permit the user to create a selected number (n) of EditText fields by clicking a button n times such that each would be uniquely identified, namely 1,2,3...,n as well as accessible programatically, from a different method (invoked by a different button click). I hope the question is clear enough as I don't really have that much code to provide.
Do something like this
// A list to keep reference to your created edit texts
List<EditText> mEditTexts = new ArrayList<EditText>();
// Get root view of your activity
ViewGroup viewGroup = (ViewGroup) ((ViewGroup)
findViewById(android.R.id.content)).getChildAt(0);
// Get the button and set a click listener to it
Button mButton = (Button) findViewById(R.id.button_id);
mButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v){
// Build edit text
EditText mEditText = new EditText(v.getContext());
// Pass two args (arg1/arg2); must be LayoutParams.MATCH_PARENT,
// LayoutParams.WRAP_CONTENT, or an integer pixel value.
mEditText.setLayoutParams(new LayoutParams(arg1, arg2));
// Add the edit text to your list
mEditTexts.add(mEditText);
// Add edit text to your root view
viewGroup.addView(mEditText);
}
}
To check your edit text fields you can then access them from the list
for(EditText editText : mEditTexts){
Log.d(TAG, editText.getEditableText().toString());
}
or explicitly
int specificPosition = (SOME_INT);
EditText specificEditText = mEditTexts.get(specificPosition);
Haven't been able to test it so it might need some modifying but it should be something along those lines. You can also use your layout directly if you don't want to use the viewGroup. Modify it to something like
LinearLayout mLayout = (LinearLayout) findViewById(R.id.layout_id);
....
mLayout.addView(mEditText);