Showing posts with label Android development tutorials.. Show all posts
Showing posts with label Android development tutorials.. Show all posts

Monday, 7 November 2016

AndroidFragmentTransaction

Android Fragment exchange (or) "Fragment Transaction" to handle sections. Android Fragment are valuable when we need to bolster different screen measure. To oversee parts we require a FragmentManager that help us to handle Android piece trasaction between sections. With Android part exchange we mean a succession of ventures to include, supplant or evacuate pieces. In the last post we demonstrated to bolster numerous screen size and introduction utilizing sections.

code for FragmentTransactin shown below.

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.fragmenttransaction.MainActivity">

<Button 

 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="fragA" 
 android:id="@+id/button" 
 android:layout_alignParentTop="true" 
 android:layout_centerHorizontal="true" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="fragB" 
 android:id="@+id/button2" 
 android:layout_below="@+id/button" 
 android:layout_centerHorizontal="true" />

<FrameLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:layout_centerHorizontal="true" 
 android:id="@+id/myframe" 
 android:background="#decaca">
</FrameLayout>
</LinearLayout>

fraga.xml :-

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

<TextView 
 android:layout_width="209dp" 
 android:layout_height="54dp" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="this fragment A" 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>


fragb.xml :-

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

<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="fragment B" 
 android:id="@+id/textView2" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.fragmenttransaction;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements
                                                   View.OnClickListener {
 Button b1,b2;
 FragmentManager fragmentManager;
 FragmentTransaction fragmentTransaction,fragmentTransaction1;


@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 fragmentManager=getFragmentManager();
 b1=(Button)findViewById(R.id.button);
 b2=(Button)findViewById(R.id.button2);
 b1.setOnClickListener(this);
 b2.setOnClickListener(this);
 }

 @Override 
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.button:
 fragmentTransaction=fragmentManager.beginTransaction();
 fragA fragA=new fragA();
 fragmentTransaction.add(R.id.myframe,fragA);
 fragmentTransaction.commit();
 break;
 case R.id.button2:
 fragmentTransaction1=fragmentManager.beginTransaction();
 frabB frabB=new frabB();
 fragmentTransaction1.add(R.id.myframe,frabB);
 fragmentTransaction1.commit();
 break;
 }
 }
}


FragA.java :-

package com.fragmenttransaction;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by Dell on 7/11/2016. */
public class fragA extends Fragment {
@Nullable 
@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                                  Bundle savedInstanceState) {

 return inflater.inflate(R.layout.fraga,container,false);
 }
}

FragB.java :-

package com.fragmenttransaction;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/** * Created by Dell on 7/11/2016. */
 public class frabB extends Fragment {
@Nullable 
@Override 
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                 Bundle savedInstanceState) {

 return inflater.inflate(R.layout.fragb,container,false);
 }
}


Output :-

AndroidFragmentTransaction
AndroidFragmentTransaction fig:1




















AndroidFragmentTransaction
AndroidFragmentTransaction fig:2




















AndroidFragmentTransaction
AndroidFragmentTransaction fig:3



Thursday, 3 November 2016

ExternalStorage

Each Android-perfect gadget bolsters a common "outer stockpiling or External Storage" that you can use to spare records. This can be a removable stockpiling media(Storage media), (for example, a SD card) or an inward (non-removable) capacity. Records spared to the outer stockpiling are world-intelligible and can be changed by the client when they empower USB mass stockpiling to exchange documents(transfer files) on a PC.

Access to external storage  i.e Read or Write files.
In Android manifest.xml we have to include the code.
<manifest>
   - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
   - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
<uses-permission android : name ="android.permission.WRITE_EXTERNAL_STORAGE/>
<uses-permission android : name ="android.permission.READ_EXTERNAL_STORAGE/>
- - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - - - -

</manifest>

In MainActivity.java we have to write code for external storage below shown how it is,

                          //WRITE FILE //
public boolean isExternalStorageWritable() {
    String externalstate = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(externalstate)) {
        return true;
    }
    return false;
}

                         //READ FILE //
public boolean isExternalStorageReadable() {
    String externalstate = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(externalstate) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(externalstate)) {
        return true;
    }
    return false;
}

Thursday, 13 October 2016

ProgressBar

Progress Bar is used to show progress of the task in android.It is android widget. For Example its shows like  Downloading or Uploading or Processing from internet or others. So see below the code of progress bar with code. They are different progress bars are there :  Circular progress bar, Horizontal progress bar, Custom progress bar, Spinner progress bar
And Add the image of the "progress bar" i.e
Resources or Res------>Drawable------->download.jpg(i.e figure)

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">


<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="progressbar button"
  android:id="@+id/button"
  android:layout_marginTop="30dp"
  android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp :-

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button;

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //USing Progress Dialog Class
                ProgressDialog pd=new ProgressDialog(MainActivity.this);
                pd.setTitle("processing");
                pd.setMessage("wait for downloading");
                pd.setIcon(R.drawable.download);

                pd.show();
            }
        });
    }
}

Output :-

ProgressBar
ProgressBar fig:1




















ProgressBar
ProgressBar fig:2