Thursday, August 25, 2022

How to Use Git in Android Studio

In this blog, you will learn how to use Git in Android Studio. Firstly, you will learn the basic concept of Git and then create a new project using Git. Next, you will learn how to clone and fork a repository. Next up, you will see how to push your changes online using Github.  Also you can learn:
  • Integrating Git into the project
  • Setting up remote connections
  • Staging and committing changes
  • Pushing the changes to remote
  • Fetching the changes from remote
  • Working with branches
  • Showing log history

Note: This article uses Android Studio on a Windows OS machine for all references to paths, options, names, and configurations. Although it might alter significantly on other operating systems like Mac or Linux, the concept will remain the same.

Integrate Git into the project
Test whether Git is configured
In Android Studio, go to Android Studio > File > Settings > Version Control > Git. Click Test to ensure that Git is configured properly in Android Studio.

Enable version control integration
Suppose you’ve just created an Android project. In Android Studio, go to VCS > Enable Version Control Integration. This option won’t be visible if it’s integrated with any version control before.


Select Git version control system. On a successful enable VCS, a default local master branch will be created.

Add .gitignore to exclude files from Git
When you create a new Android project with Android Studio, two .gitignore files are automatically added (one in the project root folder, and the app folder). Files like generated code, binary files (executables, APKs), local configuration files should not be added to Git. Those files should be excluded from version control. Here’s my initial .gitignore file content:
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
File extensions that you want to keep outside the git track can be added. 
Stage and Commit Changes
The project is ready to use with Git version control. To stage and commit your changes, go to Git> Commit.
You will get a dialog to examine all files that will be added, enter commit messages and commit. You can deselect any files that you don’t want to belong to this commit.
When you click commit, a popup shows that you haven’t configured your username and email yet. You should always configure them because they will be attached to your commit message.
All done — now the whole project has been added to Git.
Set Up Remote Connections

To add the project to the remote repository, go to  Git > Manage Remotes :



Click on + and add the remote repo URL . Your local project is now linked to your remote Github repository. You can utilize Bitbucket, Gitlab, or any repository in addition to Github.

Push the Changes to Remote

To push your local changes to the remote repository, go to Git > Push.

The “Push Commits” popup will display which commit will be pushed to the remote-tracking branch. You can go ahead and make the push.
Fetch the Changes From Remote
To download the latest changes from remote, go to Git > Pull.

Work With Branches
Git's branching approach is referred to as its "killer feature" by some, and it undoubtedly distinguishes Git from other VCS systems. I'll demonstrate how to use branches in Android Studio in this part.
Create a new branch

Go to Git > Branches

“Git Branches” appears. It shows all the local branches and remote branches as well as the “New branch” option. If you click on New branch, you can create a new branch.
You can have a lot of options with the existing branch

  • Checkout: checkout master branch.
  • Checkout As: checkout new branch from master
  • Checkout and Rebase onto Current: checkout master and rebase feature branch onto it.
  • Compare with Current: commits that exist in master but don’t exist in feature and vice versa.
  • Show Diff with Working Tree: show the difference between the master and the current working tree.
  • Rebase Current onto Selected: rebase master on feature.
  • Merge into Current: merge master into feature.
  • Rename: rename the master branch.
  • Delete: delete the master branch.

You can choose the right option based on your needs. 
Show Log History
Go to Git > Show History.

It shows the log 



Tuesday, August 23, 2022

How to upgrade Git on Windows to the latest version

Upgrading Git on Windows is a simple process that can be accomplished in a few minutes. You shouldn't need to take any special action to update your Git client software; simply update the program as instructed in the installation message.

You can find the version of your installed git with the below command:
git --version:


To upgrade the git to latest version from Git 2.16.1(2) you can use the below command:

C:\> git update-git-for-windows

In versions between 2.14.2 and 2.16.1, the command was:


C:\> git update

This command does not exist in Git 2.13 and before. For that you need to use the below method:

You can use the latest stable release of Git on Windows in its original form or you can use the latest version of Git for Windows. Downloading the latest version may take some time, depending on the server bandwidth. Once it is downloaded, run the installer and follow step-by-step instructions to install Git on Windows.

You can download it from: https://git-scm.com/downloads

This will automatically finds the existing version and upgrades it to latest version.

Git for Windows is the best way to get started with version control on Windows. 
The Git for Windows installer includes everything you need to get started on Windows. The installer does not include a Git server, you will need to install your own Git server if you wish to distribute code using Git. To install an existing standalone Git server or an old version of JGit, see the JGit installation instructions.. The above process is for git client.

If you already have the latest version it does nothing, in which case you can manually run the installer to reinstall.


C:\> git update-git-for-windows
Git for Windows 2.17.0.windows.1 (64bit)
Up to date

Sunday, August 21, 2022

Way to fix “cannot resolve symbol R” in Android Studio

The problem "Cannot resolve symbol R" may have appeared frequently while you were developing android projects. The R is red when you initially create a new activity or class, and Android Studio informs you that it cannot identify the sign R. To import the necessary files that are missing, simply hover over the R symbol and click Alt + Enter. But doing so does not correct this mistake. The resource is represented by the letter R. The build process' inability to sync Resource files with your projects is the cause of this problem. This typically occurs as a result of the project's faulty construction.

When you move your code to another computer or email the code to another user, you frequently get the problem "Cannot resolve symbol R" in Android Studio. When an application cannot be run, the "R" becomes red and throws an error. The "R cannot be resolved" error most frequently occurs when one or more of your resource files are problematic. You are unable to create your application because of this issue. We must therefore find a solution to this problem because a simple restart or pressing Alt+Enter won't make it go away.

To solve this problem, the easiest way is to Gradle Sync by clicking on  File > Sync project with Gradle Files.


After this you can clean and rebuild the project and the error is gone. !!!

Friday, August 19, 2022

Adding a scrollbar to listview in android

Listview Displays a vertically-scrollable collection of views, where each view is positioned immediately below the previous view in the list. For a more modern, flexible, and performant approach to displaying lists, use RecyclerView.

To display a list, you can include a list view in your layout XML file:

<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
To make it scrollable you need to add: 
android:scrollbars=”vertical”
xml file looks as below: 
<?xml version="1.0" encoding="utf-8"?>
<relativeLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <listView
            android:id="@+id/listview"
            android:scrollbars="vertical"
            android:layout_width="fill_parent"
            android:layout_height="200dip"
            android:layout_gravity="fill_vertical"
            android:layout_alignParentTop="true"
            >
        </listView>
 
</relativeLayout>
Now the java file snippet looks like: 
   
package com.marutsoft.exampleListview;
 
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
 
public class Example extends Activity
{
    /** Called when the activity is first created. */
    ListView list;
    private List<string> List_file;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        List_file =new ArrayList<string>();
        list = (ListView)findViewById(R.id.listview);
 
        CreateListView();
    }
    private void CreateListView()
    {
        
         List_file.add("Google");
         List_file.add("Android");
         List_file.add("Apple");
         List_file.add("iPhone");
         
         //Create an adapter for the listView and add the ArrayList to the adapter.
         list.setAdapter(new ArrayAdapter<string>(Example.this, android.R.layout.simple_list_item_1,List_file));
         list.setOnItemClickListener(new OnItemClickListener()
           {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
                {
                    
                }
           });
    }
}