How to code google analytics for mobile apps? - android

I'm new to android programming and I assume that I've completely prepared my app to use google-analytics including setting up the necessary plugins, dependencies, configurations , etc... according to this.
but for coding my activities I'm quite confused and i couldn't find straightforward instructions to code my activities in order to use the minimum features of google analytics (like merely knowing the number of page visits and user counts).thank you in advance!

the answer was quite simple.. all i needed was modifying the oncreate method like below:
public class MainActivity extends AppCompatActivity {
private Tracker mTracker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();
mTracker.setScreenName("Home");
mTracker.send(new HitBuilders.AppViewBuilder().build());
and a few adjustments in the manifest.

Related

Enable taking screenshot of a App after disabled using FLAG_SECURE

Using the code below I prevent users taking screenshots of my app. I want to implement "enable screenshot button". Is it possible? Which concept should I use?
public class FlagSecureTestActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(LayoutParams.FLAG_SECURE,
LayoutParams.FLAG_SECURE);
setContentView(R.layout.main);
}
}
You could restart the activity and this time not set the SECURE flag.
In my app, I have a switch in settings to enable or disable the secure flag. Then I simply do not set it if the settings specifies that I am allowed to take screenshots.

Can't set Logo in ActionBar

I've seriously tried everything.
I've tried both setLogo() and setIcon().
I've tried adding android:logo="" in manifest.
I've made sure to try both supportActionBar and regular ActionBar. (I'm running sdk 21 with a min sdk of 15.)
The funny thing is if I try to use the regular ActionBar I get null pointers but when I use the support ActionBar it at least works.
Is there anything else I can try...? Here's where I try and change it.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar ab = getSupportActionBar();
ab.setLogo(R.drawable.logo);
If your min sdk is 15, i'm not sure why you're using the support package at all.
You class should instead extend Activity, and use getActionBar().

Error while using setDisplayHomeAsUpEnabled in FadingActionBarHelper

I want to implement FadingActionBar in my application to get the effects like latest Google play Music app.
Below is how i am using the FadingActionBar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FadingActionBarHelper helper = new FadingActionBarHelper()
.actionBarBackground(R.drawable.ab_background)
.headerLayout(R.layout.header)
.contentLayout(R.layout.activity_listview);
setContentView(helper.createView(this));
helper.initActionBar(this);
Here i wanted to add back button in actionBar using setDisplayHomeAsUpEnabled(true);
I am getting error while adding the above code saying no such method found.
My question is there any way to add setDisplayHomeAsUpEnabled(true);
if not, how we can add back button for FadingActionBar
i had the same problem but it was because i was using appcompat v7 so you just have to add this.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Or
getActionBar().setDisplayHomeAsUpEnabled(true);
Hope it helps

Android Facebook Integration without Fragments

I am creating a social app and would like to have integration with Facebook so I can grab information on Friends, User Info, to Post on Users Wall and Upload a Picture to Facebook. I have followed the tutorials on the Facebook website however they are all using Fragments. I would prefer to not use fragments if at all possible. I'm sure that Facebook wouldn't tie their API around the use of Fragments as that could shut out some people.
I understand the clear answer is to use the newest API with fragments however I would rather not do that for the following reasons:
I don't see why you wouldn't be able to do it without Fragments.
I don't entirely understand the point for Fragments and why they are becoming increasingly more popular.
I have most of the application implemented without using Fragments and would like to implement Facebook login/use at this point in development.
That being said, how can I go about doing this without the use of Fragments?
Why are Fragments becoming increasingly popular?
If the answer is to use the Facebook API with Fragments, is there an "easy" way to alter the app I've already created, that uses Activities, so that it uses Fragments?
Cheers,
Jake
I'm using Fragments, but I've implemented the Facebook Login button using an activity.
I just declared the button in layout
<com.facebook.widget.LoginButton
android:id="#+id_login/btEntrarFacebook"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:background="#drawable/shape_bg_bt_azul"
android:layout_below="#+id_login/btEntrar"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textSize="16dp"
android:typeface="serif"
android:text="f | Entre com o Facebook"/>
and in the activity:
btEntrarFacebook = (LoginButton) findViewById(R.id_login.btEntrarFacebook);
The SDK examples comes with both Activity and Fragments implementations. The tutorials aren't clear enough, but the examples are way better than own Facebook manuals.
There are many sites which will tell you about the advantages of using fragments over activities. One very important reason is that as the screen sizes vary a lot now with different devices, so using fragmnets we can utilize diffeerent screen sizes very well.
Changing activities to fragmnets is very simple as the life cycle and apis of both these components are alomost same. may be you can change an activity to fragment in 15-20 minutes :)
Give it a shot ..
With fragments the concepts is like - There is a FragmentActivity. It is like normal fragmnets which in turn holds different fragmnets. I will try to explain you wih an example which is there in android developer site. Suppose we are working on tabltes. We can have a layout whihcan have left pane and right pane. Bothe panes are two different fragmnets. left pane will contain menu items and on clickin any item, right pane will show. since tablet screen is large we can show both pane on the screen and it will be easy for the user to use. May be like news application. But this design wont look good on small phone screens. in such cases fragmnets panes will automatically get divided into two screens and you can swipe between them. each pane will occupy full screen now. so without you doing anything, fragment takes care of screen sizes to adjust their layout. also normally in portrait mode, they are shown as tabs but in horizontal mode, they become drop down..
If you want a sample reference on how to switch your activities to fragments. I'm adding my example activity and fragments.
// Now this is the Activity class which I have to convert to fragments
TestActivity.java
public class TestActivity extends Activity implements OnClickListener {
private static EditText edittext;
private static Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
edittext = (EditText) findViewById(R.id.et_price);
button = (Button) findViewById(R.id.bt_bold);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(button)) {
edittext.setText("Arshad's Test App");
}
}
}
Now for converting the TestActivity to a Fragment we need to make it in two classes. TestActivity.java and TestFragment.java
// Now the TestActivity will extend the FragmentActivity instead of Activity
public class TestActivity extends FragmentActivity {
private TestFragment testFragment_object;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
testFragment_object = new TestFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, testFragment_object).commit();
} else {
// Or set the fragment from restored state info
testFragment_object = (TestFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
}
}
Now the Fragment class will contain all the code of the layouts
and actions to be performed in class which was contained in the Activity class earlier.
TestFragment.java
public class TestFragment extends Fragment implements OnClickListener {
private static EditText edittext;
private static Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_first, container, false);
edittext = (EditText) view.findViewById(R.id.et_price);
button = (Button) view.findViewById(R.id.bt_bold);
button.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(button)) {
edittext.setText("Arshad's Test App");
}
}
}

How to exclude the my app screenshots showing recent apps

As per security concern screenshots of my app screen should not be shown in recent apps.
I have tried adding android:excludeFromRecents="true" in all activities on the manifest file. However, screenshots are shown in recent apps, can some help me to solve this issue?
excludeFromRecents excludes from recents which is not what you need. Try setting FLAG_SECURE:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(LayoutParams.FLAG_SECURE,
LayoutParams.FLAG_SECURE);
setContentView(R.layout.main_layout;
}
}

Categories

Resources