I have been searching a lot of tutorials to implement an on item long press Listener on a Listview which is Inside a RecyclerView. So far I had no luck.
I want a menu to more ap after holding the List Item for some time. I am attaching my code here please help me with it.
I have added my complete code Including the MainAcyivity.
I have a tabbed view which contains 3tabs. Inside the tab view, there is a fragment which contains a Listview.
I want the List to be long pressed to bring a popup menu which has as add or delete options.
MainActivity.java
package tabbardemo.com.materialdesigntabs_demo;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static Toolbar toolbar;
private static ViewPager viewPager;
private static TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager) findViewById(R.id.viewPager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);//setting tab over viewpager
//Implementing tab selected listener over tablayout
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
switch (tab.getPosition()) {
case 0:
Log.e("TAG","TAB1");
break;
case 1:
Log.e("TAG","TAB2");
break;
case 2:
Log.e("TAG","TAB3");
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
//Setting View Pager
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new DummyFragment("Inbox"), "Inbox");
adapter.addFrag(new EventsFragment("QA"), "QA");
adapter.addFrag(new EventsFragment("Events"), "Events");
viewPager.setAdapter(adapter);
}
//View Pager fragments setting adapter class
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();//fragment arraylist
private final List<String> mFragmentTitleList = new ArrayList<>();//title arraylist
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
//adding fragments and title method
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinatorLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- AppBar Layout -->
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<!-- Tab Layout for creating tabs -->
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<!-- Helps handing the Fragments for each Tab -->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
dummy_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/blue"
android:gravity="center"
android:orientation="vertical">
<!-- Recycler View -->
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</LinearLayout>
DummyFragment.java
package tabbardemo.com.materialdesigntabs_demo;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* Created by SONU on 16/09/15.
*/
public class DummyFragment extends Fragment {
private View view;
private String title;//String for tab title
private static RecyclerView recyclerView;
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
public void onLongItemClick(View view, int position);
}
public DummyFragment(String title) {
this.title = title;//Setting tab title
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dummy_fragment, container, false);
setRecyclerView();
return view;
}
//Setting recycler view
private void setRecyclerView() {
recyclerView = (RecyclerView) view
.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView
.setLayoutManager(new LinearLayoutManager(getActivity()));//Linear Items
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
arrayList.add(title+" Items " + i);//Adding items to recycler view
}
RecyclerView_Adapter adapter = new RecyclerView_Adapter(getActivity(), arrayList);
recyclerView.setAdapter(adapter);// set adapter on recyclerview
}
}
RecyclerView_Adapter
package tabbardemo.com.materialdesigntabs_demo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* Created by SONU on 10/09/15.
*/
public class RecyclerView_Adapter extends
RecyclerView.Adapter<DemoViewHolder> {
private ArrayList<String> arrayList;
private Context context;
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public RecyclerView_Adapter(Context context,
ArrayList<String> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#Override
public int getItemCount() {
return (null != arrayList ? arrayList.size() : 0);
}
#Override
public void onBindViewHolder(DemoViewHolder holder,
int position) {
final DemoViewHolder mainHolder = (DemoViewHolder) holder;
//Setting text over textview
// mainHolder.bind(arrayList.get(position), listener);
mainHolder.title.setText(arrayList.get(position));
}
#Override
public DemoViewHolder onCreateViewHolder(
ViewGroup viewGroup, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(viewGroup.getContext());
ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
R.layout.item_row, viewGroup, false);
DemoViewHolder mainHolder = new DemoViewHolder(mainGroup) {
#Override
public String toString() {
return super.toString();
}
};
// 13805 ////////////////////////////////
return mainHolder;
}
}
DemoViewHolder.java
package tabbardemo.com.materialdesigntabs_demo;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
/**
* Created by SONU on 31/08/15.
*/
public abstract class DemoViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public DemoViewHolder(View view) {
super(view);
this.title = (TextView) view.findViewById(R.id.cardTitle);
}
}
ite_row.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iv_image"
android:padding="10dp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:src="#mipmap/ic_launcher">
</ImageView>
<TextView
android:id="#+id/cardTitle"
android:layout_toRightOf ="#+id/iv_image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:text="Card Title"
android:textColor="#000000"
android:textSize="17sp" ></TextView>
</RelativeLayout>
1- create your menu items (your list item to show on long click)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/one"
android:title="One"/>
<item
android:id="#+id/two"
android:title="Two"/>
<item
android:id="#+id/three"
android:title="Three"/>
</menu>
2- setup listener and menu.
#Override
public void onBindViewHolder(DemoViewHolder holder, int position) {
holder.title.setText(arrayList.get(position));
holder.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
showMenu(v);
return true;
}
});
}
// method to open a popup menu(list) upon long click in the recyclerView item
private void showMenu(View view){
PopupMenu popup = new PopupMenu(context,view );
popup.getMenuInflater()
.inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popup.show();
}
When you are doing setOnItemLongClickListener() in the ListView:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// Dialog/Popup will appears here
showAddDeleteDialog();
return true;
}
});
public void showAddDeleteDialog(){
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setCancelable(false);
dialog.setTitle("Add or Delete Dialog");
// dialog.setMessage("Are you sure you want to delete this entry?" );
dialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
//Action for "Add".
}
})
.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Action for "Delete".
}
});
final AlertDialog alert = dialog.create();
alert.show();
}
Related
hello please I need help
I want to add an Edittext to use it for search for recycleview
my program will be run without crashing but it not work
in my project I have a BottomNavigation (fragment) and inside it I used Tablayout (fragment) (a fragment inside another fragment (Tab inside bottom navigation))
I have no error but my search is not working
**If It is possible for you , please add this code inside your new blank project and test it and help me **
I used this search totorial by this learning project
https://codinginflow.com/tutorials/android/searchview-recyclerview
My Main Activity code is(Fragment of BottomNavigation is inside MainActicity)
package forokans.sirwansoft.forlearn;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
//call Fragments class
private BottomNavigationFragment fragmentOne;
/* private FragmentTwo fragmentTwo;
private FragmentThree fragmentThree;*/
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
setFragment(fragmentOne);
return true;
case R.id.navigation_dashboard:
/*
setFragment(fragmentTwo);
*/
return true;
case R.id.navigation_notifications:
/*
setFragment(fragmentThree);
*/
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//make new from fragments
//fragment of BottomNavigation
fragmentOne = new BottomNavigationFragment();
/* fragmentThree = new FragmentThree();
fragmentTwo = new FragmentTwo();
*/
setFragment(fragmentOne);
frameLayout = findViewById(R.id.main_fream);
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_fream, fragment);
fragmentTransaction.commit();
}
}
My Layout Of main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/main_fream"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
My Bottom Navigation Fragment class
package forokans.sirwansoft.forlearn;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
/**
* A simple {#link Fragment} subclass.
*/
public class BottomNavigationFragment extends Fragment {
TabLayout tabLayout;
EditText search;
ViewPager viewPager;
public BottomNavigationFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_bottom_navigation, container, false);
search=view.findViewById(R.id.searchinfragmentOne);
tabLayout=view.findViewById(R.id.tablayout_id);
viewPager=view.findViewById(R.id.viewpager);
ViewPagerAdapterForTabs adapter = new ViewPagerAdapterForTabs(getChildFragmentManager());
// this is Tab Fragments
adapter.AddFragment(new TabFragment(),"Tab1");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
return view;
}
}
**and My Layout Of this Buttom Navigation **
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BottomNavigationFragment">
<EditText
android:id="#+id/searchinfragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="search ..." />
<android.support.design.widget.TabLayout
android:id="#+id/tablayout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
app:tabGravity="fill"
app:tabIndicatorColor="#color/colorPrimary"
app:tabMode="scrollable"
app:tabTextColor="#color/colorPrimaryDark" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
Tab Fragment
package forokans.sirwansoft.forlearn;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class TabFragment extends Fragment {
public List<Items> lstItems;
public RecyclerViewAdapter mAdapter;
EditText search;
public TabFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tab, container, false);
lstItems = new ArrayList<>();
lstItems.add(new Items("a", "11265", "27500", "horizental", R.drawable.gemtv));
lstItems.add(new Items("b", "11265", "27500", "horizental", R.drawable.bbc));
lstItems.add(new Items("c", "11265", "27500", "horizental", R.drawable.voa));
lstItems.add(new Items("d", "11265", "27500", "horizental", R.drawable.manoto));
search = view.findViewById(R.id.searchintabbadrkurdi);
search.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
RecyclerView myrv = view.findViewById(R.id.recyclerview_id_badr_kurdi);
RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(getContext(), lstItems);
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
myrv.setAdapter(mAdapter);
/*mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new RecyclerViewAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);*/
return view;
}
private void filter(String text) {
ArrayList<Items> filteredList = new ArrayList<>();
for (Items item : lstItems) {
if (item.getTvName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
}
Tab Layout . xml
<?xml version="1.0" encoding="utf-8"?>
<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=".TabFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/searchintabbadrkurdi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_id_badr_kurdi"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</FrameLayout>
My Item Class
package forokans.sirwansoft.forlearn;
public class Items {
private String TvName;
private String Forekans;
private String BistoHaft;
private String Horizental;
private int ImageSrc;
//main constructor
public Items() {
}
// constructor
public Items(String tvName, String forekans, String bistoHaft, String horizental, int imageSrc) {
TvName = tvName;
Forekans = forekans;
BistoHaft = bistoHaft;
Horizental = horizental;
ImageSrc = imageSrc;
}
//getter
public String getTvName() {
return TvName;
}
//getter
public String getForekans() {
return Forekans;
}
public String getBistoHaft() {
return BistoHaft;
}
//getter
public int getImageSrc() {
return ImageSrc;
}
public String getHorizental() {
return Horizental;
}
}
My Recycler View Adapter Class
package forokans.sirwansoft.forlearn;
import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext;
private List<Items> lstItems;
public RecyclerViewAdapter(Context mContext, List<Items> lstItems) {
this.mContext = mContext;
this.lstItems = lstItems;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.recycler_view_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
//in main activity displays
holder.tv_Items_TvName.setText(lstItems.get(position).getTvName());
holder.tv_Items_cat.setText(lstItems.get(position).getForekans());
holder.tv_Items_desc.setText(lstItems.get(position).getBistoHaft());
holder.tv_Items_horizental.setText(lstItems.get(position).getHorizental());
holder.img_Items_ImageSrc.setImageResource(lstItems.get(position).getImageSrc());
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return lstItems.size();
}
public void filterList(ArrayList<Items> filteredList) {
lstItems = filteredList;
notifyDataSetChanged();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_Items_TvName;
TextView tv_Items_cat;
TextView tv_Items_desc;
TextView tv_Items_horizental;
ImageView img_Items_ImageSrc;
CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
tv_Items_TvName = itemView.findViewById(R.id.Items_TvName_id);
tv_Items_cat = itemView.findViewById(R.id.Items_cat_id);
tv_Items_desc = itemView.findViewById(R.id.Items_desc_id);
tv_Items_horizental = itemView.findViewById(R.id.Items_Hor_id);
img_Items_ImageSrc = itemView.findViewById(R.id.Items_img_id);
cardView = itemView.findViewById(R.id.cardview_id);
}
}
}
And My ViewPagerAdapter class For Tabs
package forokans.sirwansoft.forlearn;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerAdapterForTabs extends FragmentPagerAdapter {
private final List<Fragment> FragmentList = new ArrayList<>();
private final List<String> FragmentListTitles = new ArrayList<>();
public ViewPagerAdapterForTabs(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return FragmentList.get(position);
}
#Override
public int getCount() {
return FragmentListTitles.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return FragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment, String titles) {
FragmentList.add(fragment);
FragmentListTitles.add(titles);
}
}
i found out
change this
RecyclerView myrv = view.findViewById(R.id.recyclerview_id_badr_kurdi);
RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(getContext(), lstItems);
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
myrv.setAdapter(mAdapter);
to this
RecyclerView myrv = view.findViewById(R.id.recyclerview_id_badr_kurdi);
mAdapter = new RecyclerViewAdapter(getContext(), lstItems);
myrv.setLayoutManager(new GridLayoutManager(getContext(), 3));
myrv.setAdapter(mAdapter);
the reason is
before We call onCreate this RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(getContext(), lstItems); was initialized and in last one if we make this class a new abject , it will be a new initializing value
I have used a custom toolbar.Inside custom toolbar I do have a textView above the tabs.Now I want to set the visibility of TextView to off for the second and third tabs.This is a image of my layout
In this image when the Ongoing tabs is selected the textfield above the tab(The ongoing survey closes on.........GMT) should be visible while for rest of the tabs the textview visibility should be gone.How can I do this?
The next problem is I have set the background of whole layout to using a background image in parent layout.But I am getting default background behind tabs.What I want is to remove the default tab background color and get the background of parent.How to do this?
This is my code for my layout:
<?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"
android:layout_width="match_parent"
android:background="#drawable/innerpg_bg"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#42474b"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="#+id/toolbarlogo"
android:src="#mipmap/toolbarlogo"
android:layout_width="wrap_content"
android:layout_marginRight="200dp"
android:layout_height="46dp"
/>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/deadline"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#494e51"
android:gravity="center_vertical"
android:paddingLeft="40dp"
android:textSize="10dp"
android:textColor="#fff"
android:text="The Ongoing survey closes on Dec 31,2016 at 2:00pm GMT"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
And here is my java class
package com.example.user.timothysurvey.activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.user.timothysurvey.R;
import java.util.ArrayList;
import java.util.List;
import fragment.Ongoing;
import fragment.Result;
import fragment.Upcomming;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
TextView deadLine;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.mipmap.ongoing,
R.mipmap.upcoming,
R.mipmap.results
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deadLine= (TextView) findViewById(R.id.deadline);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorHeight(0);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
LayoutInflater inflater = LayoutInflater.from(this);
View view1 = inflater.inflate(R.layout.custom_tabs, null, false);
((TextView) view1.findViewById(R.id.text)).setText("Ongoing");
((ImageView) view1.findViewById(R.id.image)).setImageResource(tabIcons[0]);
View view2 = inflater.inflate(R.layout.custom_tabs, null, false);
((TextView) view2.findViewById(R.id.text)).setText("Upcomming");
((ImageView) view2.findViewById(R.id.image)).setImageResource(tabIcons[1]);
View view3 = inflater.inflate(R.layout.custom_tabs, null, false);
((TextView) view3.findViewById(R.id.text)).setText("Results");
((ImageView) view3.findViewById(R.id.image)).setImageResource(tabIcons[2]);
tabLayout.getTabAt(0).setCustomView(view1);
tabLayout.getTabAt(1).setCustomView(view2);
tabLayout.getTabAt(2).setCustomView(view3);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Ongoing(), "Ongoing");
adapter.addFrag(new Upcomming(), "Upcomming");
adapter.addFrag(new Result(), "Result");
viewPager.setAdapter(adapter);
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
return true;
}
return super.onOptionsItemSelected(item);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
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, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Implement OnPageChangeListener for ViewPager
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{
Override onPageSelected methods
#Override
public void onPageSelected(int position) {
//This method will be invoked when a new page becomes selected.set your visiblity here with respect to tab
if( position == 0){
deadLine.setVisibility(View.VISIBLE);
}
else if(position == 1){
deadLine.setVisibility(View.GONE);
}
else if(position == 2){
deadLine.setVisibility(View.GONE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
getting default background behind tabs.add transparent background to AppBarLayout.
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
In my activity (Please see image), I have seek bar and view pager ( have 3 pages).
I want so that, when seek bar in activity change, the text of the fragment size also varies.
How can I do it?
I try it with interface but it not run.
Mainactivity
package com.creativei.viewpagerloop;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private static final String LOG_TAG = "ViewPagerLoop";
public static final String[] content = new String[] {
"Hello Welcome to ViewPager Loop Example. This is first view. Swipe right → for second view, swipe left � for last view.",
"Awesome, now you are on second view. Swipe right → again to go to last view.",
"Finally made it to last view, swipe right → again to go to first view." };
Interface inter;
public MainActivity(Interface inter) {
this.inter = inter;
}
private SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
TextView counter = (TextView) findViewById(R.id.counter);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
inter.seekBarChange(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
SimpleViewPagerAdapter adapter = new SimpleViewPagerAdapter(
getSupportFragmentManager(), pager, content, counter);
pager.setAdapter(adapter);
pager.setOnPageChangeListener(adapter);
pager.setCurrentItem(1, false);
}
public static class SimpleFragment extends Fragment implements Interface {
private TextView textView;
public SimpleFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
String content = getArguments().getString("content");
textView = (TextView) rootView.findViewById(R.id.content);
textView.setText(content);
return rootView;
}
#Override
public void seekBarChange(int id) {
// TODO Auto-generated method stub
textView.setTextSize(id);
}
}
public static class SimpleViewPagerAdapter extends
FragmentStatePagerAdapter implements OnPageChangeListener {
private String[] content;
private ViewPager pager;
private TextView counter;
public SimpleViewPagerAdapter(FragmentManager fm, ViewPager pager,
String[] content, TextView counter) {
super(fm);
this.pager = pager;
this.content = content;
this.counter = counter;
}
#Override
public Fragment getItem(int position) {
SimpleFragment fragment = new SimpleFragment();
Bundle bundle = new Bundle();
int index = position;
bundle.putString("content", content[index]);
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return content.length;
}
#Override
public void onPageSelected(int position) {
}
private String makeCounterText(int pageNo) {
return "Page " + pageNo + " of " + content.length;
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
}
}
Interface
package com.creativei.viewpagerloop;
public interface Interface {
public void seekBarChange(int id);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.creativei.viewpagerloop.MainActivity"
tools:ignore="MergeRootFrame" >
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/counter"
android:layout_below="#+id/seekBar1" />
<TextView
android:id="#+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_horizontal" />
</RelativeLayout>
fragment_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.creativei.viewpagerloop.SimpleFragment" >
<TextView
android:id="#+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
You can get reference from this example http://sampleprogramz.com/android/seekbar.php
I have fragment activity that have view pager, I want every changing page the content is change programmatically. `
package com.idroid.splashscreen;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class ScreenSlidePagerActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
OnPageChangeListener pageChangeListener=null;
pageChangeListener = new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int arg0) { }
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
#Override
public void onPageSelected(int position) {
Toast.makeText(getApplicationContext()
.getApplicationContext(),
"page "+position,
Toast.LENGTH_LONG).show();
TextView txt=(TextView) findViewById(R.id.txtcoba);
txt.setText("page "+position);
}
};
mPager.setOnPageChangeListener(pageChangeListener);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
`
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtcoba"
style="?android:textAppearanceMedium"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="coba" />
</ScrollView>
I change it on page selected method and change the content of text view. for the toast it's work but for the text doesn't change. but on page 4 when I back to page 3, the page 3 show the page. but the other page not.
what should I do ? thanks before. this is the ScreenSlidePageFragment class
package com.idroid.splashscreen;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
}
this is fragment_screen_slide_page.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtcoba"
style="?android:textAppearanceMedium"
android:padding="16dp"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="coba" />
</ScrollView>
this is activity_screen_slide.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Issue is your trying to set the TextView of Fragment layout.
As per your code your TextView is in the ScreenSlidePageFragment layout.
Remove the 'TextView' setText in onPageeSelcted
Change your ScreenSlidePageFragment like this
private class ScreenSlidePageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View row = inflater.inflate(R.layout.activity_screen_slide, container, false);
TextView txt=(TextView) row.findViewById(R.id.txtcoba);
txt.setText("page using fragment");
return row;
}
}
I have Pageradapter in which i have 3 fragments.
I have button on first fragment
what i want to achieve is open list of installed app with check box.
I have created activity which shows ListView with check box
I'm unable to open that activity.
MainActivity
package com.aditya.att;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
// Tab titles
//private String[] tabs = { "Setting", "Charts", "Usage", "Add" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating PagerAdapter */
PagerAdapter pAdapter = new PagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
viewPager.setAdapter(pAdapter);
/** Called when the user clicks the Send button */
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
fragment AppSetting
package com.aditya.att;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
public class AppSetting extends Fragment {
public void sendMessage(View view) {
Intent intent = new Intent(getActivity(), MyList.class);
startActivity(intent);
//getActivity().startActivity(intent);
}
}
Activity MyList
package com.aditya.att;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class MyList extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// create an array of Strings, that will be put to our ListActivity
ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<Model> getModel() {
List<Model> list = new ArrayList<Model>();
list.add(get("Linux"));
list.add(get("Windows7"));
list.add(get("Suse"));
list.add(get("Eclipse"));
list.add(get("Ubuntu"));
list.add(get("Solaris"));
list.add(get("Android"));
list.add(get("iPhone"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private Model get(String s) {
return new Model(s);
}
}
Adapter InteractiveArrayAdapter
package com.aditya.att;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
class Model
package com.aditya.att;
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
selected = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
activity_main
<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" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
fragment_setting
<?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:orientation="vertical"
android:background="#5cdf06">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Setting add Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="66dp"
android:layout_marginTop="26dp"
android:drawableBottom="#drawable/btn_toggle_off"
android:text="Button"
android:onClick="sendMessage"/>
</RelativeLayout>
rowbuttonlayout
<?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="wrap_content" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="30px" >
</TextView>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
</RelativeLayout>
PagerAdapter
package com.aditya.att;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
public Bundle data;
/** Constructor of the class */
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
AppSetting myFragment = new AppSetting();
/*data= new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);*/
return myFragment;
case 1:
AppActivity myFragment1 = new AppActivity();
/*data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment1.setArguments(data);*/
return myFragment1;
}
AppSetting myFragment2 = new AppSetting();
/*data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment2.setArguments(data);*/
return myFragment2;
}
/** Returns the number of pages */
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
// Top Rated fragment activity
return "Setting";
case 1:
// Games fragment activity
return "Usage";
case 2:
// Movies fragment activity
return "Actitvity";
case 3:
// Add fragment activity
return "Add";
}
return null;
}
}
there are two more fragments
1-AppActivity
1-AppUsage
but there is no code for them yet
I hope I have cleared everything.
mainactivity loads with pagertitlestrip---> on setting ---> click on button---> open activity list of installed app with check box.(used ListView as placeholder )
exact feature available in app lock where user is asked to select apps to lock.
Thank You
Intent intent = new Intent(getActivity(), Activity_name.class);
startActivity(intent);