Showing posts with label Android App. Show all posts
Showing posts with label Android App. Show all posts

Sunday, August 20, 2017

Android build apk file with command line




Google Play

build apk
- gradlew assembleDebug
build apk when running device
    for debug
    - gradlew installDebug
    for release
    - gradlew installRelease


buildTypes {
        release {
            minifyEnabled false
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "boolean", "ENABLE_DEBUG", "false"
        }
        debug {
            buildConfigField "boolean", "ENABLE_DEBUG", "true"
        }
    }

Tuesday, June 27, 2017

[Android] Retrofit library




compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

public class ApiClient {
    public static final String BASE_URL = "your URL";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

public interface ApiInterface {
    @GET("Retrofit.php")
    Call<List<Message>> getInbox();
}

public class Message {
    public int id;
    public String msg;
    public String url;

    public Message(int id, String msg, String url) {
        this.id = id;
        this.msg = msg;
        this.url = url;
    }
}

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        Call<List<Message>> call = apiService.getInbox();
        call.enqueue(new Callback<List<Message>>() {
            @Override
            public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {
                // TODO - avoid looping
                List<Message> messageList = response.body();
                for (Message message : messageList) {
                    Log.d(TAG, new Gson().toJson(message));
                }
            }
            @Override
            public void onFailure(Call<List<Message>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }
}
    }
}

Friday, June 9, 2017

[Android application] Object with Kotlin android




class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) 
 var user = User("Kotlin", "Kotlin@yahoo.com") 
 var msg: String = "name:" + user.name + "\n" + user.email 
 var tv = findViewById(R.id.tv) as TextView 
 tv.text = msg

    }

    inner class User(var name: String, var email: String)
}

[Android application] Compare code between Kotlin and Java





class MainActivity : AppCompatActivity() {
    // kotlin code 
 var a: Int = 10 
 var b: Int = 5 
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) 
 var btn_hello = findViewById(R.id.btn_hello) as Button 
 var c = a + b 
 var msg: String = "Kotlin" 
 btn_hello.setOnClickListener { 
 view -> 
 Toast.makeText(applicationContext, msg, Toast.LENGTH_SHORT).show() 
        } 
    }
}

[Android] Install Kotlin language for Android Studio


class MainActivity : AppCompatActivity() {
 private var TAG: String = "MainActivity" 
 private var a: Int = 0 
 private var b: Int = 0
 
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState) 
 a = 10 
 b = 25 
 Log.d(TAG, "result:" + (a + b)) 
 // result:35 
  }
}

Tuesday, March 14, 2017

Bottom Sheet




 
build.gragle file 
compile 'com.android.support:appcompat-v7:25.2.0' 
compile 'com.android.support:design:25.2.0'
 
MainActivity
 
public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 View bottomSheet = findViewById(R.id.design_bottom_sheet); 
 final BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); 
 // listen view change 
 behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override 
 public void onStateChanged(@NonNull View bottomSheet, int newState) {
 switch (newState) {
 case BottomSheetBehavior.STATE_DRAGGING:
 Log.d(TAG, "BottomSheetBehavior.STATE_DRAGGING"); 
 break; 
 case BottomSheetBehavior.STATE_SETTLING:
 Log.d(TAG, "BottomSheetBehavior.STATE_SETTLING"); 
 break; 
 case BottomSheetBehavior.STATE_EXPANDED:
 Log.d(TAG, "BottomSheetBehavior.STATE_EXPANDED"); 
 break; 
 case BottomSheetBehavior.STATE_COLLAPSED:
 Log.d(TAG, "BottomSheetBehavior.STATE_COLLAPSED"); 
 break; 
 case BottomSheetBehavior.STATE_HIDDEN:
 Log.d(TAG, "BottomSheetBehavior.STATE_HIDDEN"); 
 break;                }
            }

            @Override 
 public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.d(TAG, "slideOffset: " + slideOffset); 
                     }
        });
        Button button = (Button) findViewById(R.id.button); 
 button.setOnClickListener(new View.OnClickListener() {
            @Override 
 public void onClick(View view) {
                if (behavior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
                 } else {
                    behavior.setState(BottomSheetBehavior.STATE_COLLAPSED); 
                 }
                      }
             }); 
          }
}
 
 
 activity_main
 
<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent">
    <android.support.v4.widget.NestedScrollView 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <RelativeLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin">
            <ImageView 
 android:id="@+id/image" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true" 
 android:src="@mipmap/ic_launcher"/>
            <Button 
 android:id="@+id/button" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@+id/image" 
 android:layout_centerHorizontal="true" 
 android:text="Show Bottom Sheet"/> 
 </RelativeLayout> 
 </android.support.v4.widget.NestedScrollView>
    <RelativeLayout 
 android:id="@+id/design_bottom_sheet" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@color/colorAccent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 app:behavior_hideable="true" 
 app:behavior_peekHeight="50dp" 
 app:elevation="4dp" 
 app:layout_behavior="@string/bottom_sheet_behavior">
        <TextView 
 android:id="@+id/bottomsheet_text" 
 android:layout_width="wrap_content" 
 android:layout_height="match_parent" 
 android:text="Introducing Bottom Sheets" 
 android:textColor="#FFFFFF"/>
    </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

Tuesday, November 29, 2016

FragmentStatePagerAdapter


activity_main 
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:gravity="center_horizontal" 
 android:orientation="vertical" 
 android:padding="4dip">

    <android.support.v4.view.ViewPager 
 android:id="@+id/pager" 
 android:layout_width="match_parent" 
 android:layout_height="0px" 
 android:layout_weight="1">
</android.support.v4.view.ViewPager>

    <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="0" 
 android:gravity="center" 
 android:measureWithLargestChild="true" 
 android:orientation="horizontal">

        <Button 
 android:id="@+id/goto_first" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="first">
</Button>

        <Button 
 android:id="@+id/goto_last" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="last"></Button>
    </LinearLayout>
</LinearLayout>
fragment_image
<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" >

    <TextView 
 android:id="@+id/text" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical|center_horizontal" 
 android:text="hello_world" 
 android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView 
 android:id="@+id/imageView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_below="@+id/text" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="36dp" 
 android:src="@mipmap/ic_launcher" />

</RelativeLayout>
fragment_pager_list
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@android:drawable/gallery_thumb" 
 android:orientation="vertical" >

    <TextView 
 android:id="@+id/text" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical|center_horizontal" 
 android:text="hello_world" 
 android:textAppearance="?android:attr/textAppearanceMedium" />

    <FrameLayout 
 android:layout_width="match_parent" 
 android:layout_height="0dip" 
 android:layout_weight="1" >

        <ListView 
 android:id="@android:id/list" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:drawSelectorOnTop="false" />
    </FrameLayout>

</LinearLayout>
public class MainActivity extends FragmentActivity {
    static final int ITEMS = 5;
    MyAdapter mAdapter;
    ViewPager mPager;

    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        Button button = (Button) findViewById(R.id.first);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(0);
            }
        });
        button = (Button) findViewById(R.id.last);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mPager.setCurrentItem(ITEMS - 1);
            }
        });
    }

    public static class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override 
 public int getCount() {
            return ITEMS;
        }

        @Override 
 public Fragment getItem(int position) {
            switch (position) {
                case 0: // Fragment # 0 - This will show image 
 return ImageFragment.init(position);
                case 1: // Fragment # 1 - This will show image 
 return ImageFragment.init(position);
                default:// Fragment # 2-9 - Will show list 
 return ArrayListFragment.init(position);
            }
        }
    }


}
public class ImageFragment extends Fragment {
    int fragVal;

    static ImageFragment init(int val) {
        ImageFragment truitonFrag = new ImageFragment();
        // Supply val input as an argument. 
 Bundle args = new Bundle();
        args.putInt("val", val);
        truitonFrag.setArguments(args);
        return truitonFrag;
    }

    @Override 
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_image, container,
                false);
        View tv = layoutView.findViewById(R.id.text);
        ((TextView) tv).setText("Truiton Fragment #" + fragVal);
        return layoutView;
    }
}
public class ArrayListFragment extends ListFragment {
    int fragNum;
    String arr[] = {"1", "2", "2", "3", "4", "5"};

    static ArrayListFragment init(int val) {
        ArrayListFragment truitonList = new ArrayListFragment();

        // Supply val input as an argument. 
 Bundle args = new Bundle();
        args.putInt("val", val);
        truitonList.setArguments(args);

        return truitonList;
    }

    /**     * Retrieving this instance's number from its arguments.     */ 
 @Override 
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
    }

    /**     * The Fragment's UI is a simple text view showing its instance number and 
 * an associated list.     */ 
 @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View layoutView = inflater.inflate(R.layout.fragment_pager_list,
                container, false);
        View tv = layoutView.findViewById(R.id.text);
        ((TextView) tv).setText("Truiton Fragment #" + fragNum);
        return layoutView;
    }

    @Override 
 public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, arr));
    }

    @Override 
 public void onListItemClick(ListView l, View v, int position, long id) {
        Log.i("Truiton FragmentList", "Item clicked: " + id);
    }
} 
 

Tuesday, November 15, 2016

Fragment in Android

public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";

    @Override 
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v(TAG, "<<screen>>");


        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        FragmentMain fragmentMain = new FragmentMain();
        transaction.addToBackStack("fragmentMain");
        transaction.replace(R.id.container, fragmentMain);
        transaction.commit();

    }


    public boolean handleBackStack() {
        int count = getFragmentManager().getBackStackEntryCount();
        Log.v(TAG, String.valueOf(count));
        if (count == 1) {
            finish();
        } else {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            getFragmentManager().popBackStackImmediate();
            transaction.commit();
        }
        return true;
    }


    //handle the hardware back press 
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            handleBackStack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
public class FragmentMain extends Fragment {


    public FragmentMain() {
        // Required empty public constructor    }

    @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        final Button mButtonFragmentMain=(Button)view.findViewById(R.id.button__fragment_main);

        Log.v("<< main fragment>>","<<screen>>");

        mButtonFragmentMain.setOnClickListener(new View.OnClickListener() {
            @Override 
 public void onClick(View view) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                FragmentChild fragmentChild = new FragmentChild();
                transaction.addToBackStack("fragmentChild");
                transaction.replace(R.id.container, fragmentChild);
                transaction.commit();

            }
        });


        return view;
    }
}
public class FragmentChild extends Fragment {


    public FragmentChild() {
        // Required empty public constructor    }


    @Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_child, container, false);



        return view;
    }
}
activity_main
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/container" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical">

</FrameLayout>
 
fragment_main
 
<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin">

    <TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerHorizontal="true" 
 android:layout_marginTop="20dp" 
 android:text="Main Fragment Screen" 
 android:textSize="26sp" 
 android:textStyle="bold" />

    <Button 
 android:id="@+id/button__fragment_main" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_centerInParent="true" 
 android:paddingBottom="15dp" 
 android:paddingTop="15dp" 
 android:text="Open Child Fragment" 
 android:textSize="20sp" />
</RelativeLayout>
 
fragment_child
<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin">

    <TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_centerInParent="true" 
 android:text="Child Fragment Screen" 
 android:textSize="26sp" 
 android:textStyle="bold" />
</RelativeLayout>