Monthly Archives: October 2012

Most Used GIT Codes


Create a new repo in git
Steps:
mkdir gitfile
cd gitfile
git init
git hello.txt
git add .
git commit -m 'creating a new repo'
git remote add origin git@git.braindigit.com:editprofile.git

Branching and Merging From The Developers Local Branch To The Master Branch
Branching and Merging From The Developers Local Branch To The Master Branch
Update the repo
Steps:
cd gitfile
git init
git clone git@git.braindigit.com:editprofile.git
cd editprofile
git add .
git commit -m 'update the git file'
git push origin master

Put projects into the branch of the master
Steps:
cd gitfile
git clone git@git.braindigit.com:parentproject.git
cd parentproject
git branch branchname
git checkout branchname
git add .
git commit -m 'putting project in the branch of the origin'
git status
git push origin branchname

Pull project from a given branch of the master
Steps
cd gitfile
git clone git@git.braindigit.com:parentproject.git
cd parentproject
git branch branchname
git checkout branchname
git pull origin branchname

Remove a file from the git
Code:
git rm filepath/filename

Export a project from the git
git checkout-index -a -f --prefix=../foldername_for_checkout/
Pull from git
git pull origin branchname

Git Pull:
Git pull will pull down from a remote whatever we ask and instantly merge it into the branch where we are in when we make the request.Pull is a high-level request that runs ‘fetch’ then a ‘merge’ by default, or a rebase with ‘–rebase’.
Steps:
git checkout localBranch
git pull origin master
git branch * master

If we have any remote branches that we want to fetch at once:
git remote update
git pull -all

To pull from a specific branch:
When we do git clone git://location, all branches and tags are fetched.
In order to work on top of a specific remote branch,
assuming its the origin remote:
git checkout -t branch origin/branchname

Undo a pull:
git reset HEAD@{1}
Note:ORIG_HEAD is previous state of HEAD, set by commands that have possibly dangerous behavior, to be easy to revert them. It is less useful now that Git has reflog: HEAD@{1} is roughly equivalent to ORIG_HEAD (HEAD@{1} is always last value of HEAD, ORIG_HEAD is last value of HEAD before dangerous operation)

Git Fetch:
Fetch is similar to pull, except it won’t do any merging.
git checkout localBranch
git fetch origin remoteBranch
git branch master
* localBranch remoteBranch

The fetch will have pulled down the remoteBranch and put it into a local branch called “remoteBranch”. It creates a local copy of a remote branch which we shouldn’t manipulate directly; instead create a proper local branch and work on that.

‘git checkout’ has a confusing feature though. If we ‘checkout’ a local copy of a remote branch, it creates a local copy and sets up a merge to it by default.

Git Clone:
Git clone will clone a repo int a newly created directory. It’s useful for when you’re setting up your local doodah
cd newfolder
git clone git@github.com:whatever/something.git
git branch

* master remoteBranch
Git clone additionally creates a remote called ‘origin’ for the repo cloned from, sets up a local branch based on the remote’s active branch (generally master), and creates remote-tracking branches for all the branches in the repo

Git Reset
Git Reset
Git Rebase
Git Rebasing
Git Merge
Git Merging

References
http://blog.mikepearce.net/2010/05/18/the-difference-between-git-pull-git-fetch-and-git-clone-and-git-rebase/
http://stackoverflow.com/questions/9537392/git-fetch-remote-branch
http://www.kernel.org/pub/software/scm/git/docs/git-pull.html

Advertisements

Custom Views with Custom Attributes in Android


This is a short tutorial on creating a custom linear layout and using custom attributes in the linear layout.
The scenario here is:
We have a linear layout which can be reused in different places. If we go through the traditional way of using this linear layout, in various places, then we will have to include that linear layout wherever it is required. But, we can also use this linear layout as a view. The advantage of using the linear layout as a view is, we can use it in whatever layout we like. Its just like using a button. This linear layout will also contain child views. The child view consists of linear layouts, text views and buttons. With respect to our project, some child views need to be displayed in a certain place where other child views need to be hidden. We can accomplish this by using a custom attribute. Custom attributes are not inherited like android width or height, but brand new to define some specific behavior that can not be to handled through default propertis or attributes. To create a custom layout by using custom attributes, here is a short guide that will help you:

1. Create a linear layout with childs such as layouts, buttons, text views as per your requirement.
How to create a linear layout that will be used as a view ?
Here is the sample code…
The name of the layout is: search_handler.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@id/custom_search_layout"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@id/search_type_header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />

    <LinearLayout
        android:id="@id/video_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <EditText
            android:id="@id/search_video_edt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@id/people_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >

        <EditText
            android:id="@id/search_people_edt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <CheckBox
            android:id="@id/avatar_cb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/members_with_avatar" />
    </LinearLayout>

</LinearLayout>

2. Create a class that extends the linear layout, inflates the layout, initializes the child views of the layout and handles the custom attributes
How to create a class to handle the custom layout with its custom attributes?
Heres the sample code:



public class SearchCustomLayout extends LinearLayout implements ViewClicked {
    public SearchCustomLayout(Context context) {
        super(context);
        this.context = (SherlockActivity) context;
        init(context);
   
    }
    public SearchCustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context);
        initCustomAttribute(attrs, context);
    }
    public SearchCustomLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }
 public void init(Context context) {
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        ViewGroup viewgroup = (ViewGroup) layoutInflater.inflate(
                R.layout.search_handler, this);
        initCustomSearchLayout(viewgroup);
    }
    public void initCustomSearchLayout(ViewGroup viewGroup) {
          //perform all your initialization here
    }
}

4.Inside the public SearchCustomLayout(Context context, AttributeSet attrs) pass your attribute and use it the way you want to use it.
What do you do inside this construction and how do you pass the attribute?
Heres the sample code:

    public void initCustomAttribute(AttributeSet attrs, Context context) {
        layoutTypedArray = context.obtainStyledAttributes(attrs,
                R.styleable.CustomLayout);
        CharSequence layoutTypeCharSequence = layoutTypedArray
                .getString(R.styleable.CustomLayout_layout_type);

        if (layoutTypeCharSequence != null) {
            activeLayout = layoutTypeCharSequence.toString();

        }
        Log.i("Check for Active layout", activeLayout);
        if (activeLayout.equalsIgnoreCase("video")) {
            //use your attribute the way you want

        }
        layoutTypedArray.recycle();
    }

4. Create a attrs.xml file in res > values folder, and provide the custom attributes.
How do you create a custom attribute?
Heres the attrs.xml file with its contents:


<?xml version="1.0" encoding="utf-8"?>
<resources>
      <declare-styleable name="CustomLayout">
         <attr name="layout_type" format="string" />
     </declare-styleable>
</resources>

5. Create a layout that defines the custom namespace to use the cusom attribute and the custom layout as its child.
How to use the custom layout in other layout and use the custom layout as a view by creating a custom name space and custom attribute?
Here is how it is done…

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:search="http://schemas.android.com/apk/res/com.braindigit.jomsocial.widgets"
    android:id="@id/linear_layout_search"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.braindigit.jomsocial.widgets.SearchCustomLayout
        android:id="@+id/custom_search_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        search:layout_type="group" />

</LinearLayout>

6. Initialize the custom layout in the activity.
How is it initialized in the activity?
Here is the source code to initialize it in the activity..

public class SearchLayoutActivity extends SherlockActivity{
    SearchCustomLayout searchCustomLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(com.actionbarsherlock.R.style.Theme_Sherlock);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_layout);
        initViews();
    }
    public void initViews() {
        searchCustomLayout = (SearchCustomLayout) findViewById(R.id.custom_search_layout);
       //hence the custom linear layout is visible
    }
}

Hence, our custom view is ready. Happy Coding 😀