use Onclick in extends PagerAdapter - android-studio

Hi I’m trying to create a horizontal slide where each horizontal page will have a button that will call a Dialog but onClick is not working in extends Pageradapter can anyone tell me what I’m doing wrong?
I gave the name onClickApprove to onClick that I want you to call Dialog. This Dialog is calling an Activity with a Ratingbar to make an assessment.
Thank you.
public class SliderAdapterUsado extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
BottomSheetDialog dialog;
Button show;
public SliderAdapterUsado(Context context){
this.context = context;
}
public String[] slide_rota ={
"Titulo1",
"Titulo2"
};
public String[] slide_nome={
"Descrção do titulo 1",
"Descrção do titulo 2"
};
#Override
public int getCount(){
return slide_rota.length;
}
#Override
public boolean isViewFromObject(View view, Object object){
return view == (LinearLayout) object;
}
#Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout_usados, container, false);
TextView slideHeading = view.findViewById(R.id.slide_rota);
TextView slideDescricao = view.findViewById(R.id.slide_nome);
slideHeading.setText(slide_rota[position]);
slideDescricao.setText(slide_nome[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
public void onClickAvaliar(View view) {
show = view.findViewById(R.id.show);
dialog = new BottomSheetDialog(dialog.getContext());
show.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog();
dialog.show();
}
});
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
private void createDialog(){
View view = layoutInflater.inflate(R.layout.activity_rating, null, false);
Button FeedBack = view.findViewById(R.id.FeedBack);
FeedBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.setContentView(view);
}
}

Please read the documentation.
Note this in particular (emphasis mine):
To define the click event handler for a button, add the android:onClick attribute to the element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
The expectation is that the value set on the onClick xml attribute will be a method on the hosting Activity. Yours is in the adapter. That's why it doesn't work.
So either move that method to the hosting activity, or just handle the click event explicitly:
...
TextView slideHeading = view.findViewById(R.id.slide_rota);
TextView slideDescricao = view.findViewById(R.id.slide_nome);
Button slideButton = view.findViewById(R.id.slide_button);
slideButton.setOnClickListener (...)
...

Related

Cannot create dialog inside a fragment

I am trying to create a dialog inside a fragment.when I am trying to press on the button and enter the dialog the app collaspe.
I guess the code is not right can you please help me with that?
Here is my code:
private void openDialog(){
Dialog dialog=new Dialog(getContext());
//AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
LayoutInflater layoutInflater=this.getLayoutInflater();
View custom_dialog=getActivity().getLayoutInflater().inflate(R.layout.geo_dialog,null);
dialog.setContentView(custom_dialog);
// add_geofence_radius= custom_dialog.findViewById(R.id.radius_size);
save_btn=custom_dialog.findViewById(R.id.save_btn);
cancel_btn=custom_dialog.findViewById(R.id.cancel_btn);
/* save_btn.setOnClickListener(new View.OnClickListener() {
#Override
}
});
*/
/*cancel_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
*/
// dialog.setTitle("hello");
dialog.show();
}
The best way to show dialog in android is to use "DialogFragments" since they are aware of the lifecycle of the view it is attached on (ie. fragments/activities).
Here is an examples provided in Android docs:
public class PurchaseConfirmationDialogFragment extends DialogFragment {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {} )
.create();
}
public static String TAG = "PurchaseConfirmationDialog";
}
To show the dialog use:
new PurchaseConfirmationDialogFragment().show(
getChildFragmentManager(), PurchaseConfirmationDialog.TAG);
For more reference on dialogFragments, checkout : Create a DialogFragment

Cannot close a custom AlertDialog

so I have an app with three different buttons(parent buttons) on pressing any of them, the custom alert dialog with 9 different buttons is displayed but the functions of these 9 buttons within the alert dialog are different depending on which of the three parent buttons called it. On pressing any of the 9 buttons, I want the app to perform a particular function and then close the alertdialog. Now the problem is that I can easily call the alert dialog by calling the method showcustomdialog(); that I created but I can't dismiss it using alertdialog.dismiss(); inside the OnClickListener of the parent button since the method has a void result type. I've tried using if-else statements but it doesn't work. How can I achieve what is required?
The method:
private void showCustomDialog() {
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_main2, viewGroup, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
I am calling the meathod and using it as follows:
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCustomDialog();
alertbutton1.getId();
alertbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("500");
function();
//I want to dismiss the alertdialog here.
}
});
alertbutton2.getId();
alertbutton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("1000");
function();
//I want to dismiss the alertdialog here.
}
});
and so on.
I figured out a solution for you using AlertDialog.
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog();
}
});
Then the method MUST be a private or public AlertDialog:
private AlertDialog createDialog() {
LayoutInflater inflater = (getActivity()).getLayoutInflater(); // for fragment or getLayoutInflater(); for activity
View v = inflater.inflate(R.layout.dialog_add_new_list, null);
Button okButton = v.findViewById(R.id.confirm_button);
Button cancelButton = v.findViewById(R.id.cancel_button);
final AlertDialog dialog = new AlertDialog.Builder(getActivity()) // for fragment or AlertDialog.Builder ( this ) for activity
.setView(v)
.show();
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p1) {
dialog.dismiss();
}
});
return dialog;
}
P.S. Replace the ids I used to create this sample with yours.

How do I put the adapter into the viewpager?

This is the code of MyPageAdapter
public class MyPageAdapter extends PagerAdapter {
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13
, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23, R.drawable.img24
, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29, R.drawable.img30};
private LayoutInflater inflater;
private Context context;
public MyPageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == ((LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_slideshow, container, false);
EditText editText = v.findViewById(R.id.editTextNumberDecimal);
ImageView imageView = (ImageView)v.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
int text = position+1;
editText.setText(text);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
}
AND this is the code of SlideshowFragment.
public class SlideshowFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
ViewPager2 viewPager2 = (ViewPager2) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager2.setAdapter(adapter);
return root;
}
}
I would like to add a view pager to the slide fragment so that 30 photos can be viewed as a slide show.
But I can't do setAdapter in viewpager.
Really, is there anything other than a way to make and put each fragment for each of the 30 pictures?
Please let me know how to modify my code.
You are mixing Viewpager and Viewpager2 these are implemented in 2 totally different ways.
try
ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager.setAdapter(adapter);
and change your xml to also be a ViewPager widget.
Alternatively you could probably change your adapter to a RecyclerView.Adapter https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
As Viewpager2 is just a customised RecyclerView.

setLayout not inflating correct xml layout?

been alright so far. I try to use docs or find my issues.
However my mainActivity has 4 buttons. These buttons depending on which view is clicked passes the R.id.button. I pass this value into an intent which Activity2 uses bundle to get id integer. I then setContentView using this.
However im having issues getting correct view to inflate now, it seems like its loading an old layout. My aim is to inflate 3 separate layout on Activity2 depending which button was clicked.
I know my other other button does inflate another activity and get proper xml layout.
Why are my other 3 buttons not inflating on Activity2 properly?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button pizza;
Button hamburger;
Button icecream;
Button history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizza = findViewById(R.id.buttonPizza);
hamburger = findViewById(R.id.buttonBurger);
icecream = findViewById(R.id.buttonIceCream);
history = findViewById(R.id.buttonHistroy);
pizza.setOnClickListener(this);
hamburger.setOnClickListener(this);
icecream.setOnClickListener(this);
history.setOnClickListener(this);
}
#Override
public void onClick(View v){
int idView = v.getId();
switch(v.getId()) {
case R.id.buttonBurger : startOrderActivity(R.layout.burger_layout);
makeToast(idView);
break;
case R.id.buttonPizza : startOrderActivity(R.layout.pizza_layout);
makeToast(idView);
break;
case R.id.buttonIceCream : startOrderActivity(R.layout.icecream_layout);
makeToast(idView);
break;
case R.id.buttonHistroy : startOrderActivity(R.layout.history_layout);
makeToast(idView);
break;
}
}
private void startOrderActivity(int id){
if(id != R.layout.history_layout) {
Intent intentOrder = new Intent(this, MakeOrderActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
else {
Intent intentOrder = new Intent(this, OrderHistoryActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
}
public void makeToast(int id){
String txt = String.valueOf(id);
Toast newToast = Toast.makeText(getApplicationContext(),txt,Toast.LENGTH_LONG);
newToast.show();
}
}
public class MakeOrderActivity extends AppCompatActivity {
Button placeOrderButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getIntent().getExtras();
int layoutNum = data.getInt("layout");
setContentView(layoutNum);
placeOrderButton = findViewById(R.id.placeOrder);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.order, menu);
return true;
}
public void onCheckboxClicked(View view){
}
}
Answer is to create a View which is created by getInflator.
https://stackoverflow.com/a/17070408/8443090

How to send data in ListView dynamicaly in android.?

My question is how do i send data written in EditText of dialogue box by clicking button and display it on LIstView of Main Activity. ?
public class TaskDetailsActivity extends Activity implements OnClickListener{
String[] timepass= new String[100];
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.taskdetails);
//timepass[0] = "sidd";
/*ListView tasklist= (ListView)findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.insideaddtask, R.id.tp, timepass);
tasklist.setAdapter(adapter);*/
}
public void addnewtask(View view)
{
showDialog(1);
}
#Override
protected Dialog onCreateDialog(int id)
{
Dialog dialog=null;
switch (id)
{
case 1: dialog = new Dialog(TaskDetailsActivity.this);
dialog.setContentView(R.layout.addtask);
Button task_add_ok = (Button)findViewById(R.id.btn_ok);
task_add_ok.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
EditText writetask = (EditText)findViewById(R.id.txt_writetask);
String data = writetask.getText().toString();
timepass[0] = data;
}
});
break;
default:
break;
}
return dialog;
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
}
you can use any adapter for this arrayadapter of type string...
and use the button's onclick method to populate the listview...
below link provides a good example...
http://android.amberfog.com/?p=296
www.androidhive.info/2011/10/android-listview-tutorial/

Resources