Using ViewPager from Image url- getting crashed - android

I get no error in compile time but the application is getting crushed the moment I click on gridview item where it shall take me to a pagerview activity.
Here is the complete code for the activity for Viewpager:
package com.example.samer.applicationformatech.activities;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.example.samer.applicationformatech.R;
import com.example.samer.applicationformatech.adapters.ImagePagerAdapter;
import java.util.ArrayList;
public class BirdsGalleryActivity extends AppCompatActivity {
private String img0=getIntent().getExtras().getString("IMG0");
private String img1=getIntent().getExtras().getString("IMG1");
private String img2=getIntent().getExtras().getString("IMG2");
private String img3=getIntent().getExtras().getString("IMG3");
private String img4=getIntent().getExtras().getString("IMG4");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pagerview);
final ArrayList<String> birds = new ArrayList<>();
birds.add(new String(img0));
birds.add(new String(img1));
birds.add(new String(img2));
birds.add(new String(img3));
birds.add(new String(img4));
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter(this,birds);
viewPager.setAdapter(adapter);
}
}
Then the adapter class:
package com.example.samer.applicationformatech.adapters;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.samer.applicationformatech.R;
import com.example.samer.applicationformatech.activities.BirdsGalleryActivity;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ImagePagerAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
ArrayList<String> arrayList;
public ImagePagerAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arrayList = arrayList;
}
public ImagePagerAdapter(BirdsGalleryActivity birdsGalleryActivity) {
}
#Override
public int getCount() {
if(arrayList != null){
return arrayList.size();
}
return 0;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = layoutInflater.inflate(R.layout.image_viewpager_layout, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.viewPagerItem_image1);
Picasso.with(context).load(arrayList.get(position));
// .placeholder(R.drawable.image_uploading)
// .error(R.drawable.image_not_found).into(imageView);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
The XMLs:
<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"
tools:context="com.example.samer.applicationformatech.activities.BirdsGalleryActivity"
>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and
<?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" >
<ImageView
android:id="#+id/viewPagerItem_image1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
Any idea why?

You should initialize your imgN Strings inside the onCreate method, otherwise you're doing it prematurely, and I assume you're getting NullPointerExceptions somewhere in those calls.
The Intent is only available from the onCreate method and onwards, right now you're trying to access it at the time when the constructor of the Activity is running.
public class BirdsGalleryActivity extends AppCompatActivity {
private String img0;
private String img1;
private String img2;
private String img3;
private String img4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pagerview);
img0=getIntent().getExtras().getString("IMG0");
img1=getIntent().getExtras().getString("IMG1");
img2=getIntent().getExtras().getString("IMG2");
img3=getIntent().getExtras().getString("IMG3");
img4=getIntent().getExtras().getString("IMG4");
final ArrayList<String> birds = new ArrayList<>();
birds.add(new String(img0));
birds.add(new String(img1));
birds.add(new String(img2));
birds.add(new String(img3));
birds.add(new String(img4));
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter(this,birds);
viewPager.setAdapter(adapter);
}
}
Some further suggestions:
You can probably leave out the new String() calls when you're creating your list, you're making copies of Strings for no apparent reason.
You could get the extras bundle only once with this new code, like this:
Bundle extras = getIntent().getExtras();
img0 = extras.getString("IMG0");
...

Related

use Edit text to search in recyclerview inside fragment

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

TabLayout or horizontal scrollview Android

Image for the TabLayout that I wanted to achieve.
Can somebody shows me the way to achieve this?
My code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="center"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
</LinearLayout>
I already set the attributes app:tabMode="scrollable" and app:tabGravity="center" but all the tabs just stick to the left in the layout.
I wanna achieve the result like the above image no matter how many the tabs are. Means the position of the tabs will away at the center or the tablayout.
PageFragment.java:
package com.example.testing;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
// In this case, the fragment displays simple text based on the page
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
SampleFragmentPagerAdapter.java:
package com.example.testing;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
TabActivity.java:
package com.example.testing;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class TabAcitivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),TabAcitivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
The tabs start from the left. How do I place it to make it start at the middle? Or is it better to use a horizontal scrollable ListView?
use this for scrolling tabs:
sliding_tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
For any other reference please have a look at the following url.
https://guides.codepath.com/android/google-play-style-tabs-using-tablayout

View Pager(Handling Event Listener)

I created a Image Slider using View Pager.
I Tried using setOnPageChangeListener but It only triggered when
the user change the page of the viewpager.
What I'm doing(still no luck) is when the user tap on the screen
Another layout will appear.
But Im stuck to triggering the TAP event.
Here's my codes.
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.thesis.juandirection.juandirectionfinale.R;
import uk.co.senab.photoview.PhotoViewAttacher;
public class viewInfo extends AppCompatActivity {
private ViewPager viewPager;
private CustomSwipeAdapter adapter;
PhotoViewAttacher mAttacher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_info);
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
Toast.makeText(getBaseContext(), "" + position, Toast.LENGTH_LONG).show();
}
});
}
public class CustomSwipeAdapter extends PagerAdapter {
private int[] image_resources = {R.drawable.bsu, R.drawable.gameover2};
private Context context;
private LayoutInflater layoutInflater;
public CustomSwipeAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return image_resources.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout) object);
}
#Override
public Object instantiateItem(View container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, (ViewGroup) container, false);
ImageView imageView = (ImageView) item_view.findViewById(R.id.image_view);
imageView.setPadding(5, 5, 5, 5);
imageView.setImageResource(image_resources[position]);
((ViewGroup) container).addView(item_view);
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.update();
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
}
And the activit's XML.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pls"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical"
tools:context="com.thesis.juandirection.juandirectionfinale.viewInfos.viewInfo">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"></android.support.v4.view.ViewPager>
</LinearLayout>
And this is the XML layout that will be infalted.
<?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:gravity="center"
android:background="#000"
>
<ImageView
android:id="#+id/image_view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
/>
</LinearLayout>
What I doing is when the user Tap on the Image.
Infos of the image will show on the Same Screen.(Like Facebook viewing photo)

My ViewPager is empty

I've created an about activity in my Android application, with a ViewPager. But I can't see any content
My AboutActivity.java:
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.loloof64.android.chess_positions_archiver.R;
/**
* The about activity
*/
public class AboutActivity extends Activity {
static final int NUM_ITEMS = 2;
AboutDialogFragmentPagerAdapter pagerAdapter;
ViewPager viewPager;
static String pagesContents [] = new String[NUM_ITEMS];
public void exit(View view){
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_dialog_layout);
pagesContents[0] = getString(R.string.this_app_manual);
pagesContents[1] = getString(R.string.about_resources_content);
pagerAdapter = new AboutDialogFragmentPagerAdapter(getFragmentManager(),
new String[]{
"Chess Positions Archiver",
getString(R.string.about_resources_title)
});
viewPager = (ViewPager) findViewById(R.id.about_dialog_pager);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
}
public static class AboutDialogFragmentPagerAdapter extends FragmentStatePagerAdapter {
private String [] titles;
public AboutDialogFragmentPagerAdapter(FragmentManager fragmentManager, String [] titles){
super(fragmentManager);
this.titles = titles;
}
#Override
public Fragment getItem(int position) {
return AboutDialogSingleFragment.newInstance(position);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
public static class AboutDialogSingleFragment extends Fragment {
static String PAGE_TEXT_KEY = "PageTextKey";
private String pageText;
static AboutDialogSingleFragment newInstance(int position){
AboutDialogSingleFragment fragment = new AboutDialogSingleFragment();
Bundle arguments = new Bundle();
arguments.putString(PAGE_TEXT_KEY, pagesContents[position]);
fragment.setArguments(arguments);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.about_dialog_single_page_layout, container, false);
TextView pageTextView = (TextView) view.findViewById(R.id.about_dialog_page_textview);
pageTextView.setText(pageText);
container.addView(view);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageText = getArguments() != null ? getArguments().getString(PAGE_TEXT_KEY) : getResources().getString(R.string.about_dialog_content_error);
}
}
}
My about_dialog_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/about_dialog_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/about_dialog_pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/quit_about"
android:onClick="exit" />
</RelativeLayout>
My about_dialog_single_page_layout.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/about_dialog_page_textview"/>
</LinearLayout>
I am using the support library 13.
So, what is my programming error ?
android:layout_weight="1" works only with LinearLayout (it is a property of LinearLayout). Since the ViewPager's parent is a RelativeLayout the property is ignored, and your ViewPager has height 0, which explain why you are not see anything

Android Programming ViewPager from a GridView

I am new to android programming and am looking to implement a gallery which has a grid view that then pushes on to a view pager but I am having a few problems getting this to work. I have it working currently just using a static Image Adapter but am not sure how to change this to use a view pager instead. I also want to be able to add my own titles to each screen but I wasn't sure how to do this or whether it had to be the image name that would appear?
The code I have at the moment is...
the .xml with the grid view
<?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="#3d3d3e" >
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/phone"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
the grid view .java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Marble extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.marble);
GridView gridView = (GridView) findViewById(R.id.gridview1);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter1(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity1.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
the image adapter .java
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter1 extends BaseAdapter {
private Context mContext;
// Keep all Images in array
Integer[] mThumbIds = {
R.drawable.arabescato, R.drawable.biancocarrara,
R.drawable.botticinoclassico, R.drawable.calacattaoro,
R.drawable.cremamarfil, R.drawable.cremavalencia,
R.drawable.emperadordark, R.drawable.jurabeige,
R.drawable.neromarquina, R.drawable.perlatoolympo,
R.drawable.rojoalicante
};
// Constructor
public ImageAdapter1(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(265, 265));
return imageView;
}
}
and finally the image activity .java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class FullImageActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter1.mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
All help on this will be massively appreciated as I'm absolutely stumped!
Thanks in advance!
Before you proceed, add the android.support.v4 jar file to your project.
You need 2 things: the ViewPager in your layout, and an adapter that extends PagerAdapter
First change your full_image.xml layout to this:
<?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.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Then create an adapter that extends PagerAdapter (required by ViewPager)
public class ImagePagerAdapter extends PagerAdapter {
private List<ImageView> images;
public ImagePagerAdapter(List<ImageView> images) {
this.images = images;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = images.get(position);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
#Override
public int getCount() {
return images.size();
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}
In your FullImageActivity1 class:
public class FullImageActivity1 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// Loop through the ids to create a list of full screen image views
ImageAdapter1 imageAdapter1 = new ImageAdapter1(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < imageAdapter1.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter1.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
images.add(imageView);
}
// Finally create the adapter
ImagePagerAdapter imagePagerAdapter = new ImagePagerAdapter(images);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setAdapter(imagePagerAdapter);
// Set the ViewPager to point to the selected image from the previous activity
// Selected image id
int position = getIntent().getExtras().getInt("id");
viewPager.setCurrentItem(position);
}
}
The general idea is to create an adapter that extends PagerAdapter to supply data to the ViewPager. For more information, you can visit Android Docs on PagerAdapter

Categories

Resources