I have:
popUp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
....
<Button
android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"
android:gravity="left"
/>
</LinearLayout>
main.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
popUp();
}
});
and I get force close here
public void popUp(){
LayoutInflater inflater = (LayoutInflater) project.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup,null, false),300,400,true);
ok_button = (Button) findViewById(R.id.ok_button);
pw.showAtLocation(findViewById(R.id.list), Gravity.BOTTOM, 0,10);
ok_button.setOnClickListener(new OnClickListener() { //here I get FC
#Override
public void onClick(View v) {
pw.dismiss();
}
});
}
}
When I use button (in popUp();) from main.xml instead of popUp.xml everything is working.
What is wrong with using the button from not main.xml
I think that the problem is here:
ok_button = (Button) findViewById(R.id.ok_button);
it is looking for the button view inside the main layout instead of looking inside the popup layout. So probably ok_button is null.
Try to move out the inflate call from the constructor, like this:
View v = inflater.inflate(R.layout.popup,null, false);
final PopupWindow pw = new PopupWindow(v,300,400,true);
and then:
ok_button = (Button) v.findViewById(R.id.ok_button);
Related
I'm developing an Android application for a restaurant for an food ordering system.
In my project,I have a Gridview ,When click on a button on the item in gridview, I want to get a popup window with some textfields,buttons etc.
I got help from this document example
the above document example is working alone as single application.it's just an example for creating basic pop-up window.but I need it on Gridview.
my work -->
instead of above doc's main.xml and PopUpWinndowDemoActivity.java (it's main activity),
I use my grid_single.xml and it's implementation file Grid_single.java.
But it doesn't work for me.When I click on the each item's button,not showing the pop-up window as i hope. So, Please help me to solve this or give me a working example for pop-up window on grid view items.
I have attached my files with this.
waiting for your help,
Thank you!
Grid_single.java
Button btnClosePopup;
Button btnCreatePopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_single);
btnCreatePopup = (Button) findViewById(R.id.addToCart);
btnCreatePopup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initiatePopupWindow();
}
});
btnCreatePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
initiatePopupWindow();
}
});
}
private PopupWindow pwindo;
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) Grid_Single_Popup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
grid_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:padding="5dp"
android:id="#+id/grid_single_back"
>
<ImageView
android:id="#+id/grid_image"
android:layout_width="150dp"
android:layout_height="150dp">
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="24dp" >
</TextView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Cutomize & Add"
android:id="#+id/addToCart"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:textSize="18dp"
android:padding="5sp"/>
</LinearLayout>
Thanks in advance
This is working for me, check once
In grid adapter:
public CustomGrid(String[] web, int[] Imageid, Grid_single activity) {
this.Imageid = Imageid;
this.web = web;
this.activity = activity;
}
In getView method of your adapter, keep like this:
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
Button btnPopup = (Button) grid.findViewById(R.id.btnPopup);
btnPopup.setOnClickListener(activity);
Grid_single Should implement OnClickListener, and keep like this:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPopup:
initiatePopupWindow(web[grid.getPositionForView(v)]);
break;
default:
break;
}
}
I have a dialog where I wish to force the user to fill in a name into an EditText, if it's empty I wish for pressing done to have no effect. However,
EditText x = (EditText) view.findViewById(R.id.name);
always returns null. I have tried searching but have only found the solution to add a view to findViewById(...) but I already have that. My guess it that it has to do with me creating my own version of the button.
This is the code for the Dialog.
package com.example.dialogruta;
#SuppressLint({ "NewApi", "ValidFragment" })
public class RecordDialog extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(view).setPositiveButton("Save experiment", null); //Creating my own
final Dialog d = builder.create();
d.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
Button b = ((AlertDialog) d).getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText x = (EditText) view.findViewById(R.id.name);
if( x!= null && !x.toString().isEmpty()){
String xs = x.getText().toString();
d.dismiss();
}
else{
//TODO: Stuff
}
}
});
}
});
return d;
}
}
And this is the .xml-file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/name"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Experiment name" />
<EditText
android:id="#+id/notes"
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_weight ="1"
android:lines="5"
android:minLines="1"
android:gravity="top|left"
android:maxLines="10"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Notes" />
</LinearLayout>
Try this..
Change View name
final View main_view = inflater.inflate(R.layout.record_fragment, null);
builder.setView(main_view).setPositiveButton("Save experiment", null);
and use it
EditText x = (EditText) main_view.findViewById(R.id.name);
because in your public void onClick(View view) { onclick has view that edit text need to refer inflated view. Not onclick view.
Or
As like #Raghunandan said change onclick view name as public void onClick(View v) {
Change this
public void onClick(View view) {
to
public void onClick(View v) {
You have
final View view = inflater.inflate(R.layout.record_fragment, null);
and this
public void onClick(View view) {
Both View view.
What happens here
#Override
public void onClick(View view) {
EditText x = (EditText) view.findViewById(R.id.name);
EditText is not a child of button and view.findViewById looks for a view in the current view hierarchy. It should look for the view in the inflated layout final View view = inflater.inflate(R.layout.record_fragment, null);
change local view name in onClick() method.
public void onClick(View v)
Declare the variable as private at the onCreate method beginning and access there.
EditText x = (EditText) findViewById(R.id.name);
Now you will get the value of x in onClick() method.
I have two buttons in ListView Header and I want to detect button is clicked from the header. how can I do this..
here is my code:
header_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b2" />
</LinearLayout>
in java code I have done this
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
You can add listener to these buttons.
btnB1 = (Button) mTop.findViewById(R.id.b1);
btnB1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
})
LayoutInflater inflater = LayoutInflater.from(this);
View mTop = inflater.inflate(R.layout.header_list, null);
listview.addHeaderView(mTop);
Button _btnb1 = (Button) mTop.findViewById(R.id.b1);
_btnb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
Button _btnb2 = (Button) mTop.findViewById(R.id.b2);
_btnb2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
Try this code
I've created a ListView with a list of items, each with their own delete button.
I've overridden the getView method in the ArrayAdapter and attached a handler to the delete button.
The problem is, the getView method is being called twice. On the second time, the v.findViewById(DeleteButton) call returns null. I've inspected the view and the ImageButton is there in the hierarchy, but the Id is 0.
Can anyone explain why this is happening please?
ManualFragment.java
public class ManualFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.fragment_manual, container, false);
Button addButton = (Button) view.findViewById(R.id.AddButton);
ListView listView = (ListView) view.findViewById(R.id.ListView);
final List<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), R.layout.list_item, R.id.TextItem, items) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) ManualFragment.this.getLayoutInflater(savedInstanceState);
v = vi.inflate(R.layout.list_item, null);
}
TextView time = (TextView) v.findViewById(R.id.TextItem);
time.setText(items.get(position));
ImageButton delete = (ImageButton) v
.findViewById(R.id.DeleteButton);
//THIS LINE THROWS A NULLREF ON SECOND CALL
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ImageButton deleteButton = (ImageButton) v;
items.remove(deleteButton.getId());
notifyDataSetChanged();
}
});
delete.setId(position);
return v;
}
};
listView.setAdapter(adapter);
final EditText addText = (EditText) view.findViewById(R.id.AddText);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String value = addText.getText().toString();
if (!"".equals(value)) {
items.add(value);
adapter.notifyDataSetChanged();
addText.setText("");
}
}
});
return view;
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusable="false" >
<TextView
android:id="#+id/TextItem"
android:layout_width="267dp"
android:layout_height="match_parent"
android:layout_weight="0.89"
android:focusable="false"
android:gravity="center_vertical"
android:text="Item"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="#+id/DeleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:focusable="false"
android:src="#drawable/delete" />
</LinearLayout>
</LinearLayout>
You're setting it with
delete.setId(position)
I think you wanted to tag the buttojn with the position
delete.setTag(position)
I have an app with fragments working ok,
I have a view with buttons, when a button gets tapped, a popup view is showed,
but I need to set different text on the pop up view for each button that is pressed.
Im new to android and java and just realised I don't understand how to send the data to the pop up view, to set the text on the xml for the pop up view,
public class Tab2HeadH1 extends Fragment implements OnClickListener{
//testeo popero
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
final Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(buttonNose, 50, 30);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
and the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#android:color/background_light">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="1dp"
android:background="#android:color/darker_gray">
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's a PopupWindow" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dismiss" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
So how can I set the text of the textView,
Thanks a lot!
you need to find your textView in popup layout and set popup text
TextView text = popupView.findViewById(R.id.popup_text);
text.setText(your text);