How to make modification Android Snackbar containing with LayoutInflater button and editText - android

My project now is how to make a Snackbar displayed at the bottom of the screen. The Snackbar contain with editText and three button for selection categories.
I use LayoutInflater and follow these code for my script, and it's work. My problem now is, how to get response when user click the button ?
Here my code
#Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
layout.addView(snackView, 0);
snackbar.show();
}
and here my snacbar.xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="#+id/event_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:hint="#string/type_your_message"
android:inputType="textNoSuggestions"
android:maxLines="4"
android:singleLine="false"
android:textSize="18sp"
android:paddingLeft="4dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_mapel"
android:onClick="mapel"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_image_edit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_image_edit" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_image_edit" />
</LinearLayout>

here is the source required to implement button listener to your snackbar custom layout.
#Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
// button id for snackbar
Button button_mapel = (Button) snackView.findViewById(R.id.btn_mapel);
// perform button click listener
button_mapel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here perform your button click task
}
});
layout.addView(snackView, 0);
snackbar.show();
}

Follow this documentation
https://developer.android.com/training/snackbar/action.html
public class MyUndoListener implements View.OnClickListener{
&Override
public void onClick(View v) {
// Code to undo the user's last action
}
}
Now for setAction()
Snackbar mySnackbar = Snackbar.make(findViewById(R.id.myCoordinatorLayout),
R.string.email_archived, Snackbar.LENGTH_SHORT);
mySnackbar.setAction(R.string.undo_string, new MyUndoListener());
mySnackbar.show();

You have to get id of your Button. And have to set clickListener.
You have to just replace your code with this.
I have put listener on btn_mapel, you can put any other button according to your requirement.
Try this.
#Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
Button mapelBt = (Button)snackView.findViewById(R.id.btn_mapel);
mapelBt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get your button click callback here
}
});
layout.addView(snackView, 0);
snackbar.show();
}

Try This,
Button btn_feed_back = (Button)snackView.findViewById(R.id.feedback);
btn_feed_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Related

How to trigger radioButton to checked if I pressed on Linear layout

How can I make the radio button get selected if I pressed on other place ( not the radio button ) but on image, text view and etc inside 1 Linear layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/card_balance"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_vertical"
android:paddingLeft="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:textColor="#color/dc_white" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:paddingRight="20dp">
<RadioButton
android:id="#+id/radio_button_payment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>

</RelativeLayout>
And here is the adapater: PaymentAdapter.java
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View allView = inflater.inflate(R.layout.item_choose_payment, parent, false);
TextView txt1 = (TextView) allView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) allView.findViewById(R.id.card_balance);
final RadioButton r = (RadioButton) allView.findViewById(R.id.radio_button_payment);
LinearLayout layout_card = (LinearLayout) allView.findViewById(R.id.layout_1);
r.setChecked(position == selectedPosition);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});
}
Right now the condition is I can pressed only at radio button.
I've already tried to add onClickListener to linear layout, and yes I can pressed on the linear layout however the Radio button not select only 1 but all selected.
this code for linearLayout onClick
layout_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
r.setChecked(position == selectedPosition);
r.setTag(position);
if (r.isChecked()) {
r.setChecked(true);
r.setSelected(true);
} else if (!r.isChecked()) {
r.setChecked(false);
r.setSelected(false);
}
}
});
Sorry for my bad explanation, any help is very grateful for me.
Thank you
inside your getView(...), write
layout_card.setTag(position);
and change click listener as
layout_card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition = Integer.parseInt(v.getTag().toString());
notifyDataSetChange();
}
});
remove below code, if not needed
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
selectedPosition = (Integer) view.getTag();
notifyDataSetChanged();
}
});

Create popup window for each gridview item - Android

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;
}
}

insert a new TextView in a dialog box after pressing on a button in the dialog box

i have a custom dialog box, it has a textview an edittext and 2 buttons. i'm trying to create a new textview that containes the text of the edittext everytime the user presses on the 'add' button,and locate it beneath the buttons. how can i do that? here's what i tried:
ingDlgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//showDialog(INGS_DIALOG);
final Dialog customIngsDlg = new Dialog(context);
customIngsDlg.setContentView(R.layout.custom_ings_dialog);
customIngsDlg.setTitle("Ingredients");
TextView ingsDlgTitle = (TextView)findViewById(R.id.ingsDialogTitleTv);
final EditText ingsEt = (EditText)customIngsDlg.findViewById(R.id.ingsDialogEt);
final Button ingsAddBtn = (Button)customIngsDlg.findViewById(R.id.ingsAddButton);
final Button ingsFinishBtn = (Button)findViewById(R.id.ingsFinishButton);
ingsAddBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ArrayList<String> ingsTmp = new ArrayList<String>();
String ingredient = ingsEt.getText().toString();
ingredients.add(ingredient);
ingsEt.setText("");
TextView ingItemTv = new TextView(customIngsDlg.getContext());
ingItemTv.setText(ingredient);
RelativeLayout layout = new RelativeLayout(OwnRecipeAdding.this);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
parms.addRule(RelativeLayout.BELOW,ingsEt.getId());
layout.setLayoutParams(parms);
customIngsDlg.addContentView(ingItemTv,parms);
}
});
// ingsFinishBtn.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v) {
//
// customIngsDlg.dismiss();
// }
// });
customIngsDlg.show();
}
});
I would suggest adding the text view to your custom layout in the .xml file and then add text to that when the add button is pressed.
This needs a bit of styling, but it's functional. You'll likely need a scroll view in there when the list becomes too long.
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog_fragment_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/dialog_textview_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Add Ingredients" />
<EditText
android:id="#+id/dialog_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left" />
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button_add"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<Button
android:id="#+id/button_finish"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Finish" />
</LinearLayout>
<TextView
android:id="#+id/dialog_textview_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="" />
</LinearLayout>
DialogFragment class:
public class TestDialogFragment extends DialogFragment {
private Button buttonAdd;
private Button buttonFinish;
private TextView addTextView;
private EditText editText;
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.dialog_fragment, null);
buttonAdd = (Button)dialoglayout.findViewById(R.id.button_add);
buttonFinish = (Button)dialoglayout.findViewById(R.id.button_finish);
editText = (EditText)dialoglayout.findViewById(R.id.dialog_edittext);
addTextView = (TextView)dialoglayout.findViewById(R.id.dialog_textview_2);
buttonAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String text = addTextView.getText().toString();
text = text + "\n" + editText.getText().toString();
addTextView.setText(text);
editText.setText("");
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout)
.setTitle("Ingredients")
.setCancelable(false)
.setInverseBackgroundForced(true);
AlertDialog dialog = builder.create();
return dialog;
}
}

view.findViewById(....) always return null in dialog

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.

How To make Buttons Clickable in ListView Header

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

Categories

Resources