Sunday, November 15, 2015

how to Install apk file from pc to the Android device or emulator

Step to install apk file from pc to android devices or emulator

Windows:

  1. First you must connect you devices or start your emulator (which I will not gonna talk about it.)
  2. Then open command prompt and type adb install [apk path]  
    • (ps:replace the [apk path with the path to the apk file  you want to install into the device / emulator) 
    • == don't tell me adb command not exist.
    • which you not yet put the android-sdk\tools or platform-tools folder into the environmental path. Then you have to go to the tools folder and type the command again.
  3.  Ok installed. end of story. Thx


Linux:

  1. Copy the apk file to platform-tools in android-sdk linux folder.
  2. Open Terminal and navigate to platform-tools folder in android-sdk.
  3. Then Execute this command -
  4. ./adb install FileName.apk
  5. If the operation is successful (the result is displayed on the screen), then you will find your file in the launcher of your emulator.

Mac:

  1. PATH=$PATH:~/Library/Android/sdk/platform-tools
  2. Example : PATH=$PATH:/users/jorgesys/eclipse/android-sdk-mac_64/tools
  3. Then run adb.


If any problems can leave a comment belows. I will Try to solve it ...but not mac user. Thank you. =D


Use variable inside gradie on android code

How to differentiate debug mode and release mode in android studio?

Hi, Recently I was find a solution to let the Android Studio able to determine whether the application is debug mode or release mode. So that I can easily switch the code inside the android so that the URL and link would be modified according to the debug/release mode.

In the end , I found the solution and the method to add the variable/constant inside the gradle file so that the android code able to call it .
First of all, there are 1 variable inside the gradle call BuildConfig.DEBUG  which is use to as the variable or toggle flag between the release mode and the debug mode.

If is the variable return true mean debug mode while false is release mode.

=| ok sorry for talk nothing on the variable that can be use on the android code which setting in gradle , we still haven't talk much about the variable which I will talk about it below.


Variable in gradle that can use on android java code


To use the variable or constant inside the gradle , first you have declare the value on the gradle file.
You could use the static member like BuildConfig to get the variable you want inside the gradle file.

Below is the example of put the value of you want inside the gradle file.

 android {
    buildTypes {
        debug {
            buildConfigField "String", "BASE_URL", "http://debug.server.com"
        }

        release {
            buildConfigField "String", "BASE_URL", "http://release.server.com"
        }
    }
}


if is want to using the string variable for debug, could call using BuildConfig.debug , and vice versa if want to call release.

Nothing much to say now, if got any others question or better method , you could just give the comment thank you for reading. Have a nice days.

Saturday, October 17, 2015

ViewPager with GridView pages example

ViewPager with GridView Pages

I faced problems where the action bar tab layout  are require to implement the viewpager which is the swipe function. It was actually a contact list which have 3 gridview each for each tab.

From my friend suggest solution is create 3 view pages, but only 1 view pages  which is the page 2 that hold the Gridview. Each time the tab is swipe , although it was seem like go to another pages, but it actually is revert back to the page 2 all the time . BUT I NOT USING THAT SOLUTION. Because it could not be use due solution is tab and his case are not using the tab layout.


Below is the step I was using on the implementation:
  1. Instantiate the view's we want as pages, then we pass them in a Vector to the custom ViewPager adapter we made. The in the adapter we instantiate the correct View in the position.
  2. Don't forget to override the isViewFromObject and destroy item methods.


MainActivity.java

public class MainActivity extends Activity {
 
    /** Called when the activity is first created. */
 
 private Context mContext;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.main);
        GridView gridview1 = new GridView (mContext);
        GridView gridview2 = new GridView (mContext);
        GridView gridview3 = new GridView (mContext);
        
        Vector<View> pages = new Vector<View>();
        
        pages.add(gridview1 );
        pages.add(gridview2 );
        pages.add(gridview3 );
        
        ViewPager vp = (ViewPager) findViewById(R.id.viewpager);
        CustomPagerAdapter adapter = new CustomPagerAdapter(mContext,pages);
        vp.setAdapter(adapter);
        
        gridview3 .setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));
        gridview3 .setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A2","B2","C2","D2"}));
        gridview3 .setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1,new String[]{"A3","B3","C3","D3"}));
        
    }
}



CustomPagerAdapter.java

public class CustomPagerAdapter extends PagerAdapter {

 private Context mContext;
 private Vector<View> pages;

 public CustomPagerAdapter(Context context, Vector<View> pages) {
  this.mContext=context;
  this.pages=pages;
 }
 
 @Override
 public Object instantiateItem(ViewGroup container, int position) {
  View page = pages.get(position);
  container.addView(page);
  return page;
 }
 
 @Override
 public int getCount() {
  return pages.size();
 }

 @Override
 public boolean isViewFromObject(View view, Object object) {
  return view.equals(object);
 }
 
 @Override
 public void destroyItem(ViewGroup container, int position, Object object) {
  container.removeView((View) object);
 }

}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
    />

</LinearLayout>