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

Monday, 12 December 2016

AndroidPhoneCall

Android gives Built-in applications to telephone calls, in a few events we may need to make a telephone call through our application. This should effectively be possible by utilizing verifiable Intent with fitting activities. Likewise, we can utilize PhoneStateListener and Telephony Manager classes, keeping in mind the end goal to screen the adjustments in some communication states on the gadget.

In AndroidMainfest.XML, we should add uses-permission  for Phone Call State as shown below,

<uses-permission android:name="android.permission.CALL_PHONE" />

Activity_main.xml :-

<?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:orientation="vertical" 
 android:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android PHONE Call " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

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

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:inputType="phone" 
 android:ems="10" 
 android:layout_marginTop="20dp" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" />
</LinearLayout> 

MainActivity.java :-

package com.kiranapp;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button callbtn;
    EditText ebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 callbtn = (Button) findViewById(R.id.button);
 ebtn = (EditText) findViewById(R.id.editText);
 callbtn.setOnClickListener(new View.OnClickListener() {
 
@Override 
 public void onClick(View v) {
 String s1 = ebtn.getText().toString();
 Intent callIntent = new Intent(Intent.ACTION_CALL);
 callIntent.setData(Uri.parse("tel:" + s1));
 if (ActivityCompat.checkSelfPermission(MainActivity.this,
         Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling  // 
 ActivityCompat#requestPermissions 
 // here to request the missing permissions, and then overriding // 
 public void onRequestPermissionsResult(int requestCode, String[] permissions,
 // int[] grantResults) 
 // to handle the case where the user grants the permission. See the documentation 
 // for ActivityCompat#requestPermissions for more details. //
 return;
 }
 startActivity(callIntent);

 }
 });

 }

 }

Output :-

Android phone call
Android PhoneCall figure

Sunday, 11 December 2016

Android Wifi

Android WiFi manager can manage WiFi connectivity. And  it can be used as enable WiFi , disable WiFi and also add network or scanning for access points.

In Android manifest.XML we should add the "uses-permission". i.e shown below,

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> 

Activity_main.xml :-

<?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:orientation="vertical" 
 android:textAlignment="center">
 
<TextView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Android Wifi " 
 android:id="@+id/textView" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Enable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Disable Wifi" 
 android:layout_marginTop="20dp" 
 android:id="@+id/button1" 
 android:layout_gravity="center_horizontal" />

</LinearLayout>


MainActivity.java :-

package com.kiranapp;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button enablebtn,disablebtn;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 enablebtn = (Button) findViewById(R.id.button);
 disablebtn = (Button) findViewById(R.id.button1);
 enablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(true);

 }
 });
 disablebtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifi.setWifiEnabled(false);
 }
 });
 }
 }

Output :-

Android wifi disable mode
Android WiFi fig :1







             Android WiFi Disable mode












Android wifi enable mode
Android WiFi fig :2



                Android WiFi Enable mode

Wednesday, 16 November 2016

SplashScreen

Android application sets aside some opportunity to fire up, particularly the first run through the application is keep running on a gadget (here and there this is alluded to as a chilly begin). The sprinkle screen may show start up advance to the client, or it might show marking data to distinguish and advance the application.

And few steps to know what in splash screen,

1) we have to create one image file (i.e jpg file) resources --> drawable--> splash.jpg(upload or paste).
2) We should create one java class and xml files in the project as shown in code.
3) And Android mainfest.xml we should write the as shown below example,

<activity 
 android:name=".SplashScreen" 
 android:label="@string/app_name" >
<intent-filter>
 <action  
android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity 
 android:name=".MainActivity" 
 android:label="@string/app_name" >
<intent-filter>
<action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
 
 
code for android splash screen as shown below.
 
activity_main.xml :-
 
<?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">
</LinearLayout>
 
splash.xml :- 
 

<?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:orientation="vertical" 
 android:background="@drawable/splash">

</LinearLayout>
 
MainActivity.java :-
 
package com.kiranapp;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override 
 public void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 setContentView(R.layout.activity_main);
}
} 
 
SpalshScreen.java :-
 
package com.kiranapp;

/** * Created by Dell on 11/16/2016. */ 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class SplashScreen extends Activity {

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.splash);

 Thread timerThread = new Thread(){
 public void run(){
 try{
 sleep(3000);
 }
catch(InterruptedException e){
e.printStackTrace();
}
finally{
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
  }
 }
};
timerThread.start();
}

@Override 
 protected void onPause() {
 super.onPause();
 finish();
 }
} 
 
AndroidMainfest.xml :- 
 

<?xml version="1.0" encoding="utf-8"?> 
<manifest 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.kiranapp">

<application 
 android:allowBackup="true" 
 android:icon="@mipmap/ic_launcher" 
 android:label="@string/app_name" 
 android:theme="@style/AppTheme" >

<activity 
 android:name=".SplashScreen" 
 android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity 
 android:name=".MainActivity" 
 android:label="@string/app_name" >
<intent-filter>
<action
 android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />

<category 
 android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>
 
Output :-
 
SplashScreen
SplashScreen figure
 
 

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;
}