Category Archives: Android Studio

How to configure Nox Emulator for Android Studio


Nox Player is one of the coolest Android Emulater for playing Android Games as well as for Application Developement and Testing.  You can use the Emulator to run app you are developing with Android Studio.2018-08-30_222153

  1. Download the Nox Player from the web and Install the software.[Make sure that you had installed graphics driver properly, otherwise the installation may fail].
  2. Start Nox Player, you can switch between Tablet to Mobile view by changing Advanced Settings in Nox.
  3. Let’s Turn on Developer Option by Clicking 3 or four times in Build number  [About Phone in Nox]
  4. Also Turn on USB Debugging [Developer Option]
  5. Final step is to run the adb command with ip address in command prompt. [adb resides in Nox’s bin folder]

adb  command [ run on command prompt]

nox_adb.exe connect 127.0.0.1:62001

Optionaly you can create and run a batch file wich can be used to run Nox adb  with a single click. Please make sure your batch file resides in the same bin folder where Nox player file found.[Use right click Nox Player short Cut to find the location in Windows].

You can download the batch file from Github and move to the bin folder

  • Now Start the Android Studio
  • Also start the Noxplayer and Turn on Developer option
  • Run the adb
  • Try debug App in Adroid Studio and it will be start appearing in the device list, otherwise run the adb with Run as admin a and  try again

 

Create and read simple JSON_STRING in Android Studio


JSON_STRING strings are similar to Dictionary and List in Python, they can be searched quickly. JSON_STRING can be used to store data for your application.

This post will teach you how to create and read JSON_STRING objects in Android Studio/Eclipse Android apps.

  1. Go ahead and create a simple activity app
  2. Switch to Android View
  3. Create simple JSON_STRING as follows in the MainActivity.Java class
    String JSON_STRING = "{\"DSLR\":{\"Model\":\"Sony Alpha 58\",\"Sensor\":APC}}";

    [Drop the above code at the beginning of the OnCreate method]

  4. Open main_activity.xml [can find under res/layout]
  5. Drag and Drop two TextView control to the Form.
  6. Change the name of the controls in the content_main.xml file as ‘model’ and ‘sensor’ respectively
  7. Now create variable to read a string from JSON and assign the values to controls
String model, sensor;
TextView Model, Sensor;
Model = (TextView) findViewById(R.id.model);
Sensor = (TextView) findViewById(R.id.sensor);
try {
 // get JSONObject from JSON file
 JSONObject obj = new JSONObject(JSON_STRING);
 // fetch JSONObject named employee
 JSONObject dslr = obj.getJSONObject("DSLR");
 // get employee name and salary
 model = dslr.getString("Model");
 sensor = dslr.getString("Sensor");
 // set employee name and salary in TextView's
 Model.setText("Model: "+model);
 Sensor.setText("Sensor: "+sensor);

} catch (JSONException e) {
 e.printStackTrace();
}

Alt+Enter to auto import libraries. Here the JSONObject will help you to fetch ‘DSLR’ String. In our example, we have one only one JSON string so that used two Text views only.

Remember JSON strings are case sensitive.

You can download the source code from GitHub

DISPLAY DIFFERENT LAYOUTS USING FRAGMENT IN A ANDROID STUDIO APP


Android Studio makes App development so easy with supported libraries and classes. Today we are discussing how to show different layout within the main fragment. ( A layout is GUI Windows in Android Studio, where we can place controls and widgets)

How ?

Basically, this can be done by placing a frame layout inside the main fragment layout and place the different layouts.

Create the Drawer Activity App

Android Studio Version:  1.3

Android Version: 22

Begin a New Android Project with Navigation Drawer Activity. Go to Layout folder of the project and access  fragment_main.xml  and add a FrameLayout at the end of the Relative Layout tag, replace the TextView, if exist.

<FrameLayout
    android:id="@+id/FrameContainer"    android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>

Note that we assigned an id for the FrameLayout, which can be accessible using the id. You can also customize the Drawer menu by changing the string.xml resource file.

Now we need two additional resources.

  1. An addition Layout
  2. Java Class representing Additional Layout.

Creating a Frame Layout

In Android a Window has two sides, one has GUI and design and a program part/machine part.

Let create our first FrameLayout XML file By Right click the Layout folder – New – Layout Resource File and enter following Details

Name:dashboard_fragment

Root Element : FrameLayout

Click OK and Drag Large Text Files and add “This is my Dashboard” as Text, in order to identify. We also need the second part, a class to represent the Layout.

Creating Class

A Java Class is behind the wheel for every Layout, it should extend the fragment class.

Right click your Main_Activity -New – Java Class with Name DashboardFragment and add the following overridden OnCreateView method.

public class DashboardFragment extends android.support.v4.app.Fragment {
    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) { View rootview=inflater.inflate(R.layout.
dashboard_fragment_main,container,false); return rootview;    }
}

The Navigation Fragment event

We can fill the  MainFragment’s FrameLayout with the help of fragment manager, just go to the NavigationDrawerFragment.Java  and find the private void selectItem(int position) method, at bottom of the line add the following code.

android.support.v4.app.FragmentManager frg=getFragmentManager();if (position==0){
    frg.beginTransaction().replace(R.id.FrameContainer, new DashboardFragment()).commit();

In the above code, we just replace the content of FrameContainer with new FrameLayout. You can add as many as the layout in the same manner. Here position carries menu item id.

Let’s Run the project

One Layout Class and many fragment layouts in Android Studio


Fragment programming seems to be smart steps towards making worthy applications in Android Studio. In my last experiment, I tried to develop fragments in my app, but I realized that each fragment layout is represented by a Java class which handles the activities. 2017-02-26_001655

How can I use same Java class for different layouts? The first option created an argument constructor for the Java class, but the Fragment sibling Java class doesn’t support thing like that, instead, Android Studio supplies SetArguments and getArguments methods for the purpose.

I have group Main fragment layout carries many buttons for various functions like create,  list, Report fragments respectively. I want to use same Java Class for every fragment including the maingroupFragment.

2017-06-22_084924

The Drawer app code

private void selectItem(int position) {

    mCurrentSelectedPosition = position;
    if (mDrawerListView != null) {
        mDrawerListView.setItemChecked(position, true);
    }
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
    android.support.v4.app.FragmentManager frg=getFragmentManager();
    switch (position){
        case 0:
            frg.beginTransaction().replace(R.id.FrameContainer, new DashboardFragment()).commit();
            break;
        case 1:
            frg.beginTransaction().replace(R.id.FrameContainer, new LedgerFragment()).commit();
            break;
        case 2:
            frg.beginTransaction().replace(R.id.FrameContainer, new PaymentFragment()).commit();
            break;
        case 3:
            frg.beginTransaction().replace(R.id.FrameContainer, new ReceiptFragment()).commit();
            break;
        case 4:
            frg.beginTransaction().replace(R.id.FrameContainer, new ReportFragment()).commit();
            break;
        case 5:
            Bundle b=new Bundle();
            b.putInt("GroupIndex", R.layout.groupmain_fragment);
            GroupFragment g=new GroupFragment();
            g.setArguments(b);
            frg.beginTransaction().replace(R.id.FrameContainer,g).commit();
            break;
    }
}

The Fragment class

package com.example.developerm.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
* Created by developerM on 14-Jun-2017.
*/
public class GroupFragment extends Fragment {



@Override
public void setArguments(Bundle args) {
super.setArguments(args);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// View rootview=inflater.inflate(R.layout.group_registration,container,false);
View rootview=null;
if(getArguments().getInt("GroupIndex")==R.layout.groupmain_fragment){
rootview = inflater.inflate(getArguments().getInt("GroupIndex", 0), container, false);
}
else if(getArguments().getInt("GroupNew")==R.layout.group_registration){

rootview = inflater.inflate(getArguments().getInt("GroupNew", 1), container, false);
}
else if(getArguments().getInt("GroupManage")==R.layout.managegroupsfragment)
{
rootview = inflater.inflate(getArguments().getInt("GroupManage", 2), container, false);
}
else if(getArguments().getInt("GroupAcReports")==R.layout.acreportgroupfragment)
{
rootview = inflater.inflate(getArguments().getInt("GroupAcReports", 3), container, false);
}
else if(getArguments().getInt("GroupList")==R.layout.grouplistfragment)
{
rootview = inflater.inflate(getArguments().getInt("GroupList", 4), container, false);
}

Bundle b=new Bundle();
Button bt;
if(getArguments().getInt("GroupIndex")==R.layout.groupmain_fragment){
bt=(Button) rootview.findViewById(R.id.btn_newGroup);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupNew", R.layout.group_registration);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});
bt=(Button) rootview.findViewById(R.id.btn_findEdit);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupManage", R.layout.managegroupsfragment);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});
bt=(Button) rootview.findViewById(R.id.btn_acReports);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupAcReports", R.layout.acreportgroupfragment);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});
bt=(Button) rootview.findViewById(R.id.btn_groupList);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle b=new Bundle();
b.putInt("GroupList", R.layout.grouplistfragment);
GroupFragment g=new GroupFragment();
g.setArguments(b);
android.support.v4.app.FragmentManager frg=getFragmentManager();
frg.beginTransaction().replace(R.id.FrameContainer, g).commit();
}
});


}

return rootview;
}
}

The above codes will be explained what you have to do. Use the Argument and getArgument in place.

Expect comments and suggestions