I'm trying to have a button appear only on the last page of a ViewPager. I have the working button but it is displayed on all the pages instead of only the last page. I want to have it visible only on the last page. Here is my code.
activity_welcome2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".Welcome_Activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--- for dots -->
<View
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha=".5"
android:layout_above="#+id/dotsLayout"
android:background="#color/white"
android:layout_marginBottom="15dp" />
<LinearLayout
android:id="#+id/dotsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginBottom="55sp" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/next3"
android:layout_width="99dp"
android:layout_height="45dp"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.822"
app:srcCompat="#drawable/next" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
Welcome_Activity.java
package com.group7.salitongue;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
public class Welcome_Activity extends AppCompatActivity {
private ViewPager mPager;
private int[] layouts = { R.layout.activity_main, R.layout.activity_splashscreen2, R.layout.activity_welcome };
private MpagerAdapter mpagerAdapter;
private LinearLayout Dots_Layout;
private ImageView[] dots;
private ImageButton nextBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome2);
mPager = (ViewPager) findViewById(R.id.viewPager);
mpagerAdapter = new MpagerAdapter(layouts, this);
mPager.setAdapter(mpagerAdapter);
Dots_Layout = (LinearLayout) findViewById(R.id.dotsLayout);
createDots(0);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {
createDots(position);
}
#Override
public void onPageScrollStateChanged(int state) {}
});
// transition button to Signup page
nextBtn = (ImageButton) findViewById(R.id.next3);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Welcome_Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
private void createDots(int current_position) {
if(Dots_Layout != null)
Dots_Layout.removeAllViews();
dots = new ImageView[layouts.length];
for (int i = 0; i < layouts.length; i++) {
dots[i] = new ImageView(this);
if (i == current_position) {
dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.active_dots));
}
else {
dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.default_dots));
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(4, 0, 4, 0);
Dots_Layout.addView(dots[i],params);
}
}
}
How can I display the button only when the user has reached the last page of swiping?
Just listen in the onPageSelected method of your OnPageChangeListener when the last page is reached and set the button to visible. In all other cases, just hide it.
#Override
public void onPageSelected(int position) {
createDots(position);
// only show the button when the last page is reached
if (position == mPagerAdapter.getCount() - 1)
nextBtn.setVisibility(View.VISIBLE);
else
nextBtn.setVisibility(View.GONE);
}
Related
Spinner has data but doesn't have any preview text/selected text I try various things and still doesn't show any text
Im trying to use spinner for some data and I successfully populated some list for spinner data but it doesn't show any in the spinner.
I also try various things i saw from the net and it didnt work.
Here is the link where I based my code for spinner: https://www.youtube.com/watch?v=j7P5k-dHS9Y
JAVA CODE:
public class SpinnerSample extends AppCompatActivity {
Spinner spinner;
boolean isSpinnerTouched = false;
DatabaseReference databaseReference;
String[] status = {"Not Available", "Available", "Under Maintenance"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_sample);
spinner = findViewById(R.id.spiiner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(SpinnerSample.this, R.layout.style_spinner, status);
adapter.setDropDownViewResource(R.layout.style_spinner);
spinner.setAdapter(adapter);
databaseReference = FirebaseDatabase.getInstance().getReference();
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
isSpinnerTouched = true;
return false;
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String value = adapterView.getItemAtPosition(i).toString();
if (!isSpinnerTouched) {
Toast.makeText(SpinnerSample.this, "No Selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SpinnerSample.this, value, Toast.LENGTH_SHORT).show();
}
}
});
}
}
XML CODE:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SpinnerSample">
<Spinner
android:id="#+id/spiiner"
android:layout_width="240dp"
android:background="#ff0"
android:layout_height="40dp"
android:textColor="#000"
android:hint="Current Password"
android:layout_marginTop="10dp"/>
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:text="A"
android:padding="20dp"
android:layout_height="match_parent"
android:textColor="#000"
android:textColorHint="#1e7591">
</TextView>
I followed this tutorial on youtube for my school project. The app is able to display selected images from gallery but the recycler view have gaps in every row. What do I code to fix this app? help. enter image description here
here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/pick"
android:layout_width="53dp"
android:layout_height="64dp"
android:layout_margin="12dp"
android:layout_weight="0"
android:backgroundTint="#FF5722"
android:text="+"
android:textSize="24sp"
app:cornerRadius="64dp" />
<TextView
android:id="#+id/totalPhotos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:textColor="#FFFFFF" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView_Gallery_Images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
</LinearLayout>
RecyclerAdapter.java
package com.example.mygram;
import android.media.Image;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private ArrayList<Uri> uriArrayList;
public RecyclerAdapter(ArrayList<Uri> uriArrayList) {
this.uriArrayList = uriArrayList;
}
#NonNull
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.custom_single_image,parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecyclerAdapter.ViewHolder holder, int position) {
holder.imageView.setImageURI(uriArrayList.get(position));
}
#Override
public int getItemCount() {
return uriArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image);
}
}
}
Custom single images.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here's the MainActivity.java
package com.example.mygram;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
TextView textView;
Button pick;
ArrayList <Uri> uri = new ArrayList<>();
RecyclerAdapter adapter ;
private static final int Read_Permission = 101;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.totalPhotos);
recyclerView = findViewById(R.id.recyclerView_Gallery_Images);
pick = findViewById(R.id.pick);
adapter = new RecyclerAdapter(uri);
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
recyclerView.setAdapter(adapter);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Read_Permission);
}
pick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
if(data.getClipData() !=null) {
int x = data.getClipData().getItemCount();
for (int i = 0; i < x; i++) {
uri.add(data.getClipData().getItemAt(i).getUri());
}
adapter.notifyDataSetChanged();
textView.setText("Photos ("+uri.size()+")");
}else if (data.getData()!=null){
String imageURL=data.getData().getPath();
uri.add(Uri.parse(imageURL));
}
}
}
}
The concept of the app is instagram feed tester. I want to be able to display images like the grid in instagram but the gap is appearing preventing it to happen. help me please thankyou
In your Custom single images.xml replace the height of the parent layout to wrap_content. Replace your code with the following
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Everything is right : Just need to Change this to this: (images.xml)
android:layout_height="match_parent"
to
android:layout_height="wrap_content"
in your model xml file
Here a beginner at Android studio.
I'm creating a simple agenda app, where you can add, edit, delete and search contacts.
I used a recycle view but after adding the search option, the recycle view just shows me one contact.
The contacts are being added as appear when I search for them.
Any idea on how to fix it?
It tried changing the layout but nothing works.
My main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/txtBuscar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listacontactos"
android:layout_width="match_parent"
android:layout_height="591dp" />
</LinearLayout>
</LinearLayout>
Here is the Adapter: https://github.com/soymartanegro/agendaApp/blob/main/src/main/java/com/example/agenda/adaptadores/ListaContactosAdapter.java
Main Java: https://github.com/soymartanegro/agendaApp/blob/main/src/main/java/com/example/agenda/MainActivity.java
Thanks a lot.
I have used the below code it's working perfectly. Please check it. Currently, I'm using dummy data for testing purposes.
activity_main.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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<SearchView
android:id="#+id/txtBuscar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listacontactos"
android:layout_width="match_parent"
android:layout_height="591dp" />
</LinearLayout>
</LinearLayout>
lista_item_contacto.xml
<?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="wrap_content">
<TextView
android:id="#+id/viewNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
contactos
public class contactos {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ListaContactosAdapter.java
public class ListaContactosAdapter extends RecyclerView.Adapter<ListaContactosAdapter.ContactoViewHolder> {
ArrayList<contactos> listaContactos;
ArrayList<contactos> listaOriginal;
public ListaContactosAdapter(ArrayList<contactos> listaContactos){
this.listaContactos = listaContactos;
listaOriginal = new ArrayList<>();
listaOriginal.addAll(listaContactos);
}
#NonNull
#Override
public ContactoViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lista_item_contacto, null , false);
return new ContactoViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ContactoViewHolder holder, int position) {
holder.viewName.setText(listaContactos.get(position).getName());
}
public void filtrado(String txtBuscar){
int longitud = txtBuscar.length();
if (longitud == 0){
listaContactos.clear();
listaContactos.addAll(listaOriginal);
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
List<contactos> collecion = listaContactos.stream().filter(i->i.getName().toLowerCase().contains(txtBuscar.toLowerCase()))
.collect(Collectors.toList());
listaContactos.clear();
listaContactos.addAll(collecion);
} else {
for(contactos c: listaOriginal){
if (c.getName().toLowerCase().contains(txtBuscar.toLowerCase())){
listaContactos.add(c);
}
}
}
}
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return listaContactos.size();
}
public class ContactoViewHolder extends RecyclerView.ViewHolder {
TextView viewName, viewTelephone, viewEmail;
public ContactoViewHolder(#NonNull View itemView) {
super(itemView);
viewName = itemView.findViewById(R.id.viewNombre);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Context context = view.getContext();
}
});
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{
SearchView txtBuscar;
RecyclerView listaContactos;
ArrayList<contactos> listaArrayContactos;
ListaContactosAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtBuscar = findViewById(R.id.txtBuscar);
listaContactos = findViewById(R.id.listacontactos);
listaContactos.setLayoutManager(new LinearLayoutManager(this));
listaArrayContactos = new ArrayList<>();
contactos c1 = new contactos();
c1.setName("Hello");
contactos c2 = new contactos();
c2.setName("Android");
contactos c3 = new contactos();
c3.setName("Studio");
listaArrayContactos.add(c1);
listaArrayContactos.add(c2);
listaArrayContactos.add(c3);
adapter = new ListaContactosAdapter(listaArrayContactos);
listaContactos.setAdapter(adapter);
//Revisar si no funciona y cambiar de lugar
txtBuscar.setOnQueryTextListener(this);
}
#Override
public boolean onQueryTextSubmit(String s) {
return false;
}
#Override
public boolean onQueryTextChange(String s) {
adapter.filtrado(s);
return false;
}
}
I am making an app in android studio using java. I have made a dialogfragment for the setting option int the overflow menu. It was working just fine before but now when I click settings, the screen dims and nothing happens. I can click on the phone screen and the screen will undim and the app will continue working like normal and it throws no error in log cat. I set a log cat message after in inflater runs and it seems to be executing correctly. I hav no idea why it was working before but now it is not. Here is my code:
dialogFragment class:
package com.dicegame1;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class SettingsMenu extends DialogFragment {
Switch doubles;
Switch triples;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.settings_menu,null);
doubles = dialogView.findViewById(R.id.switchDoublesBonus);
triples = dialogView.findViewById(R.id.switchTriplesBonus);
Button save = dialogView.findViewById(R.id.btnSave);
Button cancel = dialogView.findViewById(R.id.btnCancel);
Log.d("load", "dialog load");
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dismiss();
Log.d("dis", "dismiss");
}
});
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MainActivity callingActivity = (MainActivity) getActivity();
Dice player = callingActivity.sendPlayer();
player.setDoubles(doubles.isChecked());
player.setTriples(triples.isChecked());
Log.d("s", "save");
dismiss();
}
});
return builder.create();
}
}
here is calling methods in main activity:
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.overflow_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (id == R.id.settings) {
SettingsMenu d = new SettingsMenu();
Log.d("1", "Dialog created");
d.show(getSupportFragmentManager(),"");
Log.d("2", "Show called");
return true;
}
return super.onOptionsItemSelected(item);
}
here is xml for settings menu:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="#+id/switchDoublesBonus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:text="Doubles"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.205"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.213"
/>
<Switch
android:id="#+id/switchTriplesBonus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="226dp"
android:minHeight="48dp"
android:text="Triples"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.201"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
/>
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.124"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.876" />
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.78"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.876" />
</androidx.constraintlayout.widget.ConstraintLayout>
I want to reiterate that things were working correctly before and I have reverted all changes made and its still not working. Any ideas??
MyEventsFragment.java
List item
package com.example.edvenswa.collabevents;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class MyEventsFragment extends FragmentActivity{
ViewPager viewPager = null;
MyAdapter myAdapter;
public MyEventsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_my_events);
viewPager = (ViewPager) findViewById(R.id.myEventsPager);
/*List<Fragment> listFragments = new ArrayList<Fragment>();
listFragments.add(new MyEventsInsideFragment());
listFragments.add(new ExpiredEventsFragment());
listFragments.add(new AttendingEventsFragment());
listFragments.add(new PastAttendingEventsFragment());*/
myAdapter = new MyAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.myEventsPager);
viewPager.setAdapter(myAdapter);
}
/* #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_events, container, false);
}*/
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Log.d("count","count called"+i);
Fragment fragment = null;
if (i == 0) {
fragment = new MyEventsInsideFragment();
}
if (i == 1) {
fragment = new ExpiredEventsFragment();
}
if (i == 2) {
fragment = new AttendingEventsFragment();
}
if (i == 3) {
fragment = new PastAttendingEventsFragment();
}
return fragment;
}
#Override
public int getCount() {
Log.d("count", "count called");
return 4;
}
}
fragment_my_events.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.edvenswa.collabevents.MyEvents">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/myEventsPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:gravity="center">
</android.support.v4.view.ViewPager>
</FrameLayout>
So, this is my code. here I am able to view the first page in fragment but unable scroll and no view of other fragments.
Note: This MyEventsFragment.java is also a fragment in the sidedrawer.
First add the design support library if you haven't
compile 'com.android.support:design:23.0.0'
then add the TabLayout like this one.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
Then in your onCreate() method below the viewPager add the following line,
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
Also, in your FragmentPageAdapter, you'll have to override getPageTitle() method for returning your Tabs title.