Monday, 14 July 2014

Dynamically set screen orientation android

Set following code before setting activity layout as follow

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(r.layout.yourlayout);


Sunday, 13 July 2014

Remove / Disable activity header android

Just before setting contentview add the following line

requestWindowFeature(Window.FEATURE_NO_TITLE);


Thursday, 10 July 2014

Slide ViewPager on button click

You have changed ViewPager's page by sliding it. You can also change page by click on next and previous button.

  1. Add two button so called NEXT and PREV in xml.
  2. Instantiate these buttons in your activity. And create a global counter as follow

    int globalPosition = 0;

  3. Now set next click listener. as follow

    // checking if pager can scroll or not
    boolean can = mPager.canScrollHorizontally(1);
    
    if(can)
    {
     mPager.setCurrentItem(++globalPosition,true);
    }
    
    

  4. Set previous button click listener

    boolean can = mPager.canScrollHorizontally(-1);
    if(can)
    {
     mPager.setCurrentItem(--globalPosition,true);
    }
    

  5. Dont forget to update globalPosition on ViewPager page sliding.

    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
     @Override
     public void onPageSelected(int position) {
      globalPosition = position;
     }
    });
    


Sunday, 6 July 2014

Android ViewPager

ViewPager is nothing but horizontal scroller for entire screens.

  1. Lets begin from xml. Add ViewPager in your xml file. You can copy following code.
    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/your_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
  2. Now move to java file(Activity) create instance for ViewPager added in xml. Make sure this Activity extending FragmentActivity not Activity
    mPager = (ViewPager) findViewById(R.id.your_pager);
  3. Now create adapter for pager, create pager instance
    ViewPager mPager;
    PagerAdapter mPagerAdapter;
    public static String [] pageItem = new String[]{"Page 1","Page 2","Page 3"};
     
  4. Initialize adapter object and set adapter to ViewPager.
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
  5. Create slider class which should extend FragmentStatePagerAdapter or FragmentPagerAdapter.
    private class ScreenSlidePagerAdapter extends 
        FragmentStatePagerAdapter 
    {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
     super(fm);
    }
    
    @Override
    public Fragment getItem(int position) {
            //
     return new MyFragment(position);
    }
    
    @Override
    public int getCount() {
     return pageItem.length;
    }
    }
  6. Create fragment which will display each screen. It will be slided horizontally.
    public class MyFragment extends Fragment {
     TextView textView;
     int position = 0;
     public MyFragment(int position)
     {
      this.position = position;
     }
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            ViewGroup rootView = (ViewGroup) inflater.inflate(
                    R.layout.my_screen_fragment, container, false);
            
            textView = (TextView)rootView.findViewById(R.id.my_page_tv);
            textView.setText(activityClassObject.pageItem[position]);
            return rootView;
        }
    }
    

Tuesday, 24 June 2014

[KEY : ANDROID CUSTOM CHECKBOX, CUSTOM CHECKBOX]

Custom Checkbox Android

How to create custom checkbox. Applying stylish checkbox Android

  1. Take two images for both states checked and unchecked. In png format.
  2. Create checkbox in xml.
  3. Create new xml file assign appropriate name like "checkbox_button.xml"

    <?xml version="1.0" encoding="utf-8"?>
    <selector 
    xmlns:android="http://schemas.android.com/apk/res/android">
      <!--checked image-->
        <item android:drawable="@drawable/checkbox_checked" 
        android:state_checked="true"/>
      <!--unchecked image-->
        <item android:drawable="@drawable/checkbox" 
        android:state_checked="false"/>
    
    </selector>

  4. Add button property in check box in xml

    android:button="@drawable/checkbox_button"


Monday, 23 June 2014

[KEY : ANDROID FRAGMENT, ACTIVITY, ADD FRAGMENT TO ACTIVITY]

Add Fragment to Activity

  1. Create simple activity with xml.
  2. Go to xml file add framelayout and assign id to it as follow:

     <FrameLayout
            android:id="@+id/your_frame_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true" />

  3. Create java class with name like "MyFirstFragment.java" and extend fragment.
  4. Go to activity add the following code :

    Fragment myFragment = new MyFirstFragment();
     FragmentManager fragmentManager = getSupportFragmentManager();
     FragmentTransaction fTransaction = fragmentManager
       .beginTransaction()
       .replace(R.id.your_frame_layout, bodyFragment)
       .addToBackStack("myprofile");
       
     fTransaction.commit();
       


Saturday, 21 June 2014

[KEY : FRAGMENT, ADDING FRAGMENT, ANDROID FRAGMENT, CREATING FRAGMENT]

FRAGMENT, ANDROID

Android activity is simple and best way to provide interface to user. Fragment are very similar to activity. But it will be much better if we call it small piece of activity. So an activity can have multiple fragments

Creating fragment in android

  1. Create simple class like "MyFirstFragment.java".
  2. Extend Fragment class into "MyFirstFragment.java".
  3. Fragement is almost ready. Now add required methods into it, Specifically you require two methods onCreateView() and onAttach() as mentioned below :

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); }


    @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); }

  4. Inflate your own xml file and return it as view. So finally onCreateView() will look like as below:

    @Override
     public View onCreateView(LayoutInflater inflater, 
       ViewGroup container, Bundle savedInstanceState) {
      
      View localView = inflater.inflate(R.layout.your_xml.xml, 
        container, false);
      
      return localView; //super.onCreateView(inflater, container, savedInstanceState);
     }
    

  5. Create global object of your parent activity

    YourActivity yourActivityObject;

  6. Initialize yourActivityObject in onAttach method so it will look as below:

     @Override
     public void onAttach(Activity activity) {
    
      yourActivityObject = (YourActivity)activity;
    
      super.onAttach(activity);
     }
    

Start / Call new fragment from fragment

 Fragment yourSecondFragment = new YourSecondFragment();

 FragmentManager fm = yourActivityObject.getSupportFragmentManager();

 //yourSecondFragment.setArguments(Bundle) // to pass argument to next fragment.

 fm.beginTransaction().replace(R.id.parent_activity_frame_layout, 
 yourSecondFragment).addToBackStack("myfragmentstring").commit();

 // addToBackStack method before commit is used to implement back button functionality later.  

Adding back button functionality to fragment

In parent activity add onBackPressed() method and paste the following code

int f = getSupportFragmentManager().getBackStackEntryCount();
  Log.v(tag, "onBackPressed ["+f+"]");
  if (f <= 1)
   this.finish();
  else {
   getSupportFragmentManager().popBackStackImmediate();
  }


Tuesday, 14 January 2014

[KEY : SIGNED, UNSIGNED, SINGED AND UNSIGNED APK]

Why signed and unsigned apk.

First of all be clear about signed and unsigned apk. Signed apks keeps its digital signature certificate with it, while unsigned does not. The Android system requires that all installed applications be digitally signed with a certificate whose private key is held by the application's developer. The Android system uses the certificate as a means of identifying the author of an application and establishing trust relationships between applications.

As I guess unsigned apk is for facilitate another user to sign apk with its own key.
Remember the following points.

  • All applications must be signed. The system will not install an application on an emulator or a device if it is not signed.
  • To test and debug your application, the build tools sign your application with a special debug key that is created by the Android SDK build tools.
  • When you are ready to release your application for end-users, you must sign it with a suitable private key. You cannot publish an application that is signed with the debug key generated by the SDK tools.
  • You can use self-signed certificates to sign your applications. No certificate authority is needed.

More detail here

Signing the unsigned apk.
  1. Make sure you have unsigned apk or create it (Open menifest file-> click on Export on Unsigned apk).
  2. Keep your unsigned apk in separate folder.
  3. Find the path of you key.
  4. Open Command Prompt.
  5. jarsigner -verbose -keystore path/of/your/key path/of/your/unsignedApk alias_name_of_key
  6. Key will ask password
  7. Done