Update Listener Like Android Gmail - java

I have a listview that i show post on it.
what i want is a listener when user moves his finger down and listview can't scroll.(Like Update Listener On Gmail Android App Inbox that when you move your finger from top to down it updates Inbox).
How Can I Do That?

What you're wanting is a SwipeRefreshLayout, which is in the google v4 support libraries.
In order to use it, you need to wrap your scrollable layout in <android.support.v4.widget.SwipeRefreshLayout> tags. You can then create a listener for when someone swipes to refresh in the code for your activity
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
....
#Override public void onRefresh() {
new Handler().postDelayed(new Runnable() {
#Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
More info can be found on this website, and on this page here on Google's developer pages.

Related

Prevent Ad from triggering multiple times

When I run this code on a real device the ad-banner pops up endless times. How should i modifiy this code to run it just once?
This code is from a game. Every time the player gets hit by something he loses 1 shield.
private void showBanner() {
adView.setVisibility(View.VISIBLE);
adView.loadAd(new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}
if (player.getShieldStrength() == 0){
runOnUiThread(new Runnable() {
#Override
public void run() {
showBanner();
}
});
}
This my logcat:
Aborting last ad request since another ad request is already in progress.
The current request object will still be cached for future refreshes.
...
This runnable is triggered by the run method. showBanner is part of update-method
#Override
public void run() {
while (playing) {
update();
draw();
control(); }}
You are confused.
A banner ad is not like an interstitial. It does not pop-up.
When you request a banner ad you are requesting ads to fill that AdView until you tell it to stop or you hide it. It will show new ads on the refresh cycle you have configured in the Admob dashboard. It will not show a single ad.
If you really only want a banner ad to be shown until the next game, then call adView.setVisible(VIEW.INVISIBLE) or adView.setVisible(VIEW.GONE)
And please don't repost existing questions Ad pops up multiple times

Where should I put my ShowCaseView builder in this case?

I have this in my MainActivity.java:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupHomeScreen();
}
public void setupHomeScreen() {
File latestPic = getMostRecentSnappiePicture();
if(latestPic != null){
//display pic
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
else{
layout.setBackgroundDrawable(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
//hide tutorial
findViewById(R.id.howitworks).setVisibility(View.INVISIBLE);
}
else{
//show tutorial
findViewById(R.id.howitworks).setVisibility(View.VISIBLE);
new ShowcaseView.Builder(this)
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the Home button")
.hideOnTouchOutside()
.build();
}
}
}
As you can see, in onCreate, it calls setupHomeScreen and checks if a file exists. If it doesn't exist, it displays a tutorial "howitworks" layout image as well as building a showcase view.
So this all works fine and well. The only issue comes when trying to leave the activity while the showcaseView is still there, OR sometimes even after you exit the showcase view and try and launch the new activity, this error comes up: ShowcaseView - width and height must be > 0
As you can see in the answers, the solution is to only create the showcase view in the callback after the original view has been created like so:
someView.post(new Runnable() {
#Override
public void run() {
// my ShowcaseView builder here
}
});
The only thing is, I have no idea where to put this, since my showcase view should only show up if the file from getMostRecentSnappiePicture() is null. So how can I put the view creation callback inside of my logic to check that that file is null first?
it looks like you're highlighting the HOME button instead of the 'howitworks' view. Try switching the line
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
with
.setTarget(new ViewTarget(R.id.howitworks,this));
Also, the following video might help. It's 20 minute tutorial on how to use ShowCaseView inside an activity with 3 buttons. He is declaring an onClickListener where he changes programmatically the TargetView highlighted by the showCaseView
https://www.youtube.com/watch?v=3zdeFSBplps
The video is in spanish, but at least you'll be able to follow the steps, since he's writing the code from scratch.

Call a method when fragment is visible to user

I need execute a method when the fragment is visible (to the user).
Example:
I have 2 buttons (button 1 and button 2) ,
2 fragments(fragment 1 and fragment 2)
and the method loadImages() inside the class fragment 2.
when I press "button2" I want to replace fragment 1 by fragment 2
and then after the fragment 2 is visible (to the user) call loadImages().
I tried to use onResume() in the fragment class but it calls the method before the fragment is visible and it makes some delay to the transition.
I tried setUserVisibleHint() too and did not work.
A good example is the Instagram app. when you click on profile it loads the profile activity first and then import all the images.
I hope someone can help me. I will appreciate your help so much. Thank you.
Use the ViewTreeObserver callbacks:
#Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
final View view = v;
// Add a callback to be invoked when the view is drawn
view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
#Override
public void onDraw() {
// Immediately detach the listener so it only is called once
view.getViewTreeObserver().removeOnDrawListener(this);
// You're visible! Do your stuff.
loadImages();
}
});
}
I'm a little confused by what you are trying to do. It sounds like the images are loading too fast for you... so does that mean that you have the images ready to display? And that is a bad thing?
My guess (and this is just a guess) is that Instagram does not have the profile pictures in memory, so they have to make an API call to retrieve them, which is why they show up on a delay. If the same is the case for you, consider starting an AsyncTask in the onResume method of the fragment. Do whatever loading you need to do for the images in the background, and then make the images appear in the onPostExecute callback on the main thread. Make sure you only start the task if the images are not already loaded.
However, if you already have the images loaded in memory, and you just want a delay before they appear to the user, then you can do a postDelayed method on Handler. Something like this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadImages();
}
}, 1000);
Edit
As kcoppock points out, the handler code is pretty bad. I meant it to be a quick example, but it is so wrong I should not have included it in the first place. A more complete answer would be:
private Handler handler;
public void onResume(){
super.onResume();
if(handler == null){
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
loadImages();
}
}, 1000);
}
}
public void onDestroyView(){
super.onDestroyView();
handler.removeCallbacksAndMessages(null);
handler = null;
}
Use the onActivityCreated() callBck

Android Google Analytics EasyTracker

I'm trying to use Google Analytics in my Android application with
Google Configuration
Add .jar in my project
Insert this in AndroidManifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Add this in my java file
public class MainActivity extends Activity {
GoogleAnalyticsTracker tracker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("My-UA–XXXXXXXX", this);
setContentView(R.layout.main);
Button createEventButton = (Button)findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tracker.trackEvent(
"Clicks", // Category
"Button", // Action
"clicked", // Label
77); // Value
}
});
setContentView(R.layout.main);
Button createPageButton = (Button)findViewById(R.id.NewPageButton);
createPageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Add a Custom Variable to this pageview, with name of "Medium" and value "MobileApp" and
// scope of session-level.
tracker.setCustomVar(1, "Navigation Type", "Button click", 2);
// Track a page view. This is probably the best way to track which parts of your application
// are being used.
// E.g.
// tracker.trackPageView("/help"); to track someone looking at the help screen.
// tracker.trackPageView("/level2"); to track someone reaching level 2 in a game.
// tracker.trackPageView("/uploadScreen"); to track someone using an upload screen.
tracker.trackPageView("/testApplicationHomeScreen");
}
});
Button quitButton = (Button)findViewById(R.id.QuitButton);
quitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
Button dispatchButton = (Button)findViewById(R.id.DispatchButton);
dispatchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Manually start a dispatch, not needed if the tracker was started with a dispatch
// interval.
tracker.dispatch();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
// Stop the tracker when it is no longer needed.
tracker.stopSession();
}
}
==> And it's ok, no error, compiling and executing but i have created my ua account yesterday (more 24h) and i have nothing in my google analytics panel.
My Question : is there an error in my code or i want to wait again ? Live trafic works for Android application (like tradicional website) ???
I have no information about Live trafic (when i play my app, i would like to show the number of person using my application) and Saved trafic (with viewed pages, time)
Thank you for your replies and excuse my poor english :)
bye
UPDATE 1 :
i've used this tuto : http://www.xatik.com/2012/03/27/how-to-use-google-analytics-in-android-applications/ and i've got this in my Logcat :
04-07 14:21:59.669: INFO/GoogleAnalyticsTracker(864): Host: www.google-analytics.com
04-07 14:21:59.669: INFO/GoogleAnalyticsTracker(864): User-Agent: GoogleAnalytics/1.4.2 (Linux; U; Android 2.2; en-us; sdk Build/FRF91)
04-07 14:21:59.669: INFO/GoogleAnalyticsTracker(864): GET /__utm.gif?utmwv=4.8.1ma&utmn=235327630&utme=8(1!Navigation%20Type)9(1!Button%20click)11(1!2)&utmcs=UTF-8&utmsr=240x320&utmul=en-US&utmp=%2FtestApplicationHomeScreen&utmac=BLIBLUBLIBLO–1&utmcc=more_and_more
in progress but nothing in my Live Analytics panel....
i've added EasyTracker .jar in my project
Here my Activity Code:
import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import com.google.android.apps.analytics.easytracking.EasyTracker;
import com.google.android.apps.analytics.easytracking.TrackedActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends TrackedActivity {
GoogleAnalyticsTracker tracker;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button quitButton = (Button)findViewById(R.id.QuitButton);
quitButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EasyTracker.getTracker().trackEvent("ButtonClick", "MyButtonName", "", 0);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
//How can i stop the tracking onDestroy ???
}
}
I know this is a couple months old but I'll give a response to potentially help others. I am the person who wrote the guide that was referenced in Update 1 of the OP. One issue I originally ran into was the fact that I was using a custom ROM on my phone. A lot of custom ROMs have modified 'hosts' files that block an apps access to display ads or in this case blocks the sending of data to Google Analytics. If you do have a custom ROM on your phone, you can check the 'hosts' file to see if Google Analytics is listed in there. The fastest way to do this is to open the file in a text editor on your computer. To do this:
Get a file explorer app on you android device (I use 'ES File Explorer').
Navigate to '/etc'.
Locate and copy the 'hosts' file to a known location on your SD card.
Connect phone/SD card to computer and open the 'hosts' file in a text editor (Notepad++ is nice and free).
Search through file for anything that relates to Google Analytics and delete it. I first searched for 'analytics', went through all results, and deleted everything that had something to do with Google attached to the name (there are other analytic sites). Then I searched for 'google', went through all the results, and deleted anything that still related to Analytics.
Save 'hosts' file.
Disconnect from computer and use file explorer to copy the 'hosts' file from SD card back to '/etc' and overwrite.
This should allow your phone to send data to Google Analytics. I will update my guide to include this somewhere.

Android - how to add button click event inside a TabActivity?

I am trying to understand the tab activities in android.. and working around with few examples.I have three tabs Tab1,Tab2,Tab3.. in which when I click each tab it will display a simple textview.this is fine for me.Now, I added a button in Tab1 and I tired to handle the click event... its not working for me.It says "App has stopped".
Below is the Tab1 activity....
EDIT1:I am a starter..In Android sdk it comes with a default API demo project.In that project, for 2.x Andriod OS.. the same method works.They are able to handle this click event in that ta activity.
public class Tab1 extends Activity
{
private Button bt_AddAcc = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bt_addaccount);
bt_AddAcc = (Button)findViewById(R.layout.bt_addaccount);
bt_AddAcc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Uername:",Toast.LENGTH_LONG).show();
}
});
}
}
TabActivity is already deprecated.
I suggest you look at the ActionBar or when you want to develop for Android 2.x as well then take a look at ActionBarSherlock. This is a third party library which implements the ActionBar on older devices.

Categories

Resources