How to combine two view in ViewPager Android? - android

I'm creating ViewPager with CirclePageIndicator.There are two View inside. Right now, It's showing two View in one page. But when i scroll to next the previous one is showing. It means it is showing like previous one + new.
Current Look like:
ItemA, ItemB | ItemB, ItemC | ItemC, ItemD |
Expected look like
ItemA, ItemB | ItemC, ItemD | ItemE, ItemF |
How can i fix it?
Is there any way to combine this two ItemA and ItemB?
Here is my code.
Adapter
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
ViewPager pager;
String[] domain;
String[] title;
int[] flag;
LayoutInflater inflater;
public ViewPagerAdapter(ViewPager pager,Context context, String[] domain,
String[] title, int[] flag) {
this.pager= pager;
this.context = context;
this.domain = domain;
this.title = title;
this.flag = flag;
}
#Override
public int getCount() {
//Show the CirclePageIndicator for only half of array.
return (int) Math.ceil((double)title.length/2);
// return title.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.related_article_item, container,
false);
// Locate the TextViews in viewpager_item.xml
final Button btnSave = (Button) itemView.findViewById(R.id.btn_save);
final Button btnLike = (Button) itemView.findViewById(R.id.btn_like);
final TextView tv_domain = (TextView) itemView.findViewById(R.id.domain_text);
TextView tv_title = (TextView) itemView.findViewById(R.id.title_text);
ResizableImageView imageView = (ResizableImageView) itemView.findViewById(R.id.imageViewDynamic);
final ProgressBar progressBar = (ProgressBar) itemView.findViewById(R.id.loading);
// Capture position and set to the TextViews
tv_domain.setText(domain[position]);
tv_title.setText(title[position]);
// Locate the ImageView in viewpager_item.xml
imageView.setImageResource(flag[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
//This is the code to show two view inside one page.
#Override
public float getPageWidth(int position) {
return (0.5f);
}
// return (super.getPageWidth(position) / 2);
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
MainActivity
public class TestingActivity extends ActionBarActivity {
// Declare Variables
ViewPager viewPagerRelated;
PagerAdapter adapter;
String[] title;
String[] domain;
int[] flag;
ViewPager pager1;
CirclePageIndicator mIndicator;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.viewpager_main);
title = new String[]{"Sample1", "Sample2", "Sample3",
"Sample4", "Sample5", "Sample6", "Sample7", "Sample8",
"Sample9", "Sample10"};
domain = new String[]{"text1", "text1",
"text1", "text1", "text1", "text1",
"text1", "text1", "text1", "text1"};
flag = new int[]{R.drawable.arsenal, R.drawable.aston_villa,
R.drawable.manchester_city, R.drawable.liverpool,
R.drawable.chelsea, R.drawable.manchester_united, R.drawable.swansea,
R.drawable.liverpool, R.drawable.west_brom, R.drawable.west_ham};
// Locate the ViewPager in viewpager_main.xml
viewPagerRelated = (ViewPager) findViewById(R.id.pager2);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(pager1, ArticleViewActivityV2.this, domain,
title, flag);
// Binds the Adapter to the ViewPager
viewPagerRelated.setAdapter(adapter);
// viewPagerRelated.setPageMargin(3);
// ViewPager Indicator
mIndicator = (CirclePageIndicator) findViewById(R.id.indicator_pager);
mIndicator.setViewPager(viewPagerRelated);
}
}
XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="#layout/related_article_item"
android:id="#+id/article_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<include
layout="#layout/related_article_item"
android:id="#+id/article_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
Item 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#drawable/gridview_item_selector"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<RelativeLayout
android:id="#+id/imageLayout"
android:layout_width="200dp"
android:layout_height="100dp">
<com.bindez.news.utils.ResizableImageView
android:id="#+id/imageViewDynamic"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="#drawable/app_icon" />
<ProgressBar
android:id="#+id/loading"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/imageLayout"
android:layout_marginTop="7dp"
android:maxLines="10"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="7dp"
android:text="Title Text"
android:textColor="#color/tile_text"
android:textSize="14sp" />
<TextView
android:id="#+id/domain_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_text"
android:layout_marginBottom="3dp"
android:ellipsize="end"
android:maxLines="1"
android:paddingBottom="7dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text=""
android:textColor="#color/text_gray"
android:textSize="12sp" />
<TextView
android:id="#+id/seperator"
android:layout_width="fill_parent"
android:layout_height="1px"
android:layout_below="#+id/domain_text"
android:background="#color/light_gray_seperator" />
<Button
android:id="#+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/seperator"
android:background="#android:color/transparent"
android:drawableRight="#drawable/ic_unsave"
android:minHeight="0dp"
android:minWidth="0dp"
android:paddingBottom="5dp"
android:paddingRight="7dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/btn_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/seperator"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/ic_unlike"
android:drawablePadding="5dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:paddingBottom="5dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="5dp"
android:text=""
android:textColor="#color/btn_green"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>

Related

How to make Horizontal line attach textview in recyclerview?

Am having a doubt regarding how to make the horizontal view to touch the textview while populating in recyclerview , Now let me explain briefly i having a horizontal recyclerview in that adapter view holder i have horizontal view at the end of the textview which is center vertical to that textview, so when items are added each view must touch the textview like vertical dotted timeline but the view is not touching the textview as shown in the below image.
Now let me post what i have tried so far
This is the layout that holding recyclerview:
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerview"/>
</RelativeLayout>
Here am using that recyclerview in activity:
Recyclerview recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recyclerView.setHasFixedSize(true);
TrackingAdapter mAdapter = new TrackingAdapter(this.taskLogModelList,mTaskModel.getTaskID(),fm,String.valueOf(mLastLocation.getLatitude()),String.valueOf(mLastLocation.getLongitude()));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
Here is the adapter am using :
public class TrackingAdapter extends RecyclerView.Adapter<TrackingAdapter.ViewHolder> {
private List<TaskLogModel> taskStatusFlowModels;
private D2DKnocks appclass;
private String Lat,Long;
private FragmentManager fragmentManager;
private String TaskID;
DateFormat date,msimpleDateformat;
public TrackingAdapter(List<TaskLogModel> taskStatusFlowModels,String TaskID,FragmentManager context,String Lat,String Long) {
this.taskStatusFlowModels = taskStatusFlowModels;
this.fragmentManager=context;
date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
msimpleDateformat = new SimpleDateFormat("hh.mm a");
this.Lat=Lat;
this.TaskID=TaskID;
this.Long=Long;
appclass=((D2DKnocks)D2DKnocks.getContext());
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private AvertaTextview txt_time;
private Rubik txt_delivered;
// private AvertaButton btn_start;
private RelativeLayout layout;
private View view_right;
public ViewHolder(View itemView) {
super(itemView);
txt_time = itemView.findViewById(R.id.txt_time);
// btn_start=itemView.findViewById(R.id.start);
layout=itemView.findViewById(R.id.layout);
txt_delivered = itemView.findViewById(R.id.txt_delivered);
view_right = (View) itemView.findViewById(R.id.right_view_line);
layout.setOnClickListener(this);
}
#Override
public void onClick(View view) {
TaskLogModel taskLogModel=taskStatusFlowModels.get(getAdapterPosition());
if(taskLogModel.getTaskStatusID()==0){
TaskStatusDialog taskStatusDialog=new TaskStatusDialog();
Bundle bundle=new Bundle();
bundle.putInt("Size",taskStatusFlowModels.size());
bundle.putString("Lat",Lat);
bundle.putString("Long",Long);
bundle.putString("TaskID",TaskID);
taskStatusDialog.setArguments(bundle);
taskStatusDialog.show(fragmentManager,"show");
}
}
}
#Override
public TrackingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_view_assignment_status, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(TrackingAdapter.ViewHolder holder, int position) {
if (taskStatusFlowModels.get(position).getTaskStatusID() > 0) {
StatusModel statusFlowModel = getStatusName(taskStatusFlowModels.get(position).getTaskStatusID());
holder.txt_delivered.setText(statusFlowModel.getStatus());
holder.txt_delivered.setBackgroundResource(R.drawable.btn_arrived_bg);
try {
Date uppdateDate=date.parse(taskStatusFlowModels.get(position).getUpdateDateTime());
String updatedDate=msimpleDateformat.format(uppdateDate);
String[] each = updatedDate.split(" ");
String str = each[1].replace("AM", "am").replace("PM","pm");
// SpannableString ss1= new SpannableString(each[0]);
// SpannableString ss2=new SpannableString(each[1]);
// ss1.setSpan(new AbsoluteSizeSpan(20), 0, each[0].length(), SPAN_INCLUSIVE_INCLUSIVE);
// ss2.setSpan(new AbsoluteSizeSpan(15), 0, each[1].length(), SPAN_INCLUSIVE_INCLUSIVE);
// CharSequence finalText = TextUtils.concat(ss1, " ", ss2);
holder.txt_time.setText(each[0]+" "+str);
} catch (ParseException e) {
e.printStackTrace();
}
GradientDrawable gradientDrawable = (GradientDrawable) holder.txt_delivered.getBackground();
gradientDrawable.setColor(Color.parseColor(statusFlowModel.getStatusColor()));
} else {
holder.txt_delivered.setText("Choose");
holder.txt_delivered.setBackgroundResource(R.drawable.btn_choose_bg);
holder.txt_delivered.setTextColor(Color.parseColor("#b1b1b1"));
}
if(taskStatusFlowModels.size()-1==position){
holder.view_right.setVisibility(View.GONE);
}
else {
holder.view_right.setVisibility(View.VISIBLE);
}
}
public StatusModel getStatusName(Integer statusID){
for (int i=0;i<appclass.getStatusModelist().size();i++){
if (appclass.getStatusModelist().get(i).getStatusID().equals(statusID)){
return appclass.getStatusModelist().get(i);
}
}
return null;
}
#Override
public int getItemCount() {
return taskStatusFlowModels.size();
}
}
And below is the view holder layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layout">
<com.trident.Hawkersky.service.CustomFonts.AvertaTextview
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:drawableLeft="#drawable/ic_checked"
android:drawablePadding="10dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="10.00 am"
android:textColor="#444444"
android:textSize="12sp"
/>
<View
android:id="#+id/vertical_line"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/txt_time"
android:background="#drawable/vertical_dotted_line"
android:layerType="software"
android:visibility="visible" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_below="#+id/vertical_line"
android:layout_height="wrap_content">
<com.trident.Hawkersky.service.CustomFonts.Rubik
android:id="#+id/txt_delivered"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="#dimen/thirty"
android:layout_marginBottom="5dp"
android:singleLine="true"
android:padding="#dimen/seven"
android:background="#drawable/btn_delivered_bg"
android:gravity="center"
android:text="Delivered"
android:textAllCaps="true"
android:textColor="#FFF"
android:textSize="13sp"
android:textStyle="bold" />
<View
android:id="#+id/right_view_line" <--- this is the view
android:layout_width="#dimen/hundred"
android:layout_height="5dp"
android:layout_alignBottom="#+id/txt_delivered"
android:layout_gravity="center_vertical"
android:layout_marginBottom="12dp"
android:layout_toEndOf="#+id/txt_delivered"
android:layout_toRightOf="#+id/txt_delivered"
android:background="#drawable/dotted"
android:layerType="software"
android:visibility="visible" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
Now what i need is i need that view to attach to the textview , but am getting like image above how to solve it.
Please try this and let know if you need further modifications.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<com.trident.Hawkersky.service.CustomFonts.AvertaTextview
android:id="#+id/txt_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:drawableLeft="#drawable/ic_checked"
android:drawablePadding="10dp"
android:gravity="center"
android:text="10.00 am"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="#+id/txt_delivered"
app:layout_constraintRight_toRightOf="#+id/txt_delivered"
android:textColor="#444444"
android:textSize="12sp"
/>
<View
android:id="#+id/vertical_line"
android:layout_width="30dp"
android:layout_height="20dp"
app:layout_constraintLeft_toLeftOf="#+id/txt_delivered"
app:layout_constraintRight_toRightOf="#+id/txt_delivered"
android:layout_below="#+id/txt_time"
android:layout_centerHorizontal="true"
app:layout_constraintTop_toBottomOf="#+id/txt_time"
android:background="#color/colorPrimaryDark"
android:layerType="software"
android:visibility="visible"/>
<com.trident.Hawkersky.service.CustomFonts.Rubik
android:id="#+id/txt_delivered"
android:layout_width="wrap_content"
android:layout_height="#dimen/thirty"
android:layout_marginBottom="5dp"
android:background="#drawable/btn_delivered_bg"
android:gravity="center"
app:layout_constraintTop_toBottomOf="#+id/vertical_line"
android:padding="#dimen/seven"
android:singleLine="true"
android:text="Delivered"
android:textAllCaps="true"
android:textColor="#FFF"
android:textSize="13sp"
android:textStyle="bold"/>
<View
android:id="#+id/right_view_line"
android:layout_width="100dp"
android:layout_height="5dp"
app:layout_constraintTop_toTopOf="#+id/txt_delivered"
app:layout_constraintBottom_toBottomOf="#+id/txt_delivered"
app:layout_constraintLeft_toRightOf="#+id/txt_delivered"
android:background="#drawable/dotted"
android:layerType="software"
android:visibility="visible"/>
</android.support.constraint.ConstraintLayout>
Sorry, I had to use ConstraintLayout. If you haven't been using it before, please add this implementation 'com.android.support.constraint:constraint-layout:1.0.2'1 to your module level build.gradle.

Android viewpager not showing?

I have this layout
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/header_cover_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:background="#color/com_facebook_button_background_color_pressed" />
<refractored.controls.CircleImageView
android:id="#+id/imageProfile"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="#+id/header_cover_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="-60dp"
android:layout_gravity="center_horizontal"
android:elevation="5dp"
android:padding="20dp"
android:scaleType="centerCrop" />
<RelativeLayout
android:id="#+id/profile_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_cover_image"
android:background="#ebca0707"
android:elevation="4dp"
android:paddingBottom="24dp">
<ImageView
android:id="#+id/add_friend"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:src="#drawable/played" />
<ImageView
android:id="#+id/drop_down_option_menu"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:src="#drawable/wins"
android:layout_width="30dp"
android:layout_toLeftOf="#+id/drop_down_option_menu"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp" />
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/userStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/user_name"
android:text="Status"
android:textColor="#fff"
android:textSize="14sp"
android:layout_marginLeft="16dp"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/profilePager"
>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_layout"
android:elevation="6dp"
android:background="?android:attr/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_below="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
The last LinearLayout shows the Tabs but when i switch them there is no viewpager data whatsoever . I tried creating another layout for the tablayout but it still wont work .. Im calling this from a fragment as follows :
public class ProfileFragment : Fragment
{
private static Context _context;
private static Xamarin.Facebook.Profile _profile;
private CircleImageView imgProfile;
private TextView userName;
private TabLayout tabLayout;
private ViewPager viewpager;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public static Fragment NewInstance(Activity context , Xamarin.Facebook.Profile profile)
{
_context = context;
_profile = profile;
Fragment fragment = new ProfileFragment();
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fragmentProfile, container, false);
imgProfile = view.FindViewById<CircleImageView>(Resource.Id.imageProfile);
tabLayout =view.FindViewById<TabLayout>(Resource.Id.tab_layout);
viewpager = view.FindViewById<ViewPager>(Resource.Id.pager);
userName = view.FindViewById<TextView>(Resource.Id.user_name);
userName.Text = _profile.Name;
Picasso.With(_context).Load(_profile.GetProfilePictureUri(180, 180)).Into(imgProfile);
FnInitTabLayout();
return view;
}
void FnInitTabLayout()
{
tabLayout.SetTabTextColors(Android.Graphics.Color.Aqua, Android.Graphics.Color.AntiqueWhite);
//Fragment array
var fragments = new Fragment[]
{
new ActivityFragment(),
new SportsFragment(),
new AboutFragment()
};
//Tab title array
var titles = CharSequence.ArrayFromStringArray(new[] {
"Feed",
"Sports",
"About",
});
//viewpager holding fragment array and tab title text
viewpager.Adapter = new ProileTabsAdapter(FragmentManager, fragments, titles);
tabLayout.SetupWithViewPager(viewpager);
}
}
When i put it inside an activity and without the above layout only the tab and the viewpager they work perfectly but in this scenario not . Can anyone think of what am i doing wrong ?
EDIT :
Here is the pager adapter
public class ProileTabsAdapter : FragmentPagerAdapter
{
private readonly Fragment[] fragments;
private readonly ICharSequence[] titles;
public ProileTabsAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;
}
public override int Count
{
get
{
return fragments.Length;
}
}
public override Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return titles[position];
}
}
Use android:fillViewport="true" into your ScrollView.
wrap_content of view pager won't work inside Scrollview.
ViewPager doesn't support width="wrap_content" or height="wrap_content" attrs.
Solutions here : I am unable to have ViewPager WRAP_CONTENT
You can also use this library : WCViewPager (Wrap Content ViewPager)

Android - Call Method With A Button From XML

I've got an app based on Sliding Tabs layout. I want to call the addMedicine method in the PageFragment class from an XML layout with a Button. The issue is that i cannot assign the method to the Button onClick event.
Here's the PageFragment class:
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
// TEMP VARIABLES //
ListView ItemsLst;
String[] Items = {"Medicine 1", "Medicine 2", "Medicine 3"};
TextView output;
EditText nameFld;
EditText formatFld;
EditText amountFld;
EditText exp_dateFld;
EditText timeFld;
DBManager dbAdapt = new DBManager(getActivity());
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
switch(mPage)
{
case 1:
view = inflater.inflate(R.layout.layout_sqltest, container, false);
nameFld = (EditText) view.findViewById(R.id.nameFld);
formatFld = (EditText) view.findViewById(R.id.formatFld);
amountFld = (EditText) view.findViewById(R.id.amountFld);
exp_dateFld = (EditText) view.findViewById(R.id.exp_dateFld);
timeFld = (EditText) view.findViewById(R.id.timeFld);
return view;
case 2:
view = inflater.inflate(R.layout.layout_settings, container, false);
return view;
case 3:
view = inflater.inflate(R.layout.layout_info, container, false);
return view;
}
return view;
}
public void addMedicine() // The method i want to call
{
String name = nameFld.getText().toString();
String format = formatFld.getText().toString();
int amount = Integer.parseInt(amountFld.getText().toString());
String exp_date = exp_dateFld.getText().toString();
String time = timeFld.getText().toString();
long id = dbAdapt.addMedicine(name, format, amount, exp_date, time);
}
}
And here is the layout_sqltest 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/nameLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/nameFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Format"
android:id="#+id/formatLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/formatFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Amount"
android:id="#+id/amountLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/amountFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Expiration Date"
android:id="#+id/exp_dateLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/exp_dateFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Time"
android:id="#+id/timeLbl"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/timeFld"
android:layout_gravity="center_horizontal"
android:inputType="text" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Medicine"
android:id="#+id/addMedicineBtn"
android:layout_gravity="center_horizontal" />
</LinearLayout>
The addMedicineBtn is the button i want to call the method with.
Someone can help me? I really need that!
Thank you in advance!
EDIT: I tried giving to addMedicine method the View param, but it still doesn't appear in the available methods for the addMedicineBtn in the onClick properties (I'm using Android Studio).
Try to use onClickListener
view.findViewById(R.id.addMedicineBtn).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
addMedicine();
}
});
You must add tools:context="PageFragment" line to the container (LinearLayout) of PagerFragment's layout file (layout_sqltest). Then onClick() method will find methods in PagerFragment class.

How to create a listView with multiple views in a row.

I am writing an app that requires the user to enter some details into a form. The form initially has 3 EditText views in a row. When the user enters some data in the last editText, the onFocusChangeListener should initiate a new row.
This is what I want to do.
How can I create 3 editText views in a single listView row at runtime?
To create a listView with multiple views in a row, you need to customize your adapter, so it can take the form of your row from a source XML -that you should create- and puts it into your listView, here is an example based on BaseAdapter:
This is your Xml source that contains the form of your Row:
espaceclientuploadsource.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:addStatesFromChildren="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:addStatesFromChildren="true" >
<ImageView
android:id="#+id/Uplodimage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:src="#drawable/bingo" />
<TextView
android:id="#+id/UploadedProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="24dp"
android:layout_toRightOf="#+id/Uplodimage"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/UplodedProductPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/UploadedProductName"
android:layout_below="#+id/UploadedProductName"
android:layout_marginTop="16dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="#+id/UploadedStatus"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_above="#+id/UplodedProductPrice"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/UploadedProductName"
android:layout_marginRight="27dp"
android:src="#drawable/bingo" />
<ImageView
android:id="#+id/ImageViewDelete"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignBottom="#+id/UplodedProductPrice"
android:layout_alignLeft="#+id/UploadedStatus"
android:clickable="true"
android:src="#drawable/bingo" />
</RelativeLayout>
Then you need to create an XML file that containes your ListView:
espaceclientuploads:
<EditText
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit"
android:text="Button" />
</LinearLayout>
Next is your adapter, it's role is to use your sourceXML as your row Format,
UploadedAdapter :
public class UploadedAdapter extends BaseAdapter
{
List<Article> lesArticles;
DataSource produitSource;
LayoutInflater inflater;
String path= Environment.getExternalStorageDirectory().toString()+File.separator+"Bingo";
public UploadedAdapter(Context context, List<Article> lesArticles)
{
inflater=LayoutInflater.from(context);
this.lesArticles = lesArticles;
}
#Override
public int getCount()
{
return lesArticles.size();
}
#Override
public Object getItem(int position)
{
return lesArticles.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
private class ViewHolder
{
TextView nomduProduit;
TextView prixDuProduit;
ImageView status;
ImageView imageDuProduit;
ImageView delete;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.espaceclientuploadsource, null);
holder.nomduProduit = (TextView)convertView.findViewById(R.id.UploadedProductName);
holder.prixDuProduit = (TextView)convertView.findViewById(R.id.UplodedProductPrice);
holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.Uplodimage);
holder.status = (ImageView)convertView.findViewById(R.id.UploadedStatus);
holder.delete=(ImageView)convertView.findViewById(R.id.ImageViewDelete);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Drawable drawableImage = new BitmapDrawable(bitmapImage);
holder.imageDuProduit.setImageDrawable(drawableImage);
holder.nomduProduit.setText(lesArticles.get(position).getNomArticle());
holder.prixDuProduit.setText(lesArticles.get(position).getPrix());
holder.delete.setImageResource(R.drawable.delete);
return convertView;
}
}
As you can see, your adapter extends the baseAdapter which is an abstract class, so you need to Override its method.
Finally it's your activity that will display your listView:
public class EspaceClientUplodedProducts extends Activity{
List<Article> lesArticle= new ArrayList<Article>();
ListView lvListe;
UploadedAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.espaceclientuploads);
lvListe= (ListView)findViewById(R.id.UploadListView);
adapter = new UploadedAdapter(this, lesArticle);
lvListe.setAdapter(adapter);
lvListe.setOnItemClickListener(this);
}
}

Swipe Image with button and fingers

I'm trying to do a custom gallery with images shown in fullscreen mode and be able to swipe between different images with Previous Next buttons and with fingers.So for now I did the part with changing images with fingers,but I had to redesign my xml file and the whole structure and logic of my code and I need a little help or suggestions of how to add the new stuff.So the old code which I was using to swipe images with fingers was like this :
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());
ImageView img1 = new ImageView(getApplicationContext());
ImageView img2 = new ImageView(getApplicationContext());
ImageView img3 = new ImageView(getApplicationContext());
ImageView img4 = new ImageView(getApplicationContext());
ImageView img5 = new ImageView(getApplicationContext());
ImageView img6 = new ImageView(getApplicationContext());
img1.setImageResource(R.drawable.one);
img2.setImageResource(R.drawable.two);
img3.setImageResource(R.drawable.three);
img4.setImageResource(R.drawable.four);
img5.setImageResource(R.drawable.five);
img6.setImageResource(R.drawable.six);
realViewSwitcher.addView(img1);
realViewSwitcher.addView(img2);
realViewSwitcher.addView(img3);
realViewSwitcher.addView(img4);
realViewSwitcher.addView(img5);
realViewSwitcher.addView(img6);
setContentView(realViewSwitcher);
And this code shows the image in fullscreen mode and I can swipe between them.Now I need to do something like this :
I want to be able to swipe images with buttons and finger.And my second question is how can I hide the bar in top (Back,Galler,Info) and the bar in bottom (Previous,Next) and show the image in Fullscreen mode and still be able to swipe images only with fingers.
Here is how my xml file looks :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/single_card"
android:src="#drawable/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/actionbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:layout_gravity="center"
android:padding="10dp"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/back_button"
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#333333"
android:layout_centerVertical="true"
android:textSize="13dp"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp" />
<TextView
android:text="Gallery"
android:textStyle="bold"
android:textSize="15dp"
android:textColor="#FFFFFF"
android:id="#+id/single_msg_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<Button
android:id="#+id/info_button"
android:text="Info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#333333"
android:layout_centerVertical="true"
android:textSize="13dp"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#000000" >
<Button
android:id="#+id/previous_button"
android:text="Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#333333"
android:layout_alignParentLeft="true"
android:textSize="13dp"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginLeft="50dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true" />
<Button
android:id="#+id/next_button"
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#333333"
android:layout_alignParentRight="true"
android:textSize="13dp"
android:textColor="#ffffff"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</RelativeLayout>
Thanks in advance!!! Any help,suggestions or links with example are welcomed!
Use the view Flow project to do this. your code with adding imageViews is not a good solution at all.
see viewFlow on gitHub. there are examples with images also. all you do is add this layout in the activity, make an image adapter and thats it.
I did something like this a while back, basically a finger swipe left or finger swipe right perform 2 different actions and there are buttons for the same actions that can be clicked.
In my Main Activity I register the finger swipe listener:
gestureDetector = new GestureDetector(new ListItemGestureDetector(this));
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
Then in my Efficient Adapter that renders my list view, I register the buttons and their click (this would not be the same for you but when adding the buttons to my layout I do this (call and sms being buttons in my layout)
static class ViewHolder {
TextView name, phone;
ImageButton call, sms, other;
//ImageView icon;
}
ViewHolder holder = new ViewHolder();
holder.call.setClickable(true);
holder.call.setFocusable(true);
holder.call.setTag("call");
final int pos = position;
holder.call.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(context, "CALL", Toast.LENGTH_SHORT).show();
Cursor c = (Cursor) getItem(pos);
Intent i = new Intent(Intent.ACTION_CALL);
String phoneNumber = c.getString(WaddleAddressBook.DATA_ID);
i.setData(Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneNumber));
context.startActivity(i);
}
});
For Swiping images we can simply use ViewPager concept.
1.set the viewpager in your layout
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
2.MyMain.java
public class MyMain extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.hree,
R.drawable.four
};
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MyMain.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}

Categories

Resources