VideoView Loader Android - android

I have implemented videoview in one of my fragment. It's working fine but there is one problem I am unable to set a loader or progress bar over the videoview. My progressbar is working for the whole screen but I only want it over the videoview so that user can understand that a video is loading.
Below is my java and xml code.Please help me with the solution.
FragmentTwo.java
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;
/**
* A simple {#link Fragment} subclass.
*/
public class FragmentTwo extends Fragment {
public FragmentTwo() {
// Required empty public constructor
}
public static VideoView videoview;
private static ProgressBar progressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view= inflater.inflate(R.layout.fragment_two, container, false);
videoview=(VideoView)view.findViewById(R.id.video);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
return view;
}
void storeRecievedUrl(String url){
String VideoURL = url;
System.out.println(VideoURL);
// progressDialog = ProgressDialog.show(getActivity(), "", "Buffering Video...", true);
try {
MediaController mediacontroller = new MediaController(getActivity());
mediacontroller.setAnchorView(videoview);
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
videoview.start();
progressBar.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i1) {
progressBar.setVisibility(View.GONE);
mediaPlayer.start();
}
});
}
});
}
}
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:layout_width="match_parent"
android:id="#+id/video"
android:layout_height="wrap_content" >
</VideoView>
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/progressBar1"/>
</LinearLayout>
Below is main activity xml on which i am inflating my fragment two.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:id="#+id/top"
android:layout_height="350dp">
</FrameLayout>
<FrameLayout
android:layout_width="340dp"
android:layout_marginTop="20dp"
android:id="#+id/frgten"
android:layout_below="#+id/top"
android:layout_marginLeft="20dp"
android:layout_marginRight="32dp"
android:layout_height="130dp"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

Please use the following xml to replace your fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<VideoView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</FrameLayout>

Try using RelativeLayout in fragment_two.xml instead of LinearLayout and put your progressbar on top of videoview.

Related

how to use asset folder to display in gridview in android studio

i making a gridview to open pdf files from asset
i have used download directory to show pdf file but now i want to use asset
And i am changing main activity and pdf activity to fragments
Guys help me with asset folder
sample code
CustomAdaptor.java
package com.tutorials.hp.gridviewpdf;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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 android.widget.Toast;
import java.util.ArrayList;
public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<PDFDoc> pdfDocs;
public CustomAdapter(Context c, ArrayList<PDFDoc> pdfDocs) {
this.c = c;
this.pdfDocs = pdfDocs;
}
#Override
public int getCount() {
return pdfDocs.size();
}
#Override
public Object getItem(int i) {
return pdfDocs.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view==null)
{
//INFLATE CUSTOM LAYOUT
view= LayoutInflater.from(c).inflate(R.layout.model,viewGroup,false);
}
final PDFDoc pdfDoc= (PDFDoc) this.getItem(i);
TextView nameTxt= (TextView) view.findViewById(R.id.nameTxt);
ImageView img= (ImageView) view.findViewById(R.id.pdfImage);
//BIND DATA
nameTxt.setText(pdfDoc.getName());
img.setImageResource(R.drawable.pdf_icon);
//VIEW ITEM CLICK
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openPDFView(pdfDoc.getPath());
}
});
return view;
}
//OPEN PDF VIEW
private void openPDFView(String path)
{
Intent i=new Intent(c,PDF_Activity.class);
i.putExtra("PATH",path);
c.startActivity(i);
}
}
PDF_Activity.java
i will be using fragment instead activity
package com.tutorials.hp.gridviewpdf;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.github.barteksc.pdfviewer.PDFView;
//import com.github.barteksc.pdfviewer.ScrollBar;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import java.io.File;
public class PDF_Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
//PDFVIEW SHALL DISPLAY OUR PDFS
PDFView pdfView= (PDFView) findViewById(R.id.pdfView);
//SCROLLBAR TO ENABLE SCROLLING
// ScrollBar scrollBar = (ScrollBar) findViewById(R.id.scrollBar);
// pdfView.setScrollBar(scrollBar);
//VERTICAL SCROLLING
// scrollBar.setHorizontal(false);
//SACRIFICE MEMORY FOR QUALITY
//pdfView.useBestQuality(true)
//UNPACK OUR DATA FROM INTENT
Intent i=this.getIntent();
String path=i.getExtras().getString("PATH");
//GET THE PDF FILE
File file=new File(path);
if(file.canRead())
{
//LOAD IT
pdfView.fromFile(file).defaultPage(1).onLoad(new OnLoadCompleteListener() {
#Override
public void loadComplete(int nbPages) {
Toast.makeText(PDF_Activity.this, String.valueOf(nbPages), Toast.LENGTH_LONG).show();
}
}).load();
}
}
}
PDFDoc.java
package com.tutorials.hp.gridviewpdf;
import android.net.Uri;
public class PDFDoc {
String name,path;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
MainActivity.java
i will be using fragment instead activity
i need asset folder to show there pdf files
package com.tutorials.hp.gridviewpdf;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.GridView;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final GridView gv= (GridView) findViewById(R.id.gv);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gv.setAdapter(new CustomAdapter(MainActivity.this,getPDFs()));
}
});
}
private ArrayList<PDFDoc> getPDFs()
{
ArrayList<PDFDoc> pdfDocs=new ArrayList<>();
//TARGET FOLDER
File downloadsFolder= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
PDFDoc pdfDoc;
if(downloadsFolder.exists())
{
//GET ALL FILES IN DOWNLOAD FOLDER
File[] files=downloadsFolder.listFiles();
//LOOP THRU THOSE FILES GETTING NAME AND URI
for (int i=0;i<files.length;i++)
{
File file=files[i];
if(file.getPath().endsWith("pdf"))
{
pdfDoc=new PDFDoc();
pdfDoc.setName(file.getName());
pdfDoc.setPath(file.getAbsolutePath());
pdfDocs.add(pdfDoc);
}
}
}
return pdfDocs;
}
}
layout
activity_main.xml
i will be using fragment instead activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.tutorials.hp.gridviewpdf.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
activity_pdf.xml
i will be using fragment instead activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.tutorials.hp.gridviewpdf.PDF_Activity">
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/scrollBar"/>
<com.github.barteksc.pdfviewer.ScrollBar
android:id="#+id/scrollBar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
content_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.tutorials.hp.gridviewpdf.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PDF VIEWER" />
<GridView
android:id="#+id/gv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2" />
</LinearLayout>
</RelativeLayout>
model.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
android:layout_height="200dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/pdfImage"
android:src="#drawable/pdf_icon"
android:layout_width="150dp"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/nameTxt"
android:padding="10dp"
android:textColor="#color/colorAccent"
android:textStyle="bold"
android:layout_alignParentLeft="true"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Include this permission in manifest if you are storing pdf in external storage.
Use Asset folder like this
AssetManager assetManager = getAssets();
File file = new File(getFilesDir(), "xyz.pdf");
in = assetManager.open("xyz.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
You can open it as an input stream like this from a fragment
reader = new BufferedReader(new InputStreamReader(getActivity().getAssets().open(myfile.pdf))
You don't need permisssions as you're accessing your app's internal files but putting pdf into assets can increase the weight of the app , be careful
and then you can save it as string like that
private StringBuilder text = new StringBuilder();
String mLine;
while ((mLine = reader.readLine()) != null) {
text.append(mLine);
text.append('\n');
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}

I want to hide Action Bar while scrolling the webview

Basically i have BottomNavigationView activity in that there are 3 fragments (tab1,tab2,tab3) which contain 3 webviews, what i actually wanna to do is while scrolling down the webview my actionbar should hide, likegetSupportActionBar().hide(); i know already there are answers in the stack overflow for the same question, but i don't know what to do in my case, because i am a beginner in android development
here is my activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation" />
This is my first fragment (fragment_tab1)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#android:color/transparent">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#android:color/transparent"
android:foregroundGravity="top"
android:progressDrawable="#drawable/custom_progress"
android:progress="20"/>
</FrameLayout>
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
This is my java code for tab1
package com.hackerinside.jaisonjoseph.polysocial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
/**
* A simple {#link Fragment} subclass.
*/
public class tab1 extends Fragment {
public ProgressBar bar;
public FrameLayout frameLayout;
public tab1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab1, null);
frameLayout=(FrameLayout) rootView.findViewById(R.id.frame1);
bar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
bar.setMax(100);
final WebView view=(WebView) rootView.findViewById(R.id.webview);
view.loadUrl("http://facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view1,int progress){
frameLayout.setVisibility(View.VISIBLE);
bar.setProgress(progress);
if (progress==100){
frameLayout.setVisibility(View.GONE);
}
super.onProgressChanged(view1,progress);
}
});
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
bar.setProgress(0);
return rootView;
}
}
Here is a sample idea of implementation.
boolean isShowing;
//oncraete code is here
WebView mScrollView = (WebView) findViewById(R.id.scrollView);
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
float mfloat = mScrollView.getScrollY();
if (mfloat >= tToolbar.getHeight() && isShowing) {
toolbarAnimateHide();
} else if (mfloat == 0 && !isShowing) {
toolbarAnimateShow(0);
}
}
});
private void toolbarAnimateShow(final int verticalOffset) {
isShowing = true;
//tToolbar is your view(any view)
// add getSupportActionBar().hide() this code here to hide actionbar
// remove below code or comment
tToolbar.animate()
.translationY(0)
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
toolbarSetElevation(verticalOffset == 0 ? 0 : TOOLBAR_ELEVATION);
}
});
}
private void toolbarAnimateHide() {
isShowing = false;
// add getSupportActionBar().show() this code here to showactionbar
// remove below code or comment
//tToolbar is your view(any view)
tToolbar.animate()
.translationY(-tToolbar.getHeight())
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
toolbarSetElevation(0);
}
});
}

How to create a stack view in this swipe card?

I have used the following code to create a swipe view. This is my java code
package com.lorentzos.swipecards;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import java.util.ArrayList;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
public class MyActivity extends Activity {
private ArrayList<String> al;
private ArrayAdapter<String> arrayAdapter;
FrameLayout.LayoutParams params;
View v;
#InjectView(R.id.frame) SwipeFlingAdapterView flingContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
/*frameLayout=(FrameLayout)v.findViewById(R.id.frame_layout);*/
v=findViewById(R.id.frame_layout);
ButterKnife.inject(this);
/*al.add("c");
al.add("python");
al.add("java");
al.add("html");
al.add("c++");
al.add("css");
al.add("javascript");*/
int height=500;
int width=500;
params = new FrameLayout.LayoutParams(width, height);
/*params.gravity= Gravity.CENTER;*/
al = new ArrayList<>();
for(int i=0; i<=7; i++) {
al.add("php");
arrayAdapter = new ArrayAdapter<>(this, R.layout.item, R.id.helloText, al);
flingContainer.setMaxVisible(4);
/*v.setLayoutParams(params);*/
flingContainer.setAdapter(arrayAdapter);
height=height-20;
}
flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
#Override
public void removeFirstObjectInAdapter() {
// this is the simplest way to delete an object from the Adapter (/AdapterView)
Log.d("LIST", "removed object!");
al.remove(0);
arrayAdapter.notifyDataSetChanged();
}
#Override
public void onLeftCardExit(Object dataObject) {
//Do something on the left!
//You also have access to the original object.
//If you want to use it just cast it (String) dataObject
makeToast(MyActivity.this, "Left!");
}
#Override
public void onRightCardExit(Object dataObject) {
makeToast(MyActivity.this, "Right!");
}
#Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
// Ask for more data here
/*al.add("XML ".concat(String.valueOf(i)));
arrayAdapter.notifyDataSetChanged();
Log.d("LIST", "notified");
i++;*/
}
#Override
public void onScroll(float scrollProgressPercent) {
View view = flingContainer.getSelectedView();
view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
}
});
// Optionally add an OnItemClickListener
flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
#Override
public void onItemClicked(int itemPosition, Object dataObject) {
makeToast(MyActivity.this, "Clicked!");
}
});
}
static void makeToast(Context ctx, String s){
Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
}
#OnClick(R.id.right)
public void right() {
/**
* Trigger the right event manually.
*/
flingContainer.getTopCardListener().selectRight();
}
#OnClick(R.id.left)
public void left() {
flingContainer.getTopCardListener().selectLeft();
}
}
Below are the xml files
activity_my.xml
<merge
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"
>
<com.lorentzos.flingswipe.SwipeFlingAdapterView
android:id="#+id/frame"
android:background="#ffeee9e2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rotation_degrees="15.5"
app:min_adapter_stack="6"
tools:context=".MyActivity" />
<include layout="#layout/buttons" />
</merge>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/frame_layout"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp">
<TextView
android:id="#+id/helloText"
android:textSize="40sp"
android:textColor="#android:color/white"
android:background="#A5F"
android:gravity="center"
tools:text="#string/hello_world"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="#+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:background="#A5F" />
<View
android:id="#+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:layout_gravity="right"
android:background="#5AF" />
</FrameLayout>
buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/left"
android:layout_margin="10dp"
android:text="Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/right"
android:layout_margin="10dp"
android:text="Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Refer the attached screenshots.
[![enter image description here][2]][2]
But i want the images to come like a stack.. Not exactly overlapped. Refer this
Can anyone help me achieve this?
This is a link with your solution https://github.com/Diolor/Swipecards
Incase you use custom ArrayAdaper on inflating layout this is the code that worked for me
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.swipe_item, parent, false);

Why does this only work inside a host class that extends FragmentActivity?

I have a fragment with the following layout: my_fragment.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/appName"
android:id="#+id/textView" android:layout_gravity="center"
android:layout_margin="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"
android:id="#+id/button1"
android:layout_gravity="center"/>
and corresponding MyFragment.java
package com.example.appdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_bar, container, false);
Button button1 = (Button)view.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(), "Button Pressed", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
and this is hosted inside MainActivity.java
package com.example.appdemo;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
with the following layout 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" >
<fragment
android:id="#+id/fragment"
android:name="com.example.appdemo.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Now, my question is why does this only work when I extend FragmentActivity? I'd like to extend Activity. How do I accomplish that?
Why does this only work inside a host class that extends FragmentActivity?
Because you are inheriting from android.support.v4.app.Fragment instead of android.app.Fragment.

How to enable Horizontal scroll in Android?

Basically, the code below is what I am using to retrieve images from a remote server. My problem is, whenever I have large images like 700x700, it shrinks the entire image until it only fits the screen. What I really want is whenever I have large image like that, I want it to stay just the way it is and show a vertical and horizontal scrollbar instead. is this possible to accomplish?
<?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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/imageView"
android:visibility="gone"
android:layout_width="200dp"
android:layout_height="200dp"/>
</LinearLayout>
Then my main activity ImageDownload.java
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class ImageDownload extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView mainImageView = (ImageView) findViewById(R.id.imageView);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
String imageurl = "http://4.bp.blogspot.com/-xRyw7I08tDM/TgNqD2Ij-3I/AAAAAAAAAOU/FGjboxE-u2U/s1600/cute_cat_cute_1-s357x422-47520.jpg";
ImageDownloadMessageHandler imageDownloadMessageHandler1= new ImageDownloadMessageHandler(progressBar, mainImageView);
ImageDownlaodThread imageDownlaodThread = new ImageDownlaodThread(imageDownloadMessageHandler1,imageurl);
imageDownlaodThread.start();
}
class ImageDownlaodThread extends Thread {
ImageDownloadMessageHandler imageDownloadMessageHandler;
String imageUrl;
public ImageDownlaodThread(ImageDownloadMessageHandler imageDownloadMessageHandler, String imageUrl) {
this.imageDownloadMessageHandler = imageDownloadMessageHandler;
this.imageUrl = imageUrl;
}
#Override
public void run() {
Drawable drawable = LoadImageFromWebOperations(imageUrl);
Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
imageDownloadMessageHandler.sendMessage(message);
System.out.println("Message sent");
}
}
class ImageDownloadMessageHandler extends Handler {
ProgressBar progressBar;
View imageTextView;
public ImageDownloadMessageHandler(ProgressBar progressBar, View imageTextView) {
this.progressBar = progressBar;
this.imageTextView = imageTextView;
}
#Override
public void handleMessage(Message message) {
progressBar.setVisibility(View.GONE);
imageTextView.setBackgroundDrawable(((Drawable) message.obj));
imageTextView.setVisibility(View.VISIBLE);
}
}
Drawable LoadImageFromWebOperations(String url) {
Drawable d = null;
InputStream is = null;
try {
is = (InputStream) new URL(url).getContent();
d = Drawable.createFromStream(is, "src name");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return d;
}
}
Try this:
<ScrollView android:id="#+id/scrollView1"
android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linearLayout2"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal">
<TextView/>
<ImageView/>
<ProgressBar/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
This way you can have both vertical and horizontal scroll.
The layout element ScrollView and HorizontalScrollView has been invented for this purpose.

Categories

Resources