android dynamic text with view pager - android

I have implemented a view pager in android app. I want dynamic text views to be displayed at the top and bottom of the image view. How to set different texts for every image on view pager? Let me know your suggestions.
Thanks.

You need to create custom PagerAdapter for this. I will show you a littel example :
Your Activity :
public class MainActivity
extends Activity{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_main);
List<CustomObject> items = new ArrayList<CustomObject>();
items.add(new CustomObject("First Top","First Bot"));
items.add(new CustomObject("Second Top","Second Bot"));
items.add(new CustomObject("Third Top","Third Bot"));
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(this,items);
viewPager.setAdapter(customPagerAdapter);
}}
Your adapter :
public class CustomPagerAdapter
extends PagerAdapter{
List<CustomObject> items;
LayoutInflater inflater;
public CustomPagerAdapter(Context context, List<CustomObject> items)
{
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((RelativeLayout) object);
}
#Override
public int getCount()
{
return items.size();
}
#Override
public void destroyItem(ViewGroup container, int position, Object object)
{
((ViewPager) container).removeView((View)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position)
{
View itemView;
itemView = inflater.inflate(R.layout.your_item_layout, container, false);
TextView topTextItem = (TextView) itemView.findViewById(R.id.topText);
TextView bottomTextItem = (TextView) itemView.findViewById(R.id.bottomText);
CustomObject customObject = items.get(position);
topTextItem.setText(customObject.topText);
bottomTextItem.setText(customObject.bottomText);
((ViewPager) container).addView(itemView);
return itemView;
}}
Main layout - view_main :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Layout item - your_item_layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/topText"
style="#android:style/TextAppearance.Medium"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Top Text" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#android:drawable/ic_delete" />
<TextView
style="#android:style/TextAppearance.Medium"
android:layout_alignParentBottom="true"
android:id="#+id/bottomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="Bottom Text" />
Best wishes.

you can do this in your Fragment class
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.spannerbord_image_new, null);
// RelativeLayout
// layout=(RelativeLayout)v.findViewById(R.id.spanner_image_relative);
Log.v("TEST", "is this printing everytime");
TextView tv = (TextView ) v.findViewById(R.id.my_tv );
}
i hope this works

Related

OnClickListener not working with fragments

I'm cant get the onclicklistener to work with fragments. I've searched stackoverflow and tried all the tips but i still cant get it to work. So i do my first post here. I've tried adding android:focusable="false", android:clickable="false" and android:descendantFocusability="blocksDescendants" to the layouts with no luck. Ive removed them because they make no difference. I've tried other solutions as well but none of them works. This is my first post so if i posted something wrong let me know and ill redo it, I've borrowed someones customlist just to get a working example.
Here is one of the fragments i want a clickable list in
public class ActivitiesFragment extends Fragment {
ListView list;
String[] maintitle ={
"Aktivitet 1","Aktivitet 2",
"Aktivitet 3","Aktivitet 4",
"Aktivitet 5",
};
String[] subtitle ={
"A","B",
"C","D",
"E",
};
Integer[] imgid={
R.drawable.ic_dashboard_black_24dp,R.drawable.ic_dashboard_black_24dp,
R.drawable.ic_dashboard_black_24dp,R.drawable.ic_dashboard_black_24dp,
R.drawable.ic_dashboard_black_24dp,
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_activities, container, false);
MyListAdapter adapter = new MyListAdapter(getActivity(), maintitle, subtitle,imgid);
list = (ListView) rootView.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
Toast.makeText(getActivity().getApplicationContext(),"One",Toast.LENGTH_SHORT).show();
}
else if(position == 1) {
Toast.makeText(getActivity().getApplicationContext(),"Two",Toast.LENGTH_SHORT).show();
}
else if(position == 2) {
Toast.makeText(getActivity().getApplicationContext(),"Three",Toast.LENGTH_SHORT).show();
}
else if(position == 3) {
Toast.makeText(getActivity().getApplicationContext(),"Four",Toast.LENGTH_SHORT).show();
}
else if(position == 4) {
Toast.makeText(getActivity().getApplicationContext(),"Five",Toast.LENGTH_SHORT).show();
}
}
});
return rootView;
}
}
Here is the Adapter:
public class MyListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] maintitle;
private final String[] subtitle;
private final Integer[] imgid;
public MyListAdapter(Activity context, String[] maintitle,String[] subtitle, Integer[] imgid) {
super(context, R.layout.mylist, maintitle);
// TODO Auto-generated constructor stub
this.context=context;
this.maintitle=maintitle;
this.subtitle=subtitle;
this.imgid=imgid;
}
public View getView(int position,View rowView,ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.mylist, null,true);
TextView titleText = (TextView) rowView.findViewById(R.id.title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
titleText.setText(maintitle[position]);
imageView.setImageResource(imgid[position]);
subtitleText.setText(subtitle[position]);
return rowView;
}
}
The xml for the fragment:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
The xml for the list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp" />
</LinearLayout>
</LinearLayout>
Here's whole implementation of RecyclerView with item click listener.
Fragment:
public class MyFragment extends Fragment implements ItemClickListener {
RecyclerView rvList;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_activities, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
rvList = view.findViewById(R.id.rvList);
ArrayList<ItemData> list = new ArrayList<>();
list.add(new ItemData("Aktivitet 1","A",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 2","B",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 3","C",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 4","D",R.drawable.ic_dashboard_black_24dp))
list.add(new ItemData("Aktivitet 5","E",R.drawable.ic_dashboard_black_24dp))
RVAdapter adapter = new RVAdapter(this, list);
rvList.setLayoutManager(new LinearLayoutManager(getContext()));
rvList.setAdapter(adapter);
}
#Override
public void onItemClicked(ItemData data, int position) {
// item click will be listened here
Toast.makeText(getContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
Fragment Layout: frag.xml
<?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.support.v7.widget.RecyclerView
android:id="#+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Layout for the list: item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#4d4d4d"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
RecyclerView ViewHolder:
public class RecyclerVH extends RecyclerView.ViewHolder {
private TextView titleText;
private ImageView imageView;
private TextView subtitleText;
public RecyclerVH(#NonNull View itemView) {
super(itemView);
titleText = itemView.findViewById(R.id.title);
imageView = itemView.findViewById(R.id.icon);
subtitleText = itemView.findViewById(R.id.subtitle);
}
void bind(ItemData data) {
titleText.setText(data.getMainTitle());
imageView.setImageResource(data.getImgId());
subtitleText.setText(data.getSubTitle());
}
}
RecyclerView Adapter:
public class RVAdapter extends RecyclerView.Adapter<RecyclerVH> {
private ArrayList<ItemData> list;
private ItemClickListener listener;
public RVAdapter(ItemClickListener listener, ArrayList<ItemData> list) {
this.list = list;
this.listener = listener;
}
#NonNull
#Override
public RecyclerVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new RecyclerVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull final RecyclerVH holder, int position) {
holder.bind(list.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClicked(list.get(holder.getAdapterPosition()),
holder.getAdapterPosition());
}
});
}
#Override
public int getItemCount() {
return list.size();
}
}
interface ItemClickListener {
void onItemClicked(ItemData data, int position);
}
Update 1:
Add these lines to the root tag of item layout:
android:focusable="true"
android:clickable="true"
Like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:orientation="horizontal">
...
</LinearLayout>
Time ago I had a similar problem, with an ImageView in my list item. My solution was changing android:focusable to false inside the ImageView block. I never knew why, but it worked fine.
Anyway, I strongly recommend to start using RecyclerView and ViewHolder pattern. https://developer.android.com/guide/topics/ui/layout/recyclerview
It's much more powerful, flexible and a major enhancement over ListView.

Fragment - Adding image in spinner

strings.xml
<string-array name="meal_array">
<item>Breakfast</item>
<item>Lunch</item>
<item>Dinner</item>
</string-array>
dietary.xml
<Spinner
android:id="#+id/ddl_meal_type"
android:layout_width="140dp"
android:layout_height="25dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:entries="#array/meal_array"
app:layout_constraintBottom_toBottomOf="#+id/lbl_meal_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.016"
app:layout_constraintStart_toEndOf="#+id/lbl_meal_type"
app:layout_constraintTop_toTopOf="#+id/lbl_meal_type"
app:layout_constraintVertical_bias="1.0"
tools:layout_conversion_absoluteHeight="24dp"
tools:layout_conversion_absoluteWidth="140dp" />
meal_layout.xml
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
android:src="#drawable/ic_action_food"
/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
/>
Dietary.java
public class Dietary extends Fragment{
View myView; //A view object called myView
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.dietary, container, false);
Spinner mySpinner = (Spinner)myView.findViewById(R.id.ddl_meal_type);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.meal_layout, R.id.txt, R.array.meal_array);
mySpinner.setAdapter(adapter);
return myView;
}
}
I want to add images beside the text in the spinner. The code in Dietary.java does not seems to work. I am doing it using fragment.
Can anybody help me with this?
Your Dietary.java
public class Dietary extends Fragment{
View myView; //A view object called myView
int img[] = {0,R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round};
Spinner mySpinner;
SpinnerAdapter spinnerAdapter;
String[] mealArry = {"Select Meal","Breakfast", "Lunch", "Dinner"};
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.dietary, container, false);
mySpinner = (Spinner)myView.findViewById(R.id.ddl_meal_type);
spinnerAdapter = new SpinnerAdapter(mealArry, img, Main2Activity.this);
spinner.setAdapter(spinnerAdapter);
return myView;
}
}
Your meal_layout.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".2"
android:padding="10dp" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".8"
android:padding="10dp" />
</LinearLayout>
Your dietary.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">
<Spinner
android:id="#+id/ddl_meal_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" />
</LinearLayout>
Now for Image with Text for spinner you need to make a custom adapter.
For this first create a new java file which will extends BaseAdapter, baseAdapter has 4 override methods.
1.)
#Override
public int getCount() {
}
2.)
#Override
public Object getItem(int i) {
}
3.)
#Override
public long getItemId(int i) {
}
4.)
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
}
So below is your SpinnerAdapter:
public class SpinnerAdapter extends BaseAdapter {
String[] mealPlan;
int[] mealPlanImage;
LayoutInflater layoutInflater;
Context context;
public SpinnerAdapter(String[] mealPlan, int[] mealPlanImage, Context context) {
this.mealPlan = mealPlan;
this.mealPlanImage = mealPlanImage;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mealPlan.length;
}
#Override
public Object getItem(int i) {
return mealPlan[i];
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = layoutInflater.inflate(R.layout.meal_layout, null);
ImageView img = (ImageView) view.findViewById(R.id.img);
TextView txt = (TextView) view.findViewById(R.id.txt);
img.setImageResource(mealPlanImage[i]);
txt.setText(mealPlan[i]);
return view;
}
}
and finally your view will be like below image:
I hope you got your expected result.

Android GridView: getView() in adapter never gets called

I am Trying to insert GridView in fragment. I have developed custom adapter to populate the gridview. Now everything is working fine but the gridview is not displaying in the fragment. I have check the getCount() method it is returning total length. So that means data is there but it is not displaying. I am pretty new with gridview so have no idea why it is happening. I tried the following answer but they didn't help me either.
Android: getView in adapter never called
Custom Adapter getView() method is not called
Gridview not displaying in fragment
Here is my code:
Tile Fragment Class:
public class TileFragment extends Fragment
{
public TileFragment(){
}
GridView gv;
Context context;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.mipmap.dicover_disable,R.mipmap.discover_active, R.mipmap.home_disable,
R.mipmap.home_active,R.mipmap.lists_disable,R.mipmap.lists_active,
R.mipmap.profile_disable,R.mipmap.profile_active,R.mipmap.ic_search };
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
#SuppressLint("InflateParams")
// View v = LayoutInflater.from(getActivity()).inflate(R.layout.tile_fragment, null);
View v = inflater.inflate(R.layout.tile_fragment, container, false);
gv = (GridView) v.findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(getActivity(), prgmNameList,prgmImages));
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
Custom Adapter:
public class CustomAdapter extends BaseAdapter {
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(FragmentActivity tileFragment, String[] prgmNameList, int[] prgmImages) {
result=prgmNameList;
imageId=prgmImages;
context = tileFragment;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return imageId.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.programlist, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
tile_fragment:
<?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" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3"
android:visibility="visible">
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>
prgramlist
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
This is how I am adding the fragment:
HomeFragment:
public class HomeFragment extends Fragment
{
public HomeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
FragmentTabHost mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager());
Bundle b = new Bundle();
b.putString("key", "Tile");
mTabHost.addTab(mTabHost.newTabSpec("tile").setIndicator("Tile"), TileFragment.class, b);
//
b = new Bundle();
b.putString("key", "Map");
mTabHost.addTab(mTabHost.newTabSpec("map").setIndicator("Map"), ProfileFragment.class, b);
return mTabHost;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
//
}
Any Help will be appreciated ...
Try this:
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
where R.layout.my_parent_fragment is your layout fragment where you have the tabhost.
Once you have verified that the GridView is displayed correctly in an Activity, we could deduce that this is not a problem with the Gridview neither the Adapter, the problem come from your FragmentTabHost definition or setup. You are returning your tabHost from your onCreateView(), for that you must set your layout or container in your tabHost, if you don't do this, how the fragment know where find your layout?
For more info, api here
Can't figure out the root of the problem but try this:
View rowView = convertView;
if(rowView != null) {
rowView = inflater.inflate(R.layout.programlist, null);
}
Try using this layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
>
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</LinearLayout>

ViewPager does not show content

I am trying to create a ViewPager that shows a list of ImageViews. I am able to implement the pager and adapter properly, and it works as expected on swiping. However, it does not show the content of the ImageView properly, i.e. the image itself. Its as if the images are there, but they are transparent. Only one image is visible out of the whole lot.
Here is my code-
ImagePagerAdapter.java
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater inflater;
List<ImageView> views = new ArrayList<>();
boolean[] done = {false,false,false,false,false};
ItemDetailFragment idf;
public ImagePagerAdapter(ItemDetailFragment idf,
LayoutInflater inflater) {
this.idf = idf;
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(idf.getActivity());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
public ImagePagerAdapter(LayoutInflater inflater) {
this.inflater = inflater;
for (int i = 0; i < getCount(); i++) {
ImageView iv = new ImageView(inflater.getContext());
iv.setImageResource(R.drawable.light_grey_background);
views.add(iv);
}
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (done[position]) {
return views.get(position);
}
ImageView v = views.get(position);
views.set(position, v);
((ViewPager) container).addView(v);
done[position] = true;
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return true;
}
}
ItemDetailFragment.java : This is where I set the adapter.
public class ItemDetailFragment extends Fragment {
ViewPager pager;
ImagePagerAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
pager = (ViewPager) rootView.findViewById(R.id.pager);
adapter = new ImagePagerAdaper(this, inflater);
pager.setAdapter(adapter);
}
fragment_item_detail.xml
<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:orientation="vertical"
android:padding="16dp"
tools:context="com.trial.piclist.ItemDetailFragment" >
<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="200dip" >
</android.support.v4.view.ViewPager>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/item_title"
style="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
android:textIsSelectable="true" />
<Space
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/controls_layout" />
</TableRow>
<ScrollView
android:id="#+id/descScrollView"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/item_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Please Help. Thanks.
Use:
public class ImagePagerAdapter extends PagerAdapter {
LayoutInflater Inflater;
Activity act;
int count;
public ViewPagerAdapter(Activity a, int c) {
act = a;
count=c;
Inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return count;
}
#Override
public Object instantiateItem(View collection, int position) {
View v1 = Inflater.inflate(R.layout.element_image, null);
ImageView image = (ImageView) v1
.findViewById(R.id.about_image);
image.setImageResource(R.drawable.light_grey_background);
((ViewPager) collection).addView(v1, 0);
return v1;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
Make a layout element_image.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/smv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Replace
adapter = new ImagePagerAdaper(this, inflater);
with
adapter = new ImagePagerAdaper(getActivity(), count_of_images);
initiate the ImageView in the instantiateItem() method, set the LayoutParams of the imageView
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(params);
Also set the bg colour for debugging
imageView.setBackgroundColor(Color.GREEN);
viewPager.setBackgroundColor(Color.RED);

ViewPager doesn't instantiate

I'm having a weird problem and I can't figure out why my ViewPager won't instantiate. According to all kinds of SO questions and tutorials, I'm doing it correctly, but when I print to the stack, it looks like the instantiateItem() method doesn't run at all, even though I'm calling setAdapter(adapter) in the onCreateView of my parent fragment. Everything above the ViewPager inflates fine, but the ViewPager just shows blank nothing.
Here's the code.
public class Profile extends Fragment {
ListView userInfo;
FeedAdapter adapter;
ViewPager pager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.user_profile, container, false);
ProfilePagerAdapter adapter = new ProfilePagerAdapter();
pager = (ViewPager) view.findViewById(R.id.profileViewPager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
TextView userName = (TextView) view.findViewById(R.id.userName);
userName.setText(Utility.userName);
Log.i("Profile", "Fragment and Pager instantiated");
return view;
}
private class ProfilePagerAdapter extends PagerAdapter{
public ProfilePagerAdapter(){
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch(position){
case 0:
resId = R.layout.news_feed;
View view1 = inflater.inflate(resId,null);
ListView feedList;
FeedAdapter feedAdapter;
feedList = (ListView) view1.findViewById(R.id.feedList);
feedAdapter = new FeedAdapter(getActivity(), Utility.newsFeed);
feedAdapter.notifyDataSetChanged();
feedList.setAdapter(feedAdapter);
((ViewPager) container).addView (view1,0);
Log.i("Profile", "Instantiated case 0");
return view1;
case 1:
resId = R.layout.splash;
View view2 = inflater.inflate(resId,null);
((ViewPager) container).addView(view2,0);
Log.i("Profile", "Instantiated case 1");
return view2;
}
return resId;
}
#Override
public int getCount() {
return 0;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
And here's the xml layout.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/profileLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/coverPhoto"
android:layout_width="match_parent"
android:layout_height="133dp"
android:layout_alignParentTop="true"
android:background="#color/grey_color" />
<ImageView
android:id="#+id/profilePhoto"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="100dp"
android:background="#color/com_facebook_blue" />
<TextView
android:id="#+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/coverPhoto"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#id/profilePhoto"
android:text="User&apos;s Name"
android:textSize="24sp" />
<LinearLayout
android:id="#+id/profileButtonBar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="#id/profilePhoto"
android:layout_marginTop="5dp"
android:background="#33b5e5"
android:orientation="horizontal" >
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/profileViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/profileButtonBar"
android:layout_marginTop="5dp"
android:layout_weight="1" />
</RelativeLayout>
It's because you're telling it you have 0 items:
#Override
public int getCount() {
return 0;
}

Categories

Resources