I have an activity over which contains imageview added dynamically on clicking a button. Over the activity i am inflating a dialogue box on click of the dynamically added image . Inside the dialog box i have a button to delete that image which is popping up that dialog .
My problem is how to access that imageview instance which is behind the dialog , so that i could delete it .
//Popping up the dialog on click of the imageview
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
customPotraitDialog = new CustomPotraitDialog(potrait);
customPotraitDialog.show();
//This i am doing on click of the button inside the dialog
dismiss();
View parent = (View) v.getParent();
RelativeLayout view = (RelativeLayout) parent.findViewById(R.layout.potrait);
deleteFromPotrait(view);
Potrait is my layout of the from where the dialog is popping up. It returning the view as null.
Pass instance of your activity to the dialog then onClick() deleteFromPotrait(view) method that is public in your activity like the following:
calling dialog
customPotraitDialog = new CustomPotraitDialog(potrait, context);
inside your dialog constructor:
public CustomPotraitDialog(potrait, context)
{
this.potrait = potrait;
this.context = context;
}
onClick():
((YourActivityName) context).deleteFromPotrait(view);
Related
I'm trying to use onClickListener but I keep getting Null exceptions.
This is the call to onClickListener -
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// code here
}
}
};
button1 = findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
'button1' is found on a different layout which opens (as a Dialog) when the user press on some other button.
What can I do to in this case?
Thanks! :)
You would have to inflate the layout with the dialogue's buttons.
for example, this is my method for displaying a popup.
I have named the method showPopup;
Dialogue ViewSview_student_dialogue;
view_student_dialogue = new Dialogue(getApplicationContext());
private void showStudentPopup(String name,String id_gender) {
TextView cancelpopup,st_name,id_and_gender;
view_student_dialogue.setContentView(R.layout.custom_student_popup);
cancelpopup = (TextView)view_student_dialogue.findViewById(R.id.cancel);
st_name = (TextView)view_student_dialogue.findViewById(R.id.st_name);
id_and_gender = (TextView)view_student_dialogue.findViewById(R.id.id_and_gender);
st_name.setText(name);
id_and_gender.setText(id_gender);
cancelpopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
view_student_dialogue.dismiss();
}
});
view_student_dialogue.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
view_student_dialogue.show();
}
Please take note that custom_student_popup is a layout file that i have created in the res layout folder.
Use layout inflator to inflate that view first then set listener.
The idea here is to get a reference to the view for your Dialog.
First, you inflate the xml layout for your Dialog:
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
You then set that as the view for your new Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(dialogLayout);
You can then get a reference to the Button from the inflated View and set you OnClickListener:
Button yourButton = (Button)dialogLayout.findViewById(R.id.yourbutton);
/// do the click listener assignment
Show your Dialog to the user:
builder.show();
The problem is MainActivity starts with a setContentView with a layout.xml. We can add buttons or anything to the layout and code in in the MainActivity class but when I try to code the buttons of another layout in the same Activity the app forces stop . Whats wrong ?
Ok I found out that is because of the context.
When you try to change other activity you have to use layoutinflater. Example below
LayoutInflater inflater = getLayoutInflater();
View myLayout = inflater.inflate(R.layout.my_layout, null);
To work with widgets inside it like buttons or anything .
Button b = mylayout.findViewById(R.id.button);
b.setText("Successfully changed");
Now you can use myLayout as your changed layout.
Please sent me your Activities
What the text of problem ?
You may write next code to go to another activity
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),nameActivity.class));
}
});
Where button is name of your button
See that you xml-file doesnt have any mistakes
You are getting a crash because you are trying to access the layout when it is not inflated. In other words, you must call setContentView() on an Activity, or inflater.inflate() on a Fragment to instantiate the view and make the elements accessible for manipulation. So if you want to add buttons to another Activity, you would need to call its onCreate() and setContentView() before you can add buttons to it.
EDIT: In response to comments...
In order to access/manipulate/modify elements in a layout at runtime, they must first be instantiated, which happens when the view is inflated. So to add a button to an Activity at runtime, you would do it in the onCreate() method after calling setContentView() like this:
Keep in mind this is the onCreate() of your SECOND activity...not your main Activity. So your main activity would start this Activity, then the button would get created during the setup of the second Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_second_activity;
Button button = new Button(this);
button.setText("Your New Button");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("NEW BUTTON", "I just clicked my new button!");
}
});
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.layout_in_your_second_activity);
relativeLayout.addView(button);
}
If you are using a Fragment to display your UI, you can't access your UI elements until you have inflated your layout, which happens in the onCreateView() method. So you would do something like this in your Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.container_layout_that_holds_button);
//You would get your context from an onAttach() Override
Button button = new Button(context);
button.setText("Your New Button");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("NEW BUTTON", "I just clicked my new button!");
}
});
relativeLayout.addView(button);
return view;
}
You're likely getting a NullPointerException when you try to manipulate your layouts before they are created. Keep in mind that even if you have an XML file with layouts specified within, the actual objects for those elements won't be created until the system decides it needs them, which happens when you actually try to display the view.
I dont know what path to take, I have a MainActivity class which host my ViewPagerTabStrip and each tabs are fragments, one tab consist of ListView and a button. When I click that button I want to either inflate a UI where the user fills in editfields or dialogFragment similiar process. Working with fragments what is best way to implement this? and How can I? I have set a onClickListener on the button and is working.
I have a method in my DBHelper that puts the users input into an SQLite database.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notes, container, false);
add = (Button) view.findViewById(R.id.addbtn);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "onClick works", Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
LayoutInflater inflater= getActivity().getLayoutInflater();
//this is what I did to added the layout to the alert dialog
View layout= inflarter.inflate(R.layout.alert_dialog,null);
alert.setView(layout);
final EditText titleInput=(EditText)layout.findViewById(R.id.dialog_title);
final EditText bodyInput=(EditText)layout.findViewById(R.id.dialog_body);
}
});
return view;
}
What do I call in the Fragment
Simplest way would be to use an AlertDialog with a custom layout, which has has some EditTexts. When the OK button in the dialog is clicked, get the values from the EditTexts, and put them in your database.
INTRODUCTION
I'm practicing with canvas-graphics on android. For this I created an app where I can write a line with the finger, and erase it.
When I click the color palette, a dialog window appears with some colors. So I have 2 layouts:
1- The main layout
2- The palette layout
QUESTION
First I got the color palette in the main activity, and for each color button I have an onClick which calls to a method on main Activity.
The thing is that now I'm not able to do this onClick functionality. I think that it has something to do with that now I start this layout as a view instead as a layout, so the onClick functionality of each button doesn't work
This is palette.xml layout's button example:
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:background="#FF660000"
android:onClick="paintClicked"
android:tag="#FF660000" />
When I click on each button, it starts the paintClicked method an passes the color tag.
So, how do I have to initiate the palette layout when i click on the palette button, to be able to have the information passing from this layout to the main activity?
UPDATE -- Current method to call palette.xml
final Dialog paletteDialog = new Dialog(this);
paletteDialog.setTitle("Colores:");
paletteDialog.setContentView(R.layout.palette);
LinearLayout paletteLayout = (LinearLayout) paletteDialog.findViewById(R.id.paint_colors);
bnColor = (ImageButton) paletteLayout.getChildAt(0);
bnColor.setImageDrawable(getResources().getDrawable(R.drawable.button_pressed));
paletteDialog.show();
Logcat:
java.lang.NoSuchMethodException: paintClicked [class android.view.View]
This is the paintClicked method reference:
public void paintClicked(View view){
I suggest you look at DialogFragment (if you aren't using it already). You can create a simple callback interface so that when a color is clicked, it calls back to the enclosing Activity with this information. Make the enclosing Activity implement the interface (enforced by the onAttach lifecycle method below) and have the dialog fragment call it when a color is picked.
public class ColorPickerDialog extends DialogFragment implements View.OnClickListener {
// Define a callback interface
public interface OnColorSelectedListener {
public void onColorSelected(int color);
}
private OnColorSelectedListener listener;
#Override
public void onAttach(Activity activity) {
if (!(activity instanceof OnColorSelectedListener)) {
throw new IllegalStateException("Activity must implement OnColorSelectedListener!");
}
listener = (OnColorSelectedListener) activity;
}
#Override
public void onDetach() {
listener = null;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.palette, container, false);
// call view.findViewById(...) for all your color buttons and
// set the OnClickListener
view.findViewById(...).setOnClickListener(this);
...
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Colores:");
builder.setView(view);
return builder.create();
}
#Override
public void onClick(View v) {
/*
* Determine the color from the view that was clicked. You can use a
* switch statement on v.getId() if they all have IDs, but there are
* other possibilities as well.
*/
int color = ...;
listener.onColorSelected(color);
dismiss();
}
}
android:onClick only works if the Context which expanded the view contains the function ("paintClicked" here).
So if you didn't expand this layout with your main Activity it could the reason it doesn't work.
It might be better for you to set Ids to the ImageButtons and use some findViewById(R.id.btnX).setOnClickListener(myListener); after displaying the buttons.
In my android app I have a list and in each row I have a button. On pressing the button, another activity should open. I am a little confused how to do the click listener. Can anyone kindly suggest ? Thanks.
note: i can create a click listened inside the array adapter. However, I am unable to start a new activity from there :(
Put a button in your custom view and handle click event in getView method.
Your code should look something like this.
public View getView(final int position, View convertView,ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
}
Button yourButton= (Button) convertView .findViewById(R.id.YOUR_BUTTON_ID);
yourButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
return convertView ;
}
Hope this helps.
where ever you are inflating the row view, get the reference to the button in the listItem, and add clickListener to it. You set the listener by
button.setOnClickListener()
and in the listener click call new activity.
declare a field your activity class like this-
private Context mCurrentContext = this;
and when you call the new Activity,
mCurrentContext.startActivity(Intent, int);