i set on click on the card view when i click on the card view the fragment is shown on the activity i assume that but the fragment is not shown the onclick of cardview is working but fragment is not shown i try alot but no solution i do not know why this happen. There is no error also but my fragment is not shown even no error Advance thanlks
here's code
Activity
public class SubmitAddActivity extends AppCompatActivity implements CallbackFromFragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submitadd_layout);
findViewById(R.id.choose_category_layout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("bullhead", "onClick: hellow rodl ");
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container,fragmentUsage);
fragmentTransaction.commit();
fragmentUsage.setTYPE(1);
}
});
submitbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
settingToolbar();
}
#Override
public void setValues(int type, String value) {
if(type==1)
{
fragmentTransaction.remove(fragmentUsage);
category=value;
}
else if(type==2)
{
fragmentTransaction.remove(fragmentUsage);
location=value;
}
}
}
Activity layout
<ScrollView
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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerfragments"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.asad.taleembazar.activities.HomeActivity">
<include
layout="#layout/cardview_choose_category_submitadds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
/>
</FrameLayout>
</ScrollView>
fragment
public class SelectFragment extends Fragment implements com.asad.taleembazar.adpaters.callback {
private SelectCategorySubmitAddAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<String> arrayList=new ArrayList<>();
CallbackFromFragment communication;
int type;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public SelectFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_select_category_submit,container,false);
arrayList.add("Cars");
arrayList.add("Mobiles");
arrayList.add("Bags");
RecyclerView recyclerViewforsubmitadd = (RecyclerView)view.findViewById(R.id.recyclerview_for_categoriessubmit);
layoutManager = new LinearLayoutManager(getActivity());
recyclerViewforsubmitadd.setLayoutManager(layoutManager);
recyclerViewforsubmitadd.setHasFixedSize(true);
adapter = new SelectCategorySubmitAddAdapter(arrayList);
adapter.setOnClick(this);
recyclerViewforsubmitadd.setAdapter(adapter);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
communication = (CallbackFromFragment) context;
}
#Override
public void onClick(int adapterPosition) {
if(type==1)
communication.setValues(1,arrayList.get(adapterPosition));
else if(type==2)
communication.setValues(1,arrayList.get(adapterPosition));
}
public void setTYPE(int i)
{
type=i;
}
}
match_parent of your container Framelayout won't work inside your ScrollView. You can either set android:fillViewport="true" in your ScrollView or You can set a fixed height for your container.
Try below code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
android:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerfragments"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.asad.taleembazar.activities.HomeActivity">
<include
layout="#layout/cardview_choose_category_submitadds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
/>
</FrameLayout>
</ScrollView>
Related
When Bottom sheet is open(First image below) and
When tab on editText , the keyboard hides the edit text(Second image below) , i want to move keyword below the "ADD" botton of bottomsheet.How to do this?
Image 2:
Fragment
public class CustomExerciseFragment extends BaseCardFragment {
private View parent_view;
private RecyclerView recyclerView;
private AdapterEasyListAnimation mAdapter;
private LinkedList<DaysDetails> items = new LinkedList<>();
private int animation_type = ItemAnimation.BOTTOM_UP;
private DaysDAO daysDAO;
private ExercisesDAO exercisesDAO;
private ImageView imageView;
private Button buttonBreif;
//=========Configure this at the beginning=============
private final String routineId="1520";
private final String legWorkoutImageName="leg_workout_customize";
private BottomSheetBehavior mBehavior;
private BottomSheetDialog mBottomSheetDialog;
private View bottom_sheet;
public static CustomExerciseFragment newInstance() {
CustomExerciseFragment fragment = new CustomExerciseFragment();
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_custom_exercise, container, false);
mCardView = (CardView) root.findViewById(R.id.cardView);
mCardView.setMaxCardElevation(mCardView.getCardElevation()
* CardAdapter.MAX_ELEVATION_FACTOR);
bottom_sheet = root.findViewById(R.id.bottom_sheet_add_days);
mBehavior = BottomSheetBehavior.from(bottom_sheet);
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
((ExtendedFloatingActionButton) root.findViewById(R.id.extended_fab)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAddDaysBottomSheetDialog();
}
});
return root;
}
private void showAddDaysBottomSheetDialog() {
if (mBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
final View view = getLayoutInflater().inflate(R.layout.sheet_add_days, null);
mBottomSheetDialog = new BottomSheetDialog(getContext());
mBottomSheetDialog.setContentView(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBottomSheetDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
}
BottomSheetLayout
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="#+id/nested_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical"
android:layout_gravity="bottom"
android:scrollingCache="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/spacing_middle"
android:paddingLeft="#dimen/spacing_mlarge"
android:paddingRight="#dimen/spacing_mlarge"
android:paddingTop="#dimen/spacing_middle">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_medium"
android:orientation="vertical"
android:padding="#dimen/spacing_medium">
<EditText
style="#style/EditText.Flat.Grey"
android:layout_width="match_parent"
android:layout_height="#dimen/spacing_xmlarge"
android:background="#drawable/edit_text_round_bg_outline"
android:inputType="text"
android:minHeight="#dimen/spacing_xmlarge"
android:paddingLeft="#dimen/spacing_large"
android:paddingRight="#dimen/spacing_large"
android:text="400" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_middle"
android:orientation="vertical">
<Button
style="#style/Button.Primary.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_rect_black_grey"
android:text="ADD"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
This question already has answers here:
CardViews not showing in RecyclerView
(3 answers)
Closed 2 years ago.
activity_driver_list.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"
tools:context=".DriverListActivity">
<include
android:id="#+id/toolbarDL"
layout="#layout/toolbar"/>
<RelativeLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</LinearLayout>
fragment_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".DLFragment.DriverListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/driverList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
raw_driver_list.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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"
android:elevation="5dp"
app:cardCornerRadius="4dp"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="95dp"
android:layout_height="95dp"
android:padding="10dp"
android:src="#drawable/ic_android"
app:civ_border_width="2dp"
app:civ_border_color="#color/quantum_lightgreen"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/carName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Suzuki Abcd"
android:text="SUZUKI Wagon r"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/places_text_black_alpha_87"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/driverNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0312-1234567"
android:text="0312-1234567"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:textColor="#color/cardview_dark_background"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
DriverListAdapter.Java
public class DriverListAdapter extends RecyclerView.Adapter<DriverListAdapter.DriverListViewHolder> {
private ArrayList<Driver> mLst;
public class DriverListViewHolder extends RecyclerView.ViewHolder{
public CircleImageView mImage;
public TextView mCName, mDNumber;
public DriverListViewHolder(#NonNull View itemView) {
super(itemView);
mImage = itemView.findViewById(R.id.profile_image);
mCName = itemView.findViewById(R.id.carName);
mDNumber = itemView.findViewById(R.id.driverNumber);
}
}
#NonNull
#Override
public DriverListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_driver_list, parent, false);
DriverListViewHolder driverListAdapter = new DriverListViewHolder(view);
return driverListAdapter;
}
#Override
public void onBindViewHolder(#NonNull DriverListViewHolder holder, int position) {
Driver driver = mLst.get(position);
holder.mImage.setImageResource(driver.getdImage());
holder.mCName.setText(driver.getdCName());
}
#Override
public int getItemCount()
{
return mLst.size();
}
public DriverListAdapter(ArrayList<Driver> lst) {
this.mLst = lst;
}
}
DriverListFragment.Java
public class DriverListFragment extends Fragment {
public static final String TAG = "DriverListFragment";
private ArrayList<Driver> driverLst;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private DriverListAdapter mAdapter;
public DriverListFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_driver_list, container, false);
driverLst = new ArrayList<>();
driverLst.add(new Driver(R.drawable.ic_android,"Car One","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Two","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Three","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Four","0312-1234567"));
driverLst.add(new Driver(R.drawable.ic_android,"Car Five","0312-1234567"));
mRecyclerView = (RecyclerView) v.findViewById(R.id.driverList);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this.getActivity());
mAdapter = new DriverListAdapter(driverLst);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
return v;
}
}
DriverListActivity.Java
public class DriverListActivity extends AppCompatActivity {
public static final String TAG = "DriverListActivity";
private TDD tdd = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_driver_list);
initToolbar();
getMyIntent();
driverList();
}
private void getMyIntent()
{
Intent i = getIntent();
tdd = (TDD) i.getSerializableExtra("tdd");
Log.d(TAG,tdd.toString());
}
private void initToolbar()
{
Toolbar toolbar = findViewById(R.id.toolbarDL);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Hello There..");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void driverList()
{
DriverListFragment driverListFragment = new DriverListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer,driverListFragment);
fragmentTransaction.addToBackStack("driverListFragment");
fragmentTransaction.commit();
}
}
I want to show my RecyclerView list in fragment instead of an activity, when i call fragment in activity it simply showing me blank activity however, there's no error in code. How can i achieve this task or where i am doing a mistake?
How to build a Horizontal ListView with RecyclerView?
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);
I have created tabbed activity application in android. I want to change textview's text when application is start or the button is pressed. (Button is in Tab1Fragment, TextView is in Tab2Fragment.) I created a interface and implemented this interface in MainActivity. But when i click button the application is closed. Here is my codes
MainActivity (respond is Communicator's method)
#Override
public void respond(String s) {
FragmentManager fragmentManager = getSupportFragmentManager();
Tab2 testFragment = (Tab2)fragmentManager.findFragmentById(R.id.tab2Fragment);
testFragment.update(s);
}
Tab1
Communicator comm;
private static int counter;
public Tab1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
comm = (Communicator)getActivity();
Button button = (Button)getActivity().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
comm.respond("" + counter);
}
});
}
Tab2
public class Tab2 extends Fragment {
TextView textView;
public Tab2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab2, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textView = (TextView)getActivity().findViewById(R.id.textView);
}
public void update(String s){
textView.setText(s);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.oguz.faircontrol_v11.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
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/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
fragment_tab1
<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"
tools:context="com.oguz.faircontrol_v11.Tab1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Tikla"
android:gravity="center"/>
</FrameLayout>
fragment_tab2
<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"
tools:context="com.oguz.faircontrol_v11.Tab2"
android:id="#+id/tab2Fragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test"
android:id="#+id/textView"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"/>
</FrameLayout>
clear this code ;
Button button = (Button)getActivity().findViewById(R.id.button);
like this;
Button button = (Button)view.findViewById(R.id.button);
It will resolve your problem.
In my app i am using this library
https://github.com/DmitryMalkovich/circular-with-floating-action-button
to implement progress bar with floating action button. Its working on activity but when i included this layout in my fragment's layout progress bar doesn't show.
Here is my code for better explanation
Please guide me where i am going wrong
Any help will be appreciated.
customlayout.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:clickable="true">
<ProgressBar
android:id="#+id/progressbar"
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/transparent_logo"
app:backgroundTint="#color/bg_color"
android:layout_centerInParent="true"
/>
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
</RelativeLayout>
fragmentlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="bg_color"
>
<include
layout="#layout/customlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/otherlayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I am hiding and showing the parent layout of customlayout.xml on runtime when i get data from serivice, before service call i SHOW it with View.VISIBLE and after service call i HIDE it with View.GONE
UPDATED
Java code
Fragment
public class MyFragment extends Fragment {
private Context context;
//root view of layout
View rootView;
private static final String ARG_PARAM1 = "Class";
private String screen_title;
private String URL = "";
public MyFragment() {
// Required empty public constructor
}
public static MyFragment newInstance(String param1) {
MyFragmentfragment = new MyFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
if (getArguments() != null) {
screen_title = getArguments().getString(ARG_PARAM1);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragmentlayout, container, false);
//find views by ids
getData();
return rootView;
}
private void getData() {
CustomClass.getInstance(context, rootView).show();
//service call
//on getting response from serivce (have implemented a listener here)
CustomClass.getInstance(context, rootView).hide(); }
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
CustomClass
public class CustomClass {
private ProgressBar mProgressBar;
private FloatingActionButton mFab;
private RelativeLayout parentLayout;
private LayoutInflater inflater;
private static CustomClass custom;
private CustomClass (Context context) {
findViewsById(context);
}
private CustomClass (View view) {
findViewsById(view);
}
public static CustomClass getInstance(Context context, View view) {
custom = new CustomClass (view);
custom.setPColor(context);
return custom;
}
public static CustomClass getInstance(Context context) {
custom = new CustomClass (context);
custom.setPColor(context);
return custom;
}
private void setPColor(Context context) {
if (mProgressBar != null)
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, R.color.bluecolor), android.graphics.PorterDuff.Mode.MULTIPLY);
}
private void findViewsById(View view) {
if (view != null) {
mProgressBar = (ProgressBar) view.findViewById(R.id.custom_progressbar);
mFab = (FloatingActionButton) view.findViewById(R.id.custom_floatingActionButton);
parentLayout = (RelativeLayout) view.findViewById(R.id.rl_progress_fab_container);
}
}
private void findViewsById(Context context) {
Activity activity = (Activity) context;
if (activity != null) {
mProgressBar = (ProgressBar) activity.findViewById(R.id.custom_progressbar);
mFab = (FloatingActionButton) activity.findViewById(R.id.custom_floatingActionButton);
parentLayout = (RelativeLayout) activity.findViewById(R.id.rl_progress_fab_container);
}
}
public void show() {
if (custom.parentLayout != null)
custom.parentLayout.setVisibility(View.VISIBLE);
}
public void hide() {
if (custom.parentLayout != null)
custom.parentLayout.setVisibility(View.GONE);
}
make your customlayout at the bottom with the parent layout.
so your z index for that layout is at the top and it will be visible and gone that can be seen by you.
You need to use CoordinatorLayout with FAB .
Just put your FAB inside CoordinatorLayout.
As you can see library sample code suggests to use it too.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dmitrymalkovich.android.progressfabsample.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:clickable="true"
app:layout_anchor="#id/app_bar"
app:layout_anchorGravity="bottom|end">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backup_black_24dp"
app:backgroundTint="#color/colorFab" />
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
<com.dmitrymalkovich.android.ProgressFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:clickable="true">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_backup_black_24dp"
app:backgroundTint="#color/colorFab" />
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.dmitrymalkovich.android.ProgressFloatingActionButton>
try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragmentlayout, container, false);
//find views by ids
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
getData();
}
});
return rootView;
}
or
private void getData() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
CustomClass.getInstance(context, rootView).show();
CustomClass.getInstance(context, rootView).hide();
}
});
}
I have activity with Textview and below viewpager. In one of the fragments of view pager, I have a fab button. When I am calling the Snackbar on Menu click of Activity, the Snackbar is coming over the fab button. Every thing works fine (fab button is pushed over snack bar) , if the fab button is in activity, but i need it fragment. Please let me know where I am doing mistake
Fragment Layout
<android.support.design.widget.CoordinatorLayout 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:id="#+id/coordLayoutComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/mdtp_white"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/btn_fab_margins"
android:layout_marginRight="#dimen/btn_fab_margins"
android:elevation="6dp"
android:src="#drawable/ic_comment_24_5"
app:borderWidth="0dp"
app:fabSize="mini"
app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>
Activity Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="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.support.design.widget.CoordinatorLayout
android:id="#+id/coordLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:id="#+id/newtoolbar"
layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="120dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:contentPadding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
fontPath="fonts/SourceSansPro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:textSize="28sp" />
<TextView
android:id="#+id/tvDesc"
fontPath="fonts/Roboto-Italic.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:textSize="15sp"
android:textStyle="normal|italic"
tools:ignore="MissingPrefix" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/newtoolbar"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scrollbars="horizontal" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
In you xml layout, there are two CoordinatorLayouts. One in Activity Layout and the other one in Fragment Layout. Now Snackbar.make() function's first argument is the parent Coordinator Layout in which this Snackbar will be placed, and all the elements of this Coordinator Layout will be affected by the motion/view changes of the Sanckbar. Try and use Fragment Layout's Coordinator Layout as the parent of the Snackbar.
Activity Code
public class HomeActivity extends AppCompatActivity
implements FragmentListener {
private LoadingDialog loadingDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
HomeActivity.this.showSnackbar(view, "Replace with your own action");
}
});
}
#Override
public void showSnackBar(View view, String message) {
Snackbar.make(view,message,Snackbar.LENGTH_LONG).show();
}
}
FragmentListener Code
public interface FragmentListener {
void showSnackBar(View view,String message);
}
Fragment Code
public class MyFragment extends Fragment {
private static final String TAG = MyFragment.class.getSimpleName();
private FragmentListener fragmentListener;
private FloatingActionButton floatingActionButton;
public MyFragment() {
// Required empty public constructor
}
public static MyFragment getInstance(String uniqueId) {
MyFragment MyFragment = new MyFragment();
return MyFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_surveys, container, false);
floatingActionButton = view.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragmentListner.showSnackBar(floatingActionButton,"hello");
}
});
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
fragmentListener = (FragmentListener) context;
} catch (Exception e) {
throw new ClassCastException(" must implement OnListItemSelectedListener");
}
}
}
For displaying the Snackbar in Activity and Fragment with FAB you can use this simple approach.
Activity and Fragment parent View should be CoordinatorLayout.
Create FragmentListener and implement in Activity