Android: Multi Pane layout detail view appears by default - android

In my app, I have requirement of Multi Payne layout. In this layout, the first fragment is a ListView which shows the list of items. On click of the list item, a detail view will open up on the right hand side of the list item. But, in my case, when I run my app on tablet, the detail view appears along with ListView by default. While, I want that it should appear on click of the list item.
Below is my code:
Activity Class:
public class OrderActivity extends FragmentActivity implements
OnOrderSelectedListener {
private static final String TAG = "OrderActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
setContentView(R.layout.order_details);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
OrderListFragment orderListFragment = new OrderListFragment();
orderListFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, orderListFragment).commit();
}
}
#Override
public void onOrderSelected(int position) {
Log.d(TAG, "onOrderSelected called");
OrderDetailFragment detailsFrag = (OrderDetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.order_detail_fragment);
if (detailsFrag != null) {
if (!detailsFrag.isVisible()) {
detailsFrag.setUserVisibleHint(true);
detailsFrag.updateOrderView(position);
}
} else {
OrderDetailFragment newFragment = new OrderDetailFragment();
Bundle args = new Bundle();
args.putInt(OrderDetailFragment.ARG_POSITION, position);
newFragment.setArguments(args);;
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
LIst Fragment
public class OrderListFragment extends ListFragment {
private static final String TAG = "OrderListFragment";
OnOrderSelectedListener mOnOrderSelectedListener;
public interface OnOrderSelectedListener {
public void onOrderSelected(int position);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate called");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
return inflater.inflate(R.layout.order_list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated called");
ArrayList<Data> mDataList = new ArrayList<Data>();
Data mData1 = new Data("1", "11001", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
Data mData2 = new Data("1", "11002", "08/07/2013", "GAUGE_RUN",
"Dispatched", "Terminal:", "Rail Terminal:", "New York",
"Washington DC");
mDataList.add(mData1);
mDataList.add(mData2);
setListAdapter(new OrderAdapter(getActivity(),
R.layout.order_list_item, mDataList));
}
#Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart called");
if (getFragmentManager().findFragmentById(R.id.order_detail_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG, "onAttach called");
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mOnOrderSelectedListener = (OnOrderSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d(TAG, "onListItemClicked");
mOnOrderSelectedListener.onOrderSelected(position);
getListView().setItemChecked(position, true);
}
}
Detail Fragment
public class OrderDetailFragment extends Fragment {
public final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static final String TAG = "OrderDetailFragment";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView called");
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
return inflater.inflate(R.layout.acceptance_details, container, false);
}
#Override
public void onStart() {
Log.d(TAG, "onStart called");
super.onStart();
Bundle args = getArguments();
if (args != null) {
updateOrderView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
updateOrderView(mCurrentPosition);
}
}
public void updateOrderView(int position) {
Log.d(TAG, "updateOrderView called");
/*
* TextView article = (TextView)
* getActivity().findViewById(R.id.article);
* article.setText(Ipsum.Articles[position]); btnNext = (Button)
* getActivity().findViewById(R.id.btnNext);
* btnNext.setOnClickListener(this); mCurrentPosition = position;
*/
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState called");
super.onSaveInstanceState(outState);
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="horizontal" >
<com.dzo.dispatchcrude.driverapp.ui.HeaderBar
android:id="#+id/headerBar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true" >
</com.dzo.dispatchcrude.driverapp.ui.HeaderBar>
<LinearLayout
android:id="#+id/linOrderView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/headerBar"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="#+id/order_list_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/order_detail_fragment"
android:name="com.dzo.dispatchcrude.driverapp.ui.OrderDetailFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="30dp"
android:layout_weight="2" />
</LinearLayout>
</RelativeLayout>
List Fragment layout
<?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" >
<ListView
android:id="#id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
</LinearLayout>
Order Detail Fragment
<?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="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/corner_shape"
android:orientation="vertical" >
<TextView
android:id="#+id/txtHeaderType"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="-5dp"
android:background="#drawable/upper_corner"
android:gravity="center"
android:text="#string/acceptance_details"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtHeaderSource"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/rectangle"
android:gravity="center"
android:textSize="25sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTruck"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/truck"
android:textColor="#color/text_color"
android:textSize="20sp"
android:typeface="sans" />
<Spinner
android:id="#+id/truckSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailorSpinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/txtTrailor2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/trailor2"
android:textColor="#color/text_color"
android:textSize="20sp" />
<Spinner
android:id="#+id/trailor2Spinner"
style="#android:style/Widget.Spinner"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_weight="3"
android:background="#drawable/bg_edit_text"
android:gravity="center"
android:spinnerMode="dropdown" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/divider_color" />
<Button
android:id="#+id/btnAccept"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:background="#drawable/input_button"
android:gravity="center"
android:text="#string/accept"
android:textColor="#android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
I did gone through Fragments Basic Demo given on the Android developer's site, but couldn't figure out my mistake.

try this
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listview.setItemChecked(0,false);

Related

How to fetch data related to a clicked item in a recyclerView from room database to a new activity?

My app has a RawMaterialFragment that displays raw materials data in a recyclerView from room database.
I am trying to build a detail activity(MetrialItemView) to show up the details of an individual raw material by clicking or selecting the raw material from the recyclerView.
my problem is how to send the data from the adapter and how to receive the data in the MetrialItemView Activity and display it.
MaterialListAdapter:
public class MaterialListAdapter extends RecyclerView.Adapter<MaterialListAdapter.ViewHolder> {
private final LayoutInflater mInflater;
private FragmentRawMaterials mContext;
private List<RawMaterialsEntity> mMaterial; // Cached copy of Materials
RawMaterialsEntity mCurrent;
public MaterialListAdapter(FragmentRawMaterials context) {
mInflater = LayoutInflater.from(context.getActivity());
mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final TextView materialName;
private final TextView materialBrand;
private final TextView materialQuantity;
LinearLayout parentLayout;
private ViewHolder(View itemView) {
super(itemView);
materialName = itemView.findViewById(R.id.raw_material_name);
materialBrand = itemView.findViewById(R.id.raw_material_brand);
materialQuantity = itemView.findViewById(R.id.raw_material_quantity);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (mMaterial != null) {
mCurrent = mMaterial.get(position);
holder.materialName.setText(mCurrent.getRawMaterialName());
holder.materialBrand.setText(mCurrent.getRawMaterialBrand());
holder.materialQuantity.setText(String.valueOf(mCurrent.getRawMaterialQuantity()));
} else {
// Covers the case of data not being ready yet.
holder.materialName.setText("Name NA");
holder.materialBrand.setText("Brand NA");
holder.materialQuantity.setText("Quantity NA");
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
mContext.startActivity(intent);
}
});
}
public void setMaterial(List<RawMaterialsEntity> materials){
mMaterial = materials;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
#Override
public int getItemCount() {
if (mMaterial != null)
return mMaterial.size();
else return 0;
}
}
MaterialItemView:
public class MaterialItemView extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.material_item_view);
}
}
material_list_item:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Name:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Raw Material Brand:"
style="#style/OtherTextViews"/>
<TextView
android:id="#+id/material_brand_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Weight:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2000"
style="#style/OtherTextViewsBody"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gm"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unit Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cost per gm/ml:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.1"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Available Quantity:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Cost:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="50000"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Name:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Email:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pandah#panadh.com"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Supplier Phone:"
style="#style/OtherTextViews"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+966555699517"
style="#style/OtherTextViewsBody"/>
</LinearLayout>
</LinearLayout>
FragmentRawMaterials:
public class FragmentRawMaterials extends Fragment{
private RawMaterialViewModel mMaterialViewModel;
private static final int NEW_MATERIAL_ACTIVITY_REQUEST_CODE = 1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_raw_materials, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Setup any handles to view objects here
//FloatingActionButton fab to insert recipes
TextView emptyViewText = view.findViewById(R.id.empty_raw__materials_view);
FloatingActionButton fab = view.findViewById(R.id.fab_raw_materials);
fab.setOnClickListener(view1 -> {
Intent intent = new Intent(getActivity(), RawMaterialsEditor.class);
startActivityForResult(intent, NEW_MATERIAL_ACTIVITY_REQUEST_CODE);
});
RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
//Decoration to add a line divider between recyclerView items
DividerItemDecoration decoration =
new DividerItemDecoration(Objects.requireNonNull(this.getActivity()),
R.drawable.border_line);
recyclerView.addItemDecoration(decoration);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
final MaterialListAdapter adapter = new MaterialListAdapter(this);
recyclerView.setAdapter(adapter);
// Check if adapter list is empty, if so empty text view will appear.
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onChanged() {
super.onChanged();
if (adapter.getItemCount() == 0) {
recyclerView.setVisibility(View.GONE);
emptyViewText.setVisibility(View.VISIBLE);
}
else {
recyclerView.setVisibility(View.VISIBLE);
emptyViewText.setVisibility(View.GONE);
}
}
});
mMaterialViewModel = new ViewModelProvider(this).get(RawMaterialViewModel.class);
// Update the cached copy of the words in the adapter.
mMaterialViewModel.getAllMaterials().observe(this, adapter::setMaterial);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == NEW_MATERIAL_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
RawMaterialsEntity material = new RawMaterialsEntity(data
.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_BRAND),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_WEIGHT)),
Float.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_COST)),
Integer.valueOf(data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_QUANTITY)),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_NAME),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_EMAIL),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_S_PHONE),
data.getStringExtra(RawMaterialsEditor.EXTRA_REPLY_UOM));
mMaterialViewModel.insertMaterial(material);
mMaterialViewModel.costPerGm();
mMaterialViewModel.totalCost();
} else {
Toast.makeText(
Objects.requireNonNull(getActivity()).getApplicationContext(),
R.string.editor_insert_rm_failed,
Toast.LENGTH_LONG).show();
}
}
}
each row of dataModel() must have ID and then use putExtra() whan clicked It happens
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(mContext.getContext(), MaterialItemView.class);
intent.putExtra("ID",mCurrent.getID());
mContext.startActivity(intent);
}
});
and use getIntent() in detailActivity
int id =getIntent().getIntExtra("ID",-1);
and then get a row data in detail activity from database(viewModel)by ID and parse it

How to fix fragment on back pressed button distortion?

I have a fragment with this layout:
If I click one button I get a list in a Recyclerview, everything is shown ok but on back pressed I have thi button distortion:
Here's the xml of the first fragment:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/category_events_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.FragmentCategoryEvents"
android:orientation="vertical"
android:weightSum="10">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textGrid"
android:text="Scegli una categoria"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<GridLayout
android:columnCount="2"
android:rowCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="14dp">
<!-- Riga 1 -->
<!-- Colonna 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp"
></LinearLayout>
<Button
android:id="#+id/btn_auto"
android:text="Auto"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- colonna 2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp"
></LinearLayout>
<Button
android:id="#+id/btn_moto"
android:text="Moto"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- Riga 2 -->
<!-- Colonna 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="12dp"
></LinearLayout>
<Button
android:id="#+id/btn_corsa"
android:text="Corsa"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
<!-- colonna 2 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="16dp"
></LinearLayout>
<Button
android:id="#+id/btn_bicicletta"
android:text="Bicicletta"
android:textSize="18sp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
This is the fragment code:
public class FragmentCategoryEvents extends Fragment {
private Button btn_auto;
private Button btn_moto;
private Button btn_corsa;
private Button btn_bicicletta;
public FragmentCategoryEvents() {
// Required empty public constructor
}
public static FragmentCategoryEvents newInstance() {
FragmentCategoryEvents fragment = new FragmentCategoryEvents();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_category_events, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
btn_auto = view.findViewById(R.id.btn_auto);
btn_moto = view.findViewById(R.id.btn_moto);
btn_corsa = view.findViewById(R.id.btn_corsa);
btn_bicicletta = view.findViewById(R.id.btn_bicicletta);
btn_auto.setOnClickListener(v -> getCategoryEvents("Auto"));
btn_moto.setOnClickListener(v-> getCategoryEvents("Moto"));
btn_corsa.setOnClickListener(v -> getCategoryEvents("Corsa"));
btn_bicicletta.setOnClickListener(v -> getCategoryEvents("Bicicletta"));
}
private void getCategoryEvents(String category){
FragmentShowCategory newFragment = new FragmentShowCategory();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("category_key",category);
newFragment.setArguments(bundle);
transaction.replace(R.id.category_events_layout,newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
On Button pressed I have this layout:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container_category"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recycler_view_category"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
And the code:
public class FragmentShowCategory extends Fragment {
private RecyclerView mRecyclerView;
private SwipeRefreshLayout mSwipeRefreshLayout;
private CompositeSubscription mCompositeSubscription;
public FragmentShowCategory() {
// Required empty public constructor
}
public static FragmentShowCategory newInstance() {
FragmentShowCategory fragment = new FragmentShowCategory();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_show_category, container, false);
}
#SuppressLint("ResourceAsColor")
#Override
public void onViewCreated (View view, #Nullable Bundle savedInstanceState){
mRecyclerView = view.findViewById(R.id.recycler_view_category);
mRecyclerView.setAdapter(new RecyclerViewAdapter(null));
mRecyclerView.setLayoutManager(null);
mSwipeRefreshLayout = view.findViewById(R.id.swipe_container_category);
Bundle bundle = getArguments();
mCompositeSubscription = new CompositeSubscription();
categoryEventsProcess(bundle.getString("category_key"));
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mSwipeRefreshLayout.post(new Runnable() {
#Override
public void run() {
mSwipeRefreshLayout.setRefreshing(true);
categoryEventsProcess(bundle.getString("category_key"));
}
});
}
});
}
public void onBackPressed()
{
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack();
}
private void categoryEventsProcess(String category){
mCompositeSubscription.add(NetworkUtil.getRetrofit().getEventsByCategory(category)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(this::handleResponse,this::handleError));
}
private void handleResponse(ArrayList<Evento> eventos) {
RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventos);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(adapter);
mSwipeRefreshLayout.setRefreshing(false);
}
private void handleError(Throwable throwable) {
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
How can I fix this problem?
Solved removing layout_columnWeight and layout_rowWeight attributes and setting layout_width and layout_height in Cardview.

Viewpager not delete current page position

I am using ViewPager in side activity and add Fragment-State-Pager-Adapter and trying to remove current page position after add the some pages.but every time removed last page position.so please recommend me for use right approach for doing this.Thanks in advance.
This is my activity and adapter code.
public class Main-Activity extends Fragment-Activity implements AddItemFragment.OnFragmentInteractionListener {
PagerAdapter mAdapter;
ViewPager mPager;
Button button ,btn_add;
int pos,TOTAL_PAGES=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
//THIS IS THE MODEL CLASS WHERE I ADD THE PAGES.
MoveItems.items = new ArrayList<>();
MoveItems.items.clear();
Intialize();
}
public void Intialize() {
mPager = (ViewPager)findViewById(R.id.pager);
mAdapter = new PagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
pos = position + 1;
}
#Override
public void onPageScrollStateChanged(int state) {
//Function.toast(MainActivity.this, "onPageScrollStateChanged");
}
});
Addpage();
button = (Button)findViewById(R.id.delete_current);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAdapter.deletePage(mPager.getCurrentItem());
Function.toast(MainActivity.this,"'Delete page");
}
});
btn_add=(Button)findViewById(R.id.goto_first);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Addpage();
}
});
}
public void Addpage() {
TOTAL_PAGES++;
ItemDescription item = new ItemDescription();
MoveItems.items.add(item);
if (mAdapter != null)
mAdapter.notifyDataSetChanged();
mPager.setCurrentItem((TOTAL_PAGES - 1));
}
#Override
public void onFragmentCreated(ItemDescription itemDescription, int position) {
if (mAdapter != null)
mAdapter.notifyDataSetChanged();
}
public class PagerAdapter extends FragmentStatePagerAdapter
{
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ItemDescription description = new ItemDescription();
description.setItemNo(position);
description = MoveItems.items.get(position);
return AddItemFragment.newInstance(position, description);
}
#Override
public int getCount() {
return MoveItems.items.size();
}
public void deletePage(int position)
{
MoveItems.items.remove(position);;
notifyDataSetChanged();
}
}
}
This The XML PART
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<LinearLayout android:orientation="horizontal"
android:gravity="center"
android:measureWithLargestChild="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<!--go to the first page in the view pager-->
<Button android:id="#+id/goto_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First">
</Button>
<!--Delete the current page-->
<Button android:id="#+id/delete_current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete">
</Button>
<!--go to the last page in the view pager-->
<Button android:id="#+id/goto_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last">
</Button>
</LinearLayout>
</LinearLayout>
THIS THE FRAGMENT CLASS WHERE I INFLATE THE LAYOUT WHICH HAVE CHECK-BOXES AND EDITEXT.
public class AddItemFragment extends Fragment {
// Store instance variables
private int page;
ItemDescription description;
CheckBox furniture, mattress, box, appliance;
EditText other, item_description;
private OnFragmentInteractionListener listener;
LinearLayout line1;
CheckBox checkboxes[];
View v;
// newInstance constructor for creating fragment with arguments
public static AddItemFragment newInstance(int page, ItemDescription item) {
AddItemFragment fragmentFirst = new AddItemFragment();
Bundle args = new Bundle();
args.putInt("pagecount", page);
args.putSerializable("item", item);
fragmentFirst.setArguments(args);
return fragmentFirst;
}
#Override
public void onAttach(Activity activity) {
try {
listener = (OnFragmentInteractionListener) getActivity();
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
}
super.onAttach(activity);
}
/**
* Interface for communicating data
*/
public interface OnFragmentInteractionListener {
public void onFragmentCreated(ItemDescription itemDescription, int position);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_add_item, container, false);
page = getArguments().getInt("pagecount", 0);
description = (ItemDescription) getArguments().getSerializable("item");
One = (CheckBox) v.findViewById(R.id.furniture);
Two = (CheckBox) v.findViewById(R.id.mattress);
Three = (CheckBox) v.findViewById(R.id.box);
Four = (CheckBox) v.findViewById(R.id.appliance);
other = (EditText) v.findViewById(R.id.other);
item_description = (EditText) v.findViewById(R.id.item_description);
checkboxes = new CheckBox[]{One, Two, Three, Four};
return v;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
BitmapsForImage bitmapsForImage = null;
imagePath = "";
if (resultCode == Activity.RESULT_OK) {
if (requestCode == Constants.ImagePick) {
Uri imageUri = getPickImageResultUri(data);
imagePath = getPath(getActivity(), imageUri);
bitmapsForImage = Function.resizeBitmap(imagePath, getActivity());
} else if (Constants.CAMERA_REQUEST_CODE == requestCode) {
bitmapsForImage = Function.resizeBitmap(imagePath, getActivity());
} else if (Constants.GALLERY_IMAGE == requestCode && null != data) {
if (data != null) {
Uri contentURI = data.getData();
String imagePath = Function.getRealPathFromURI(getActivity(), contentURI);
bitmapsForImage = Function.resizeBitmap(imagePath, getActivity());
}
}
if (bitmapsForImage != null && imagePath != null && imagePath.length() > 0) {
MoveItems.itemsBitmapList.set(page, bitmapsForImage.getBitmapToShow());
line1.setVisibility(View.GONE);
description.setItemFilePath(imagePath);
description.setItemFile(new File(imagePath));
MoveItems.items.get(page).setItemFile(new File(imagePath));
MoveItems.items.get(page).setItemFilePath(imagePath);
try {
listener.onFragmentCreated(description, page);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Function.toast(getActivity(), "Some Error Occured");
return;
}
}
}
}
THIS IS THE Fragment_Add_Item WHICH HAVE CHECK BOX AND EDIT TEXT.
<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"
android:padding="10dp"
android:layout_marginTop="20dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:orientation="horizontal">
<CheckBox
android:id="#+id/furniture"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="Furniture"
android:textSize="15sp" />
<CheckBox
android:id="#+id/mattress"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="Mattress"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="5dp"
android:orientation="horizontal">
<CheckBox
android:id="#+id/box"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="Box"
android:textSize="15sp" />
<CheckBox
android:id="#+id/appliance"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingLeft="10dp"
android:singleLine="true"
android:text="Appliance"
android:textSize="15sp" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/other_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/other"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Other"
android:singleLine="true"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/notes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/item_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Item Notes"
android:singleLine="true"
android:textSize="15sp" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:id="#+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2dp"
android:text="Add a Photo"
android:textSize="15sp" />
<TextView android:id="#+id/Des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="2dp"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
This problem occurs when I get any input type inside Fragment_Add_Item layout please help me for find out this problem.This very-very important from me and i am using first time view-pager. In this code item of view-pager deleted but not exact.so i was doing this differnt ways. but not getting right output. thanks in advance.

No view found for id 0x7f0c0078 for the fragment while calling fragment from activity

In my app i have to call a fragment from activity. so that i am using Frgament mangaer. While i am running that code it throws the above exception.
this is my main activity
public class UserDashBoardActivity extends DrawerActivity {
private Context context;
private ImageButton searchBtn;
private ImageButton favBtn;
private ImageButton profileBtn;
private ImageButton reminderBtn;
private ImageButton logoutBtn;
private ImageButton notificationBtn;
private ImageView seatchIcon;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onStart() {
super.onStart();
AppActivityStatus.setActivityStarted();
AppActivityStatus.setActivityContext(context);
}
#Override
protected void onPause() {
super.onPause();
AppActivityStatus.setActivityStoped();
}
#Override
protected void onResume() {
super.onResume();
AppActivityStatus.setActivityStarted();
}
#Override
protected void onStop() {
super.onStop();
AppActivityStatus.setActivityStoped();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_user_dash_boad, menu);
return true;
}
// delete the selected event from event list added here
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notify:
return true;
case R.id.action_favourite:
return true;
case R.id.action_navigation:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_dash_board);
context = getApplicationContext();
searchBtn = (ImageButton) findViewById(R.id.search_btn);
favBtn = (ImageButton) findViewById(R.id.fav_btn);
profileBtn = (ImageButton) findViewById(R.id.profile_btn);
reminderBtn = (ImageButton) findViewById(R.id.reminder_btn);
notificationBtn = (ImageButton) findViewById(R.id.notification_btn);
logoutBtn = (ImageButton) findViewById((R.id.logout_btn));
final EditText Search = (EditText) findViewById(R.id.emailAddress);
searchBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent regAct = new Intent(getApplicationContext(), SearchActivity.class);
// Clears History of Activity
regAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(regAct);
}
});
favBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//here i am calling the fragment
MyFavouritesFragment fragment = new MyFavouritesFragment();
if (fragment != null) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}
});
profileBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent tabAct = new Intent(getApplicationContext(), AboutCollegeFragment.class);
tabAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(tabAct);
}
});
}
}
this is my fragment to be called
public class MyFavouritesFragment extends Fragment {
private FavouriteDelegates favouriteDelegates = new FavouriteDelegates();
private Gson gson = new Gson();
private Context context;
private List<CollegeMO> collegeMOs = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = inflater.inflate(R.layout.my_favourites_list_view, container, false);
return view;
}
private class FavouriteCollege extends BaseAdapter {
LayoutInflater mInflater;
TextView collegeText;
FavouriteCollege() {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return collegeMOs.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
// show list values name and mobile number in contact page
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.my_favourites, null);
collegeText = (TextView) convertView.findViewById(R.id.clg_details);
collegeText.setText(collegeMOs.get(position).getCollegeName());
return convertView;
}
}
}
this is my main activity 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:layout_gravity="center"
android:background="#color/appblue"
android:orientation="vertical">
<ImageButton
android:id="#+id/search_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="5dp"
android:layout_marginRight="150dp"
android:layout_marginTop="70dp"
android:background="#drawable/search_blue"
android:gravity="center" />
<TextView
android:id="#+id/searchCollege"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/search_college"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/fav_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-167dp"
android:background="#drawable/fav_blue"
android:gravity="center" />
<TextView
android:id="#+id/myFavourites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="370dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-30dp"
android:text="#string/my_favourites"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/profile_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_marginLeft="90dp"
android:layout_marginRight="150dp"
android:layout_marginTop="30dp"
android:background="#drawable/profile_blue"
android:gravity="center" />
<TextView
android:id="#+id/myProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="105dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/my_profile"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/notification_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-165dp"
android:background="#drawable/notification_blue"
android:gravity="center" />
<TextView
android:id="#+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="390dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/notification"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/reminder_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="55dp"
android:layout_marginRight="200dp"
android:layout_marginTop="20dp"
android:background="#drawable/reminder_blue"
android:gravity="center" />
<TextView
android:id="#+id/reminder"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginLeft="110dp"
android:layout_marginRight="100dp"
android:layout_marginTop="20dp"
android:text="#string/reminder"
android:textColor="#color/green"
android:textSize="20sp" />
<ImageButton
android:id="#+id/logout_btn"
android:layout_width="115dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="150dp"
android:layout_marginRight="10dp"
android:layout_marginTop="-220dp"
android:background="#drawable/logout_blue"
android:gravity="center" />
<TextView
android:id="#+id/logout"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="410dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="#string/logout"
android:textColor="#color/green"
android:textSize="20sp" />
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="1000dp"
android:layout_height="1000dp"
android:layout_gravity="center"
>
</FrameLayout>
</LinearLayout>
this is fragment listview 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:background="#color/white"
android:orientation="vertical">
<ListView
android:id="#+id/course_detail_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbarStyle="outsideOverlay" />
</LinearLayout>
this is the item of myfavourite listview 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="match_parent"
android:background="#color/appblue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="40dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp"
android:background="#color/white">
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:id="#+id/clgImage"
android:src = "#drawable/ic_launcher"
android:layout_weight="1" android:background="#color/white"
android:padding="20dip" android:gravity="center"/>
<TextView
android:id="#+id/clg_details" android:text="Row 2 column 2"
android:layout_weight="1" android:background="#color/white"
android:textColor="#000000"
android:padding="20dip" android:gravity="center"/>
<ImageView
android:id="#+id/downloadImage"
android:src = "#drawable/ic_launcher"
android:layout_weight="1" android:background="#color/white"
android:padding="20dip" android:gravity="center"/>
</TableRow>
</LinearLayout>
</LinearLayout>
you dont have container (R.id.container) in your main activity xml where you should display your fragment into.
like
<framelayout android:id="#+id/container"/> // this snippet is just for idea.

ViewPager in Custom Dialog

I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout.
I am getting error
java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.
I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.
private void showCustomDialogForInstruction() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
layout.setAlpha(0.2f);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(dialogLayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentForInstruction1());
adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
adapter.addFrag(new FragmentForWelcomePage3());
adapter.addFrag(new FragmentForWelcomePage4());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int pos)
{
}
#Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
Button done = (Button) dialog.findViewById(R.id.done);
done.setText("Got It");
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
neverShow.setText("Never Show Again");
neverShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
my view pager class:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
public void addFrag(Fragment fragment)
{
mFragmentList.add(fragment);
}
}
and i call the method in MainActivity :
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferencesNeverShowAgain ;
boolean neverShowAgain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
if(!neverShowAgain){
showCustomDialogForMessage();
}
}
}
and the layout for dialog is:
<?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="wrap_content"
android:background="#drawable/dialog_shape"
android:orientation="vertical" >
<!-- layout title -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_edge_rounded"
android:flipInterval="2000"
android:padding="20dp" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/textColor"
android:textStyle="normal" />
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/accent"
android:textStyle="normal" />
</ViewFlipper>
<View
android:id="#+id/tri"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/triangle"
android:rotation="180" />
</LinearLayout>
<!-- layout dialog content -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pagerInstruction"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutIndicater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- layout dialog buttons -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#drawable/all_rounded_edge_plum_for_dialog" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="#color/textColor" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/textColor" />
<Button
android:id="#+id/nevershow"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Close"
android:textColor="#color/textColor" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Done"
android:textColor="#color/textColor" />
</RelativeLayout>
</LinearLayout>
FragmentForInstruction1 code:
public class FragmentForInstruction1 extends Fragment{
#Override
#Nullable
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
return view;
}
}
and its layout is:
<?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"
android:background="#color/backgroundColor"
android:gravity="center" >
<TextView
android:id="#+id/titleWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/messageWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="normal"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
You should create an adapter that extends FragmentPagerAdapter:
private class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentForInstruction1();
case 1:
return new FragmentForInstruction2();
case 2:
return new FragmentForInstruction3();
case 3:
return new FragmentForInstruction4();
default:
return new FragmentForInstruction1();
}
}
#Override
public int getCount() {
return 4;
}
}
While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():
viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));

Categories

Resources