Tuesday, April 8, 2014

How to handle textview link click to open new activity?

Step 1: Here is how the Textview looks like.
Activity 1:
TextView tv = (TextView) findViewById(R.id.buy_tc_label);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(Html.fromHtml(getString(R.string.terms_and_condition_start) + " <a href='com.package.name://tc/terms'>" + getString(R.string.terms_and_conditions) + "</a>"));

Strings.xml
    <string name="terms_and_condition_start">I accept the</string>
    <string name="terms_and_conditions">Terms &amp; Conditions</string>

Step 2: manifest.xml
<activity
            android:name=".TCsActivity"
            android:theme="@style/Theme" >
            <intent-filter>
                <data
                    android:scheme="com.package.name" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
</activity>

This means that links starting with com.package.name:// will be handled by TCsActivity.

So all I have to do is construct a URL that contains the information I want to convey:

com.package.name://action-to-be-needed/abc
In my TCsActivity, I can retrieve this.

TCsActivity:

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.terms_conds);
WebView webView = (WebView) findViewById(R.id.terms_and_conditions);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD)
{

MyWebViewClient webViewClient = new MyWebViewClient(this);

webView.setWebViewClient(webViewClient);
}

webView.setWebChromeClient(new WebChromeClient());
String tc = "blah blah blah terms and condition content goes here";
webView.loadDataWithBaseURL (null, tc, "text/html", "utf-8", "about:blank");
}

output Xml layout: terms_conds.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/terms_and_conditions"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />




Wednesday, March 26, 2014

Steps for decoding .apk files

Step 1:

- Copy .apk file in your folder.
- Rename the extension of this .apk file to .zip (eg.: rename from filename.apk to filename.zip) and save it.
- There are assets, meta-inf, res, classes.dex files, etc.
At this stage you are able to see drawable but not xml and java files.

Step 2:

- Extract the zip file.
- Download dex2jar from this link http://code.google.com/p/dex2jar/ and extract it to the same folder.
- Move the classes.dex file into the dex2jar folder.
- Now open command prompt and change directory to that folder.
- Then write dex2jar classes.dex and press enter. Now you get classes_dex2jar.jar file in the same folder.
- Then download java decompiler from http://jd.benow.ca/ and now double click on jd-gui and click on open file.
- Then open classes_dex2jar.jar file from that folder.
- Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name.
At this stage you get java source but the xml files are still unreadable, so continue.

Step 3:

- Open another folder and put .apk file which you want to decode

- download apktool v1.x  and apktool install window (both can be downloaded at the same location) and put in the same folder

- download framework-res.apk file and put in the same folder (Not all apk file need framework-res.apk file)

- Open a command window

- Navigate to the root directory of APKtool and type the following command:  apktool if framework-res.apk

- apktool d "fname".apk ("fname" denotes filename which you want to decode)

now you get a file folder in that folder and now you can easily read xml files also.

Step 4:

It's not any step just copy contents of both folder(in this case both new folder)to the single one

Src: http://stackoverflow.com/

Tuesday, December 3, 2013

Formatting String in Android

Formatting strings

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Wednesday, November 6, 2013

how to display MapFragment inside the fragment(NestedFragment):

At this point I believe you have
1. added necessary permission on manifest
2. added google play service as a lib project
3. api key in manifest file.

where.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.03"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/mapwhere" />


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>




public class WhereFragment extends SupportMapFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View root = inflater.inflate(R.layout.where, null, false);
initilizeMap();
return root;
}

private void initilizeMap()
{
mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapwhere);
if (mSupportMapFragment == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
mSupportMapFragment = SupportMapFragment.newInstance();
fragmentTransaction.replace(R.id.mapwhere, mSupportMapFragment).commit();
   }
if (mSupportMapFragment != null)
{
googleMap = mSupportMapFragment.getMap();
if (googleMap != null)
googleMap.setOnMapClickListener(new OnMapClickListener()
{
@Override
public void onMapClick(LatLng point)
{
//TODO: your onclick stuffs
}
});
}
}
}


Documentation:
Nested Fragments
You can now embed fragments inside fragments. This is useful for a variety of situations in which you want to place dynamic and re-usable UI components into a UI component that is itself dynamic and re-usable. For example, if you use ViewPager to create fragments that swipe left and right and consume a majority of the screen space, you can now insert fragments into each fragment page.

To nest a fragment, simply call getChildFragmentManager() on the Fragment in which you want to add a fragment. This returns a FragmentManager that you can use like you normally do from the top-level activity to create fragment transactions. For example, here’s some code that adds a fragment from within an existing Fragment class:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
From within a nested fragment, you can get a reference to the parent fragment by calling getParentFragment().

The Android Support Library also now supports nested fragments, so you can implement nested fragment designs on Android 1.6 and higher.

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.
source:http://developer.android.com/about/versions/android-4.2.html#NestedFragments


This will also fix for:

11-06 11:36:01.509: E/AndroidRuntime(6309): FATAL EXCEPTION: main
11-06 11:36:01.509: E/AndroidRuntime(6309): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.abc.android.ui.WhereFragment.onCreateView(WhereFragment.java:60)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4923)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2414)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.View.measure(View.java:16060)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2133)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1286)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1497)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1183)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4863)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.os.Handler.handleCallback(Handler.java:725)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.os.Looper.loop(Looper.java:137)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.app.ActivityThread.main(ActivityThread.java:5328)
11-06 11:36:01.509: E/AndroidRuntime(6309): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 11:36:01.509: E/AndroidRuntime(6309): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-06 11:36:01.509: E/AndroidRuntime(6309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-06 11:36:01.509: E/AndroidRuntime(6309): at dalvik.system.NativeStart.main(Native Method)
11-06 11:36:01.509: E/AndroidRuntime(6309): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.SupportMapFragment did not create a view.
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
11-06 11:36:01.509: E/AndroidRuntime(6309): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)

Thursday, October 31, 2013

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Name: java.lang.IllegalStateException
Reason: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
Stack Trace:
0 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
1 at android.database.CursorWindow.nativeGetString(Native Method)
2 at android.database.CursorWindow.getString(CursorWindow.java:438)
3 at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
4 at android.database.CursorWrapper.getString(CursorWrapper.java:114)
5 at com.travelzoo.android.ui.MapDealsActivity$1.onLoadFinished(MapXXXXActivity.java:****)
6 at com.travelzoo.android.ui.MapDealsActivity$1.onLoadFinished(MapXXXXActivity.java:1)
7 at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
8 at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
9 at android.support.v4.content.Loader.deliverResult(Loader.java:104)
10 at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
11 at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
12 at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
13 at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
14 at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
15 at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
16 at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
17 at android.os.Handler.dispatchMessage(Handler.java:99)
18 at android.os.Looper.loop(Looper.java:137)
19 at android.app.ActivityThread.main(ActivityThread.java:5328)
20 at java.lang.reflect.Method.invokeNative(Native Method)
21 at java.lang.reflect.Method.invoke(Method.java:511)
22 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
23 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
24 at dalvik.system.NativeStart.main(Native Method)



Fix

0. Try to position cursor by moveToFirst before reading data from it.

1. Close the cursor after if ( c.moveToFirst()) {}

2. check for null.e,g; if (c != null && c.moveToFirst()) {}

3. check for count.e,g; (c != null && c.getCount() >0 && c.moveToFirst()){}

Tuesday, October 15, 2013

Google Map V2 Android Tips and Ticks for beginners

1) How to set zoom controls?
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
           
     public void onCameraChange(CameraPosition arg0) {
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(8));
        googleMap.setOnCameraChangeListener(MySampleMapActivity.this);
      }
});
2) How to add marker at certain point?
mapView.addMarker(new MarkerOptions().position(new LatLng(lat, lng))
.title(dealTitle)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_menu_directions))
.snippet(dealPrice)
);
3) How to display my location icon?
googleMap.setMyLocationEnabled(true);
4) How to set map type?
a. googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
b. googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
 
c. googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
d. googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
5) How to animate to certain position?
final LatLng pos = new LatLng(LAT, LNG);
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
        public void onCameraChange(CameraPosition arg0) {
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 11));
        }
    });
6) How to add multiple points?

final LatLngBounds.Builder builder = new LatLngBounds.Builder();
//Number of latitude and longitude in for loop
for(;;){
final LatLng pos = new LatLng(lat,lng);
 builder.include(pos);
googleMap.addMarker(new MarkerOptions()
                        .position(pos)
                        .title("TITLE")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}

Then,
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
               
                public void onCameraChange(CameraPosition arg0) {
                    googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 20);
                    googleMap.setOnCameraChangeListener(null);
                }
            });
7) HOw to add info window?
static final LatLng MY_LOCATION = new LatLng(LAT, LNG);
Marker pune = mMap.addMarker(new MarkerOptions()
                          .position(Pune)
                          .title("Pune")
                          .snippet("YOUR TEXT HERE")
                         .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

Source:  http://mobisys.in/blog/2013/04/google-maps-android-api-v2-different-operations/