Android ViewPager - android

I wanna make the event. when I push the button 3, I want to move ViewPager1(position 0->1) also ViewPager2(position 0->1) at the same time.
here is my code
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_a"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_b"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
viewpager_a1.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="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager1(Position 0)" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button1" />
</LinearLayout>
viewpager_a2.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="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager1(Position 1)" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button2" />
</LinearLayout>
viewpager_b1.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="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager2(Position 0)" />
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button3" />
</LinearLayout>
viewpager_b2.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="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="ViewPager2(Position 1)" />
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button4" />
</LinearLayout>
main.java
public class MainActivity extends Activity {
private ViewPager viewpagerA;
private ViewPager viewpagerB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpagerA =(ViewPager)findViewById(R.id.viewpager_a);
viewpagerA.setAdapter(new AdpaterA(getApplicationContext(),viewpagerA));
viewpagerB =(ViewPager)findViewById(R.id.viewpager_b);
viewpagerB.setAdapter(new AdpaterB(getApplicationContext(),viewpagerB));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ViewPagerApdaterA.java
public class AdpaterA extends PagerAdapter {
public LayoutInflater mInflater;
public Context mContext;
public ViewPager mViewPager;
public AdpaterA(Context c, ViewPager pager) {
super();
mContext = c;
mInflater = LayoutInflater.from(c);
mViewPager=pager;
mViewPager.setAdapter(this);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
#Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}
#Override
public Object instantiateItem(View pager, int position) {
View v = null;
if (position == 0) {
v = mInflater.inflate(R.layout.viewpager_a1, null);
Button button1 = (Button) v.findViewById(R.id.button1);
button1.setOnClickListener(mPagerClickListener);
} else if (position == 1) {
v = mInflater.inflate(R.layout.viewpager_a2, null);
Button button2 = (Button) v.findViewById(R.id.button2);
button2.setOnClickListener(mPagerClickListener);
}
((ViewPager) pager).addView(v, 0);
return v;
}
private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
changeviewpager(1);
Toast.makeText(mContext, "Button 1", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
changeviewpager(2);
Toast.makeText(mContext, "Button 2", Toast.LENGTH_SHORT).show();
break;
}
}
};
public void changeviewpager(int type) {
if(type==1){
mViewPager.setCurrentItem(1);
}else if(type==2){
mViewPager.setCurrentItem(0);
}
}
#Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
#Override
public void finishUpdate(View arg0) {
}
}
ViewPagerAdpaterB.java
public class AdpaterB extends PagerAdapter {
public LayoutInflater mInflater;
public Context mContext;
public ViewPager mViewPager;
public AdpaterB(Context c, ViewPager pager) {
super();
mContext = c;
mInflater = LayoutInflater.from(c);
mViewPager=pager;
mViewPager.setAdapter(this);
}
#Override
public int getCount() {
return 2;
}
#Override
public boolean isViewFromObject(View pager, Object obj) {
return pager == obj;
}
#Override
public Object instantiateItem(View pager, int position) {
View v = null;
if (position == 0) {
v = mInflater.inflate(R.layout.viewpager_b1, null);
Button button3 = (Button) v.findViewById(R.id.button3);
button3.setOnClickListener(mPagerClickListener);
} else if (position == 1) {
v = mInflater.inflate(R.layout.viewpager_b2, null);
Button button4 = (Button) v.findViewById(R.id.button4);
button4.setOnClickListener(mPagerClickListener);
}
((ViewPager) pager).addView(v, 0);
return v;
}
private View.OnClickListener mPagerClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button3:
changeviewpager(1);
Toast.makeText(mContext, "Button 3", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
changeviewpager(2);
Toast.makeText(mContext, "Button 4", Toast.LENGTH_SHORT).show();
break;
}
}
};
public void changeviewpager(int type) {
if(type==1){
mViewPager.setCurrentItem(1);
}else if(type==2){
mViewPager.setCurrentItem(0);
}
}
#Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {
}
#Override
public void finishUpdate(View arg0) {
}
}
from here, there is no error. but i don't know how to add the code....
when push the button3, viewpager2 is moved from position '0' to '1'
and also viewpager1 is moved from position '0' to '1'.
help me~

You can use
//change 1 to whatever page you want
viewPager.setCurrentItem(1, true);
to change to any arbitrary page. The second parameter is smoothScroll, if you set it to true the pager will smoothly scroll to the new position if set to false it will just jump from one to the other.
See the docs for ViewPager to learn more.

Related

Activity's background become black when i entered in activity or onLongPress of Listview

I am trying to implement one ListView in activity. In my XML there is a ListView to fill that ListView data I have invoked one WebService in AsynkTask. I have used BaseAdapter to set that ListView. Now, Whenever I came on that activity the background becomes black for fraction seconds of time and than disappear. Same things happened when I longpress on ListView. The screen shot had attached with this question as well as following are the code that I have implemented.
Main Activity'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:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.agraeta.user.btl.ComboOfferListActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#color/strip_bg"
android:textColor="#color/white"
android:gravity="center"
android:textSize="17sp"
android:text="COMBO OFFERS"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollingCache="false"
android:cacheColorHint="#color/white"
android:id="#+id/list_comboOffers"
android:background="#color/white"
android:divider="#android:color/transparent"
android:dividerHeight="5dp"/>
</LinearLayout>
Java File Of Main Activity :
public class ComboOfferListActivity extends AppCompatActivity implements Callback<ComboOfferData> {
ListView list_comboOffers;
ComboOfferListAdapter offerListAdapter;
List<ComboOfferItem> offerItemList=new ArrayList<>();
AdminAPI adminAPI;
Custom_ProgressDialog dialog;
String userID="";
String roleID="";
AppPrefs prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combo_offer_list);
prefs=new AppPrefs(this);
adminAPI=ServiceGenerator.getAPIServiceClass();
dialog=new Custom_ProgressDialog(this,"Please Wait...");
dialog.setCancelable(false);
userID=prefs.getUserId();
roleID=prefs.getUserRoleId();
if(roleID.equals(C.ADMIN) || roleID.equals(C.COMP_SALES_PERSON) || roleID.equals(C.DISTRIBUTOR_SALES_PERSON)){
roleID=prefs.getSubSalesId();
userID=prefs.getSalesPersonId();
}
fetchIDs();
setActionBar();
}
private void fetchIDs() {
list_comboOffers=(ListView) findViewById(R.id.list_comboOffers);
offerListAdapter=new ComboOfferListAdapter(offerItemList,ComboOfferListActivity.this);
list_comboOffers.setAdapter(offerListAdapter);
list_comboOffers.setCacheColorHint(Color.TRANSPARENT);
list_comboOffers.requestFocus(0);
list_comboOffers.setScrollingCacheEnabled(false);
dialog.show();
Log.e("UR",userID+"-->"+roleID);
Call<ComboOfferData> offerDataCall=adminAPI.comboOfferDataCall(userID,roleID);
offerDataCall.enqueue(this);
}
private void setActionBar() {
// TODO Auto-generated method stub
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.actionbar_design);
View mCustomView = mActionBar.getCustomView();
ImageView image_drawer = (ImageView) mCustomView.findViewById(R.id.image_drawer);
ImageView img_home = (ImageView) mCustomView.findViewById(R.id.img_home);
ImageView img_notification = (ImageView) mCustomView.findViewById(R.id.img_notification);
FrameLayout unread = (FrameLayout) mCustomView.findViewById(R.id.unread);
image_drawer.setImageResource(R.drawable.ic_action_btl_back);
img_home.setVisibility(View.GONE);
img_notification.setVisibility(View.GONE);
unread.setVisibility(View.GONE);
image_drawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
#Override
public void onResponse(Call<ComboOfferData> call, Response<ComboOfferData> response) {
dialog.dismiss();
offerItemList.clear();
ComboOfferData offerData=response.body();
if(offerData.isStatus()){
offerItemList.addAll(offerData.getOfferItemList());
}
else {
Globals.Toast2(this,offerData.getMessage());
}
Log.e("Size","-->"+offerItemList.size());
offerListAdapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<ComboOfferData> call, Throwable t) {
dialog.dismiss();
Globals.showError(t,this);
}
#Override
public void onBackPressed() {
finish();
}
}
Adapter Class :
public class ComboOfferListAdapter extends BaseAdapter {
List<ComboOfferItem> offerItemList=new ArrayList<>();
Activity activity;
LayoutInflater inflater;
public ComboOfferListAdapter(List<ComboOfferItem> offerItemList, Activity activity) {
this.offerItemList = offerItemList;
this.activity = activity;
inflater=activity.getLayoutInflater();
}
#Override
public int getCount() {
return offerItemList.size();
}
#Override
public Object getItem(int position) {
return offerItemList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.layout_combooffer_list,parent,false);
holder.img_offerThumb=(ImageView) convertView.findViewById(R.id.img_offerThumb);
holder.txt_offerTitle=(TextView) convertView.findViewById(R.id.txt_offerTitle);
holder.card_comboOffer = (CardView) convertView.findViewById(R.id.card_comboOffer);
holder.layout_combo = (LinearLayout) convertView.findViewById(R.id.layout_combo);
convertView.setTag(holder);
}
else {
holder= (ViewHolder) convertView.getTag();
}
Picasso.with(activity).load(Globals.IMAGE_LINK+offerItemList.get(position).getImagePath())
.into(holder.img_offerThumb);
holder.txt_offerTitle.setText(offerItemList.get(position).getOfferTitle());
holder.img_offerThumb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, ComboOfferActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("combo_id", offerItemList.get(position).getOfferID());
activity.startActivity(intent);
}
});
holder.txt_offerTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(activity, ComboOfferActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("combo_id", offerItemList.get(position).getOfferID());
activity.startActivity(intent);
}
});
return convertView;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
Log.e("Called","-->"+offerItemList.size());
}
private class ViewHolder {
ImageView img_offerThumb;
TextView txt_offerTitle;
CardView card_comboOffer;
LinearLayout layout_combo;
}
}
Layout File For Adapter's View :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_comboOffer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical"
cardBackgroundColor="#fff"
app:cardCornerRadius="2dp"
app:cardElevation="5dp"
app:theme="#style/CardView.Dark">
<LinearLayout
android:id="#+id/layout_combo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img_offerThumb"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/txt_offerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f58634"
android:padding="5dp"
android:textColor="#color/white"
android:layout_gravity="center"
android:textSize="15sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>

Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference

I tried the navigation drawer. However, an exception is occurred.
Why does the error message occur?
Would you mind if you can give me the solution of this problem?
MainActivity.java
public class MainActivity extends Activity implements OnPageChangeListener, OnClickListener {
private ListView listView;
private ViewPager mPager;
private String[] navItems = {"Brown", "Cadet Blue", "Dark Olive Green", "Dark Orange", "Golden Rod"};
private ListView lvLeftSlideMenu;
private FrameLayout flMainContent;
private DrawerLayout dlDrawer;
private ActionBarDrawerToggle dtToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
mAdapter = new MainListAdapter(this);
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
...
}
});
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(new MyPagerAdapter(getApplicationContext()));
flMainContent = (FrameLayout) findViewById(R.id.fl_main_content);
lvLeftSlideMenu = (ListView) findViewById(R.id.lv_left_slide_menu);
lvLeftSlideMenu.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, navItems));
lvLeftSlideMenu.setOnItemClickListener(new DrawerItemClickListener());
dlDrawer = (DrawerLayout)findViewById(R.id.dl_activity_main_drawer);
dtToggle = new ActionBarDrawerToggle(this, dlDrawer, R.drawable.ic_drawer, R.string.open_drawer, R.string.close_drawer) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
dlDrawer.setDrawerListener(dtToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
dtToggle.syncState(); // exception occurred...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(dtToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
dtToggle.onConfigurationChanged(newConfig);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
switch(position){
case 0:
flMainContent.setBackgroundColor(Color.parseColor("#A52A2A"));
break;
case 1:
flMainContent.setBackgroundColor(Color.parseColor("#5F9EA0"));
break;
case 2:
flMainContent.setBackgroundColor(Color.parseColor("#556B2F"));
break;
case 3:
flMainContent.setBackgroundColor(Color.parseColor("#FF8C00"));
break;
case 4:
flMainContent.setBackgroundColor(Color.parseColor("#DAA520"));
break;
}
dlDrawer.closeDrawer(lvLeftSlideMenu);
}
}
private class MyPagerAdapter extends PagerAdapter {
private LayoutInflater mInflater;
public MyPagerAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return 2;
}
#Override
public Object instantiateItem(View pager, int position) {
View v = null;
if(position==0) {
v = mInflater.inflate(R.layout.main_menu_01, null);
...
} else if(position==1) {
v = mInflater.inflate(R.layout.main_menu_02, null);
...
}
((ViewPager)pager).addView(v, 0);
return v;
}
#Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager)pager).removeView((View)view);
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
#Override public void finishUpdate(View arg0) { }
#Override public void restoreState(Parcelable arg0, ClassLoader arg1) {}
#Override public Parcelable saveState() { return null; }
#Override public void startUpdate(View arg0) {}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
#Override
public void onPageScrollStateChanged(int arg0) {}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
#Override
public void onPageSelected(int position) {}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return false;
}
}
activity_main.xml
<!-- Main Layout -->
<FrameLayout
android:id="#+id/fl_main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/all_blank" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/titlebar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/main_tit" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<TextView
android:id="#+id/userInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10px"
android:gravity="left"
android:textColor="#color/grayblack"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/userInfo" >
</LinearLayout>
<TextView
android:id="#+id/userJisa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1"
android:layout_marginRight="10px"
android:gravity="left"
android:textColor="#color/grayblack"
android:textSize="12dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sub_tit_01"
android:orientation="vertical" >
<ImageView
android:id="#+id/btnRefresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="1.0dip"
android:paddingRight="6.0dip"
android:src="#drawable/icon_refresh" />
</LinearLayout>
<LinearLayout
android:id="#+id/topBg"
android:layout_width="match_parent"
android:layout_height="164dp"
android:background="#drawable/top_bg"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/sub_tit_02"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bottom_bg"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="200dip" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/location_area"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/page_mark"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="horizontal" />
<TextView
android:id="#+id/versionInfo"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:gravity="bottom"
android:textColor="#color/grayblack"
android:textSize="12sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/notice_bg"
android:gravity="center"
android:orientation="horizontal" >
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
</ViewFlipper>
</LinearLayout>
</LinearLayout>
</FrameLayout>
<!-- Main Layout -->
<!-- Left Slide Menu -->
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/lv_left_slide_menu"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#EDEDED"/>
<!-- Left Slide Menu -->
error message
FATAL EXCEPTION: main
Process: com.ex.himan.safetyPatrol, PID: 19664
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.himan.safetyPatrol/com.ex.himan.safetyPatrol.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2712)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2779)
at android.app.ActivityThread.access$900(ActivityThread.java:179)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1462)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5974)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference
at com.ex.himan.safetyPatrol.MainActivity.onPostCreate(MainActivity.java:260)
at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1200)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2694)
... 10 more
getActionBar() method returns null so you are getting NullPointerException.Use ActionBarActivity instead of Activity like below
MainActivity extends ActionBarActivity
and instead of getActionBar() use getSupportActionBar() instead.Hope it will help.

How to implement click listener for a button in android Page Viewer?

I want to create a simple Page Control in android.... I want to move from one page to another page scrolling horizontally, like the home screen in android device.
I have multiple layout in xml like main.xml, layout_first.xml, layout_second.xml and layout_third.xml
Now I have a simple button in my layout_first.xml, I want to implement a click listener for the button like
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
No I don't know where to put the above code
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myfivepanelpager"/>
</LinearLayout>
Here is my layout_first.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="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Here is my layout_second.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="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my layout_third.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="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my java code
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#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;
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount()
{
// TODO Auto-generated method stub
return 3;
}
public Object instantiateItem(View collection, int position)
{
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position)
{
case 0:
resId = R.layout.layout_first;
break;
case 1:
resId = R.layout.layout_second;
break;
case 2:
resId = R.layout.layout_third;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#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;
}
}
}
After View view = inflater.inflate(resId, null); add the following:
if(position == 0){
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
});
}

Misunderstanding with viewpager

Can I do different activity in instantiateItem() ?
please check my screenshot link..
In this screen shot,red TextView is wrote in instantiateItem()....When drag the screen,it follow...
So,I want to insert different layout and activity instead of this TextView..Can I code these in instantiateItem() ?
http://tinypic.com/view.php?pic=zlagwn&s=5#.UqbeWye_80k
This is MyPageAdapter.java
public class MyPageAdapter extends PagerAdapter {
private Context ctx;
public MyPageAdapter(Context ctx) {
this.ctx = ctx;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater vi=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.main, null);
Button btnRefresh=(Button) v.findViewById(R.id.btnRefreshGold);
btnRefresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// my operation
}
});
((ViewPager)container).addView(v,0);
return v;
}
#Override
public int getCount() {
return 3;
}
#Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
this is main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/Tab1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnRefreshGold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnRefresh" />
<ListView
android:id="#+id/myListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
Add this to your instantiateItem() :
Button button = (Button) container.findViewById(R.id.btnRefreshGold);
button.setOnClickListener(new View.OnClickListener(){
#override
public void onClick(View v){
//do what you want your button to do when clicked here
});
You can go follow this link to see a sample of what your viewpager should look like.
This is my answer..fix at instantiateItem()
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater vi=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.main, null);
Button btnRefresh=(Button) v.findViewById(R.id.btnRefreshGold);
btnRefresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// my operation
}
});
((ViewPager)container).addView(v,0);
return v;
}

CirclePageIndicator on top of ViewPager

In my layout I am trying to get the CirclePageIndicator to sit at the bottom of my image and for my image to be aligned at the top of the screen. Whatever I try it doesn't work. Here is how I want it to look like:
What it ends up looking like is this
Here is my code for this.
<RelativeLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="top">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="3dp"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/pager"/>
</RelativeLayout>
And because it is a ViewPager I added the ability to scroll through each image. Here is the code for that.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int[] Images = new int[] { R.drawable.homephoneicon1, R.drawable.homephoneicon2,
R.drawable.homephoneicon3, R.drawable.homephoneicon4, R.drawable.homephoneicon5};
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return SlideshowFragment.newInstance(Images[position]);
}
#Override
public int getCount() {
return NUM_SLIDES;
}
}
Here is the Slideshow code
public class SlideshowFragment extends Fragment {
int imageResourceId;
public static SlideshowFragment newInstance(int i) {
SlideshowFragment fragment = new SlideshowFragment();
fragment.imageResourceId = i;
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
image.setImageResource(imageResourceId);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
layout.setGravity(Gravity.TOP);
layout.addView(image, params);
return layout;
}
}
Thanks for any help!
Here is how I did it
Code
public static SlideshowFragment newInstance(int i) {
SlideshowFragment fragment = new SlideshowFragment();
fragment.imageResourceId = i;
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
image.setImageResource(imageResourceId);
image.setScaleType(ImageView.ScaleType.FIT_XY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout layout = new RelativeLayout(getActivity());
layout.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
layout.addView(image, params);
return layout;
}
Layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
late comer but seems a better workaround
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="#dimen/height"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_alignBottom="#+id/pager"/>
activity_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=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:background="#ffffff"
android:layout_height="300dp">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="300dp"></android.support.v4.view.ViewPager>
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="#android:color/transparent"
android:gravity="center_horizontal"
android:textSize="50sp" />
</RelativeLayout>
view_pager_item.xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/pagerItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAlignment="center" />
</RelativeLayout>
ViewPagerAdapter.java:-
public class ViewPagerAdapter extends PagerAdapter {
Context con;
String[] data;
public ViewPagerAdapter(Context con, String[] data) {
this.data = data;
this.con = con;
}
#Override
public int getCount() {
return data.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view_pager_item, container, false);
try {
TextView textView = (TextView) view.findViewById(R.id.pagerItem);
textView.setText(data[position]);
((ViewPager) container).addView(view);
} catch (Exception ex) {
ex.printStackTrace();
}
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
MainActivity.java:-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] data = new String[]{
"page 1",
"page 2",
"page 3",
"page 4"
};
final ViewPagerAdapter adapter = new ViewPagerAdapter(this, data);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(adapter);
final TextView textView = (TextView) findViewById(R.id.textView);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
textView.setText("");
for (int i = 0; i < adapter.getCount(); i++) {
Spannable word = new SpannableString(" " + ".");
if (i == position) {
word.setSpan(new ForegroundColorSpan(Color.RED), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.append(word);
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Simple way import pagerlibrary and use this code into .xml file.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_above="#+id/twoImage"
android:id="#+id/slideLay"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/offer_pager"
/>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/page_indicatorr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
app:fillColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="10dp"
android:padding="5dip"
>
</com.viewpagerindicator.CirclePageIndicator>
</RelativeLayout>

Categories

Resources