Implementing floating button on a PagerAdapter class - android

I want, in this code, to make the "Close" button stationary (floating) each time the view changes, something like in this example:
http://www.jondev.net/articles/Floating_Views_in_Android_%28Buttons%29
**XML**
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.myapplication.helper.TouchImageView
android:id="#+id/imgDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter" />
</LinearLayout>
<Button
android:id="#+id/btnClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="#drawable/button_background"
android:textColor="#ffffff"
android:text="Close" />
</FrameLayout>
Also my PagerFragment:
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import java.util.ArrayList;
import com.myapplication.R;
import com.myapplication.helper.TouchImageView;
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private ArrayList<String> _imagePaths;
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
ArrayList<String> imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
#Override
public int getCount() {
return this._imagePaths.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((FrameLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
TouchImageView imgDisplay;
Button btnClose;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
false);
imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
imgDisplay.setImageBitmap(bitmap);
// close button click event
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
_activity.finish();
}
});
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((FrameLayout) object);
}
}
Thanks!

Related

Android: Gridview not displayed in fragment

Good evening, i'm new in Android programming and i'm facing a problem in displaying a gridview in fragment.
I'm loading an inventory list in order to show if one item is checked or not.
I have loaded data in arraylist and i'm sure it is passed correctly to the fragment.
The problem is that the Gridadapter is trying to load only first item (for two times it seems) but the result is blank screen.
This is my fragment code
package testjava.pulsanti;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Fragment2 extends Fragment {
ArrayList<bancale> transactionList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myfragmentview = inflater.inflate(R.layout.fragment_2, container, false);
if (getArguments() != null) {
transactionList = (ArrayList<bancale>) getArguments().getSerializable("bancali");
for (int i = 0; i < transactionList.size(); i++) {
bancale banc = transactionList.get(i);
System.out.println(banc.numeroudm + " " + banc.statoInventario);
}
if (transactionList != null) {
GridView gridView = myfragmentview.findViewById(R.id.GridviewInv);
GridAdapterInv gridAdapter = new GridAdapterInv(this.getContext(), transactionList);
gridView.setAdapter(gridAdapter);
}
}
return myfragmentview ;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onPause() {
super.onPause();
System.out.println("Fragment 2 paused");
// Fragment frag = new Fragment1();
// getParentFragmentManager().beginTransaction().replace(R.id.Viewpager,frag).commit();
}
}
and this is the GridAdapterCode
package testjava.pulsanti;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class GridAdapterInv extends BaseAdapter {
Context context;
ArrayList<bancale> bancali;
LayoutInflater inf;
public GridAdapterInv(Context context, ArrayList<bancale> bancali) {
this.bancali = bancali;
this.context = context;
inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return bancali.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder holder;
if (v == null) {
v = inf.inflate(R.layout.elementogriglia_inv,null,false);
holder = new ViewHolder();
holder.imgView = (ImageView) v.findViewById(R.id.grid_image);
holder.textView = (TextView) v.findViewById((R.id.numUdm));
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
System.out.println(("Position:" + position));
bancale banc = bancali.get(position);
if (banc.statoInventario == 0) {
holder.imgView.setImageResource(R.drawable.unchecked);
}
else {
holder.imgView.setImageResource(R.drawable.checked);
}
holder.textView.setText(banc.numeroudm);
System.out.println(banc.numeroudm);
if (position % 2 == 0) {
v.setBackgroundColor(Color.LTGRAY);
} else {
v.setBackgroundColor(Color.WHITE);
}
return v;
}
public static class ViewHolder {
private TextView textView;
private ImageView imgView;
}
}
and that's my xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".Fragment2"
tools:ignore="ExtraText">
<GridView
android:id="#+id/GridviewInv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:numColumns="1"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
...
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="#+id/Icona"
android:layout_width="48dp"
android:layout_height="49dp"
android:contentDescription="TODO"
app:srcCompat="#drawable/checked" />
<TextView
android:id="#+id/idBancale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
android:textColor="#757575"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I don't understand why the gridadapter is not Loading all items in my arraylist but it seems only loading the first and at the end it isn't displayed.
Someone can help me?
Thanks

Change set of Image resource on Button click in fragment

I have there sets of drawable in int array. Is it possible to change the image resource of the image view in the fragment on button click? for example i have there a method onclicklistener for button benefit_babies... i want to set the image resource into the int array benefitBaby on click of the button.
InformationTab fragment
package com.example.aldrinjohn.sample;
import android.app.Activity;
import android.os.Bundle;
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.TextView;
/**
* Created by Aldrin John on 3/20/2017.
*/
public class InformationTab extends Fragment{
ViewPager viewPager;
CustomSwipeAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.informationtab, container, false);
viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager.setAdapter(adapter);
return rootView;
}
}
XML File:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/trivia"
android:text="BreastFeeding Trivia"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/benefit_babies"
android:text="Benefits to Babies"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/storing"
android:text="Storing Breastmilk"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/pump"
android:text="How to Pump Breastmilk"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/benefit_mother"
android:text="Benefits to Mothers"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/tips"
android:text="Tips and Information"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/image_view"/>
</LinearLayout>
JAVA Code:
package com.example.aldrinjohn.sample;
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.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.android.gms.tagmanager.Container;
/**
* Created by Aldrin John on 3/26/2017.
*/
public class CustomSwipeAdapter extends PagerAdapter implements View.OnClickListener{
private int[] trivia = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4, R.drawable.c5,R.drawable.c6};
private int[] benefitBaby = {R.drawable.c7,R.drawable.c8,R.drawable.c9};
private int[] benefitMother = {R.drawable.c91,R.drawable.c92};
private int[] info = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,
R.drawable.a8,R.drawable.a9,R.drawable.a91,R.drawable.a92};
private int[] storing = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4};
private Context ctx;
private LayoutInflater layoutInflater;
Button btnbenefitB;
public CustomSwipeAdapter(Context ctx)
{
this.ctx = ctx;
}
#Override
public int getCount() {
return trivia.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
btnbenefitB = (Button)item_view.findViewById(R.id.benefit_babies);
btnbenefitB.setOnClickListener(this);
ImageView imageView = (ImageView)item_view.findViewById(R.id.image_view);
imageView.setImageResource(trivia[position]);
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
public int getCount(int[] x) {
return x.length - 1;
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.benefit_babies:
break;
}
}
}
imageview.setImageResource(id) where id is in your int array. See the documentation for ImageView for details. It is the same as you have in your code just in the onClick() handler.
setImageResource
void setImageResource (int resId)
Sets a drawable as the content of this ImageView.
This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
Hi everyone thanks for the help. I solved it by using view page adapter. Every onclick button I will change the adapter. Thank you!!

how to get image view from grid layout to alert dialog

I have an array of images, and wanted to display image from it, to alert dialog when we click on grid item.
Here is my class:
mainactivity.java
package glassfibre.hospital2;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity
{
GridView grid;
String[] web = {
"Food",
"Doctor",
"Water",
"Feel Cold",
"Adjust Bed",
"Toilet"
} ;
int[] imageId = {
R.drawable.food,
R.drawable.doctor,
R.drawable.water2,
R.drawable.lowtemp,
R.drawable.bed,
R.drawable.toilet
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
mydialog dialog = new mydialog();
dialog.show(getSupportFragmentManager(),"my_dialog");
}
});
}
}
CustomGrid.java
package glassfibre.hospital2;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
#Override
public int getCount() {
return web.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
grid_single.xml
<RelativeLayout 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="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/grid_image"
android:src="#drawable/food"
android:layout_width="150dp"
android:layout_height="150dp">
</ImageView>
<TextView
android:id="#+id/grid_text"
android:text="#string/food"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#id/grid_image"
android:gravity="center"
android:textSize="20sp" >
</TextView>
</RelativeLayout>
mydialog.java
package glassfibre.hospital2;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.zip.Inflater;
import static android.R.attr.gridViewStyle;
import static android.R.attr.id;
import static android.R.attr.thumbPosition;
import static glassfibre.hospital2.R.layout.grid_single;
public class mydialog extends DialogFragment implements View.OnClickListener
{
Button bt;
ImageView iv;
CustomGrid cg;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.my_dialog,null);
bt=(Button)view.findViewById(R.id.button);
bt.setOnClickListener(this);
iv= (ImageView) view.findViewById(R.id.imageView);
setCancelable(false);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
// > here i wanted to set image which i click on grid view
return view;
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.button)
{
dismiss();
}
}
}
my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="I want"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:id="#+id/textView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"
android:textSize="20dp"/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="#drawable/food"
android:id="#+id/imageView"
android:layout_marginTop="19dp"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true" />
<Button
android:text="OK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="#+id/button" />
</RelativeLayout>
I have an array of images, and wanted to display image from it, to alert dialog when we click on grid item.
Do something like this:
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
mydialog dialog = new mydialog();
int imageResource = imageId[position];
dialog.setImageResource(imageResource);
dialog.show(getSupportFragmentManager(),"my_dialog");
}
});
And in your mydialog class declare int variable
private int imageResource;
And create a method:
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
now you can set image resource to imageview in onCreateView method of mydialog class
Inside your myDialog class,just have a setter method which stores the Drawable Resource id , like shown below
public void setImageIdToDisplay(int aDrawableId) {
mDrawableId = aDrawableId;
}
and in your oncreateview method of myDialog class, just set the image resource id to the imageview
iv.setImageResource(mDrawableId);
and in your MainActivity's onItemClick listner of gridview, pass the drawable resource id as shown below
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
mydialog dialog = new mydialog();
dialog.setImageIdToDisplay(imageId[position]);
dialog.show(getSupportFragmentManager(),"my_dialog");
}
});

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)

Unable to get custom spinner in android

I have followed this link to learn about implementing custom spinner. My requirement is just to get drop down menu on clicking a image, it should be like a drop down menu where in i load contents to the list from backend.
But i thought that sample example posted in the above link would work. but its not giving any list on clicking a image icon.
Here is the full source code.
package com.example.testdroid;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
public class MainActivity extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createCustomActionBar();
}
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
// return true;
// }
private void createCustomActionBar()
{
List<String> links = new ArrayList<String>();
links.add("Abc");
links.add("Def");
links.add("Ghi");
LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.externallink, links);
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
spinner.setAdapter(linkAdapter);
ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
private static class LinksAdapter extends ArrayAdapter<String> {
private List<String> strings;
private Context context;
private LinksAdapter(Context mainActivity, int textViewResourceId, List<String> objects) {
super(mainActivity, textViewResourceId, objects);
this.strings = objects;
this.context = mainActivity;
}
#Override
public int getCount() {
if (strings == null) return 0;
return strings.size();
}
#Override
public String getItem(int position) {
return super.getItem(position);
}
// return views of drop down items
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final String siteLink = strings.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// at 0 position show only icon
TextView site = (TextView) inflater.inflate(R.layout.externallink, null);
site.setText(siteLink);
site.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
//context.startActivity(i);
}
});
return site;
}
// return header view of drop down
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.custom_show_action_bar, null);
}
}
}
Here is my XML file, i.e layout xml file.
custom_show_action_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="20dp"
android:layout_gravity="center"
/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:background="#ffffff"
android:id="#+id/refresh"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:background="#ffffff"
android:layout_gravity="center"
android:id="#+id/settings"/>
</LinearLayout>
External_links.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:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Guide me where i'm wrong.

Categories

Resources