So I implemented a Pull-to-Refresh on my GridView using android.support.v4.widget.SwipeRefreshLayout and it works well. But when dragging down the finger, only the animation plays and the 'GridView-being-pulled-down-animation' doesn't happen like it should when refreshing. The GridView stays intact while it gets updated. How can I override this animation method and add a GridView movement which clearly shows the animation where the GridView gets pulled down with the finger's movement? (similar to facebook, instagram's refresh)
And also maybe display a message at the top saying 'Refreshing'
My Code so far.
XML:
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECECEC"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
</RelativeLayout>
Code:
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(new OnRefreshListener() {
#Override
public void onRefresh() {
//my update process
RequestParams params = new RequestParams();
params.put("req", "dataReqAndroid");
invokeWS(params);
}
});
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
Related
I'm working on a music player application, and I've created a FrameLayout which works as a "card," displaying information about each song (album art, title, artist, etc.) I'm trying to add checkboxes in this card, which I plan to customize to create upvote and downvote buttons. However, the checkboxes within the card do not respond to clicks. If I draw a checkbox in the main activity of the application (where I'm drawing the cards), it works fine.
Here is the onDraw() method of the card view ("MusicCardView"):
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw the voting controls
try {
CheckBox upvoteButton = new CheckBox(getContext());
upvoteButton.setX(20);
upvoteButton.setTop(-60);
upvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "upvote clicked");
}
});
CheckBox downvoteButton = new CheckBox(getContext());
downvoteButton.setX(20);
downvoteButton.setTop(-150);
downvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "downvote clicked");
}
});
upvoteButton.draw(canvas);
downvoteButton.draw(canvas);
} catch (Exception e) {
Log.e("MusicCardView", "exception", e );
}
}
The xml of the main activity ("PartyActivity"):
<?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/main_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.musique.PartyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/main_linear_view"
android:layout_marginBottom="56dp">
<com.musique.MusicCardView
android:layout_width="match_parent"
android:id="#+id/now_playing_view"
android:layout_height="80dp"
android:background="#ccc"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
app:exampleColor="#000000"
app:songTitle="Money"
app:artist="Pink Floyd"
app:album="The Dark Side of the Moon"
app:exampleDimension="18sp"
app:exampleDrawable="#drawable/darkside"
app:exampleString="example" />
<Space
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="#+id/controller_space"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/query"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchBtn"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search"
android:background="#5eca99"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvData"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:id="#+id/main_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scroll_child">
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
And an example of a MusicCardView being the draw in PartyActivity
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);
I've had a look at this question and this question, but had no luck with the solutions posted, and would greatly appreciate any advice you might have.
If you draw the controls directly to the canvas they are basically just bitmaps, you need to add them to the view hierarchy (activity/fragment layout) for them to receive touch events.
As a side note, it's a big no-no to instantiate things in onDraw, especially views.
I'm having an issue with the SwipeRefreshLayout. I have a layout within two GridView's and every time I scroll upward in the GridView the swipe to refresh layout is tiggered and you can never scroll back to the top of the layout. Anyone have any ideas?
I checkedthis and this but that isn't for a GridView. Please help if you can
SwipeRefreshLayout
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:baselineAligned="false"
android:orientation="vertical">
<GridView
android:id="#+id/GridView1"
android:layout_width="wrap_content"
android:layout_height="31dp"
android:numColumns="1"
android:padding="1dp" />
<GridView
android:id="#+id/GridView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:footerDividersEnabled="false"
android:numColumns="3"
android:padding="1dp" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Try adding android:fillViewport="true" to your SwipeRefreshLayout.
Give your Linear layout id as android:id="#+id/parentPanelLL" and write below code in your activity...
swipe_container.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (parentPanelLL.getScrollY() == 0) {
swipe_container.setEnabled(true);
} else {
swipe_container.setEnabled(false);
}
}
});
try this...
I am trying to implement a tabview with the nice gradual change from one view to another animation and have the current tab highlighted. At current all I can get is the basic tabview that functions, but does instant switching of tabs and no highlighting of selections. Here is my current code:
package com.example.ex.test;
public class MainActivity extends navBar {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"),
smithingTable.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"),
smithingTable2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"),
Map.class, null);
}
}
meanwhile in my activity main I have:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
and in tab1 for example (tab2 is the same but with slight variation)
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".DeviceFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:layout_gravity="center_horizontal"
android:src="#drawable/newcomer_map" />
I would really recommend you use Material Design Tabs, it alredy has nice transitions and its much more customizable.
Here's a very good post on how to implement it
Found out that I needed to add pageviews/listeners
I have a TRANSPARENT overlay in my android app that when user click on it,it fade but it can't fill all activity like below image
MainActivity :
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is Original View" />
</RelativeLayout>
OverlayActivity :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/over_lay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="This is Overlay View" />
</RelativeLayout>
Java :
public class MainActivity extends Activity {
private ImageView mOverLayImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog overlayInfo = new Dialog(MainActivity.this);
overlayInfo.requestWindowFeature(Window.FEATURE_NO_TITLE);
overlayInfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
overlayInfo.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
overlayInfo.setContentView(R.layout.overlay_view);
overlayInfo.show();
mOverLayImage = (ImageView) overlayInfo.findViewById(R.id.over_lay_image);
mOverLayImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
overlayInfo.cancel();
}
});
}
}
Use FrameLayout. Each item added to FrameLayout is on top of the previous one, like in this example the second TextView is on top of the frist one, but since it is not fully opaque, you can see them both!
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:text="Blablabla"
android:background="#FFFFFFFF"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000"
android:textColor="#FFFFFF"
android:text="I am on top"
android:gravity="center"
/>
</FrameLayout>
Now all you need to do is show/hide the overlayed items and you are good to go.
Delete your overlay activity, and inside your main activity apply this code :
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="This is Original View" />
<!-- This is your overlay -->
<RelativeLayout android:id="#+id/over_lay_page"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/over_lay_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#50220000"
android:onClick="clickedOverlay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="This is Overlay View" />
</RelativeLayout>
</RelativeLayout>
Note that I added a line on your ImageView which runs a function when clicked, now on your java file add this function:
//The onClick on xml requires a function of signature void(View) which is the clicked view (in this case the ImageView)
public void clickedOverlay(View view)
{
//ImageView is clicked
RelativeLayout rlLayout = (RelativeLayout) findViewById(R.id.over_lay_page);
rlLayout.setVisibility(View.GONE);
}
This will make the RelativeLayout that contains the overlay views (including the ImageView which is clicked) to not only be invisible but not to interfere with anything. It also ignores input to it.
In case I misunderstood anything about your question feel free to correct me (I'm not sure I understood that completely).
Also if you want it to fade in or out or something like that you can do it with an AlphaAnimation.
I have created an Android app with the new ads sdk, but it is invisible. I got it working in a previous app, it works good there. Now, in my new app, I have done the same. But:
The AdView is not visible, but clickable. If you go back to your home screen (just minimize, dont stop!), and re-open it, the ad will be shown.
My XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<org.andengine.opengl.view.RenderSurfaceView
android:id="#+id/SurfaceViewId"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:gravity="center" />
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="XXX"
ads:adSize="BANNER" />
</RelativeLayout>
And my onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adView = (AdView) findViewById(R.id.adViewId);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
EDIT: After 60 sec., the view becomes visible.
Your problem is that you have told you RenderSurfaceView to matchParent forits height. THis means that it will consume all of the space of its parent leaving none for the AdView.
Try this instead:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.android.gms.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<org.andengine.opengl.view.RenderSurfaceView
android:id="#+id/SurfaceViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" />
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="XXX"
ads:adSize="BANNER" />
</LinearLayout>
This uses layout_weight with LinearLayout to have your RenderSurfaceView expand to fill all unused vertical space.
Maybe you forgot implementing these important methods
public void onPause() {
super.onPause();
this.adView.pause();
}
public void onResume() {
super.onResume();
this.adView.resume();
}