Misunderstanding with viewpager - android

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;
}

Related

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){
}
});
}

How to implement imageview and button in pager

I want to implement ImageView and Button in ViewPager. But when I tried to implement it just crash my app and I am not able to implement it, Please help me as I am new for Android.
public class FullScreenImageActivity extends Activity {
private static int NUM_VIEWS = 5;
private MyPagerAdapter adapter;
private ViewPager pager;
int gotbiscuit;
public String TAG = "hello";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_view);
Intent i = getIntent();
int gotbiscuit = i.getExtras().getInt("position");
adapter = new MyPagerAdapter(this);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}
public class MyPagerAdapter extends PagerAdapter {
public Integer[] images1 = { R.drawable.i_3, R.drawable.i_4,
R.drawable.i_6 };
public Integer[] images2 = { R.drawable.i_8, R.drawable.i_9,
R.drawable.i_10 };
public Context mContext;
public MyPagerAdapter(Context context) {
super();
this.mContext = context;
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return images1.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == ((ImageView) object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((ImageView) view);
}
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(container
.getContext());
View view1 = inflater.inflate(R.layout.fullscreen_view, null);
ImageView view = (ImageView) findViewById(R.id.imgDisplay);
Button btn = (Button) findViewById(R.id.wallb);
// view1.setScaleType(ScaleType.CENTER_CROP);
switch (gotbiscuit) {
case 0:
view.setImageResource(images1[position]);
break;
case 1:
view.setImageResource(images2[position]);
break;
}
view.setAdjustViewBounds(true);
((ViewPager) container).addView(view1, 0);
return view1;
}
}
}
this is my Pager xml file for pagerview
<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/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
This
int gotbiscuit = i.getExtras().getInt("position"); // local to onCreate
should ve
gotbiscuit = i.getExtras().getInt("position"); // already declared
You also have
setContentView(R.layout.fullscreen_view);
and inflate the same layout
View view1 = inflater.inflate(R.layout.fullscreen_view, null);
should probabbly be
setContentView(R.layout.pager_view); // pager xml
Modify the below according to your requirement
public class MainActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager)findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = null;
view = inflater.inflate(R.layout.pagerview, null);
((ViewPager) collection).addView(view, 0);
Button btn = (Button)view.findViewById(R.id.button1);
ImageView iv = (ImageView)view.findViewById(R.id.imageView1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
}
});
return view;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
}
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"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
pagerview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="176dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Snap
Yes it can be done by something like this ...
i made an xml layout file and named it settings_merge.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/set_settings"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/share" />
<Button
android:id="#+id/facebook_share"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/facebook" />
Now in my viewpager activity layout i used include tag to inflate the above layout
<FrameLayout
android:id="#+id/transparentlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<include layout="#layout/settings_merge" />
</FrameLayout>
and then finally in your PagerActivity
ImageView set_settings;
Button facebook_share;
set_settings=(ImageView)findViewById(R.id.set_settings);
//the onclick listener
set_settings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Do whatever you want to do
}
});
and you are done ..:)

Display grid view with multiple images? android

I want to display the grid view with multiple images on my Galaxy Tab...
I am new to the development, i will really appreciate for any help.
Vivek
public class MainActivity extends Activity {
GridView gv1;
GridViewAdapter madapter;
int arr[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String arr1[]={"ashu","hello","yes","no"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv1=(GridView)findViewById(R.id.gridView1);
madapter=new GridViewAdapter();
gv1.setAdapter(madapter);
}
Adapter class:
class GridViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
return arr1.length; }
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position; }
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0; }
#Override public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater infla=getLayoutInflater();
View v=infla.inflate(R.layout.main1,null);
TextView tv1=(TextView)v.findViewById(R.id.textView1);
ImageView iv1=(ImageView)v.findViewById(R.id.imageView1);
tv1.setText(arr1[position]);
iv1.setBackgroundResource(arr[position]);
return v; }}
}
activity_main.xml
<GridView
android:id="#+id/gridView1" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/> </RelativeLayout>
main1.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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:text="TextView" /> </RelativeLayout>

Android ViewPager

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.

Categories

Resources