Monday, 31 July 2017

Dialog with MultipleChoice ListView

ScreenShots:





strings.xml:

<resources>     <string name="app_name">Dialog with Multiple Choice</string>     <string-array name="ITEMS">         <item>Item1</item>         <item>Item2</item>         <item>Item3</item>         <item>Item4</item>         <item>Item5</item>     </string-array> </resources>
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">
    <Button         android:id="@+id/button_dialogList"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Dialog List" />     <TextView         android:id="@+id/textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="10dp"         android:layout_marginTop="20dp"         android:textSize="30sp"         android:textStyle="bold" /> </LinearLayout>
MainActivity.java:
package com.ramsandroid.dialogwithmultiplechoice; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener {     ArrayList<Integer> selectedItems;     TextView textView;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         selectedItems = new ArrayList<>();         textView = (TextView) findViewById(R.id.textview); Button button_dialogList = (Button) findViewById(R.id.button_dialogList);         button_dialogList.setOnClickListener(this);     }     @Override    public void onClick(View v) {   AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);         builder.setTitle("Select Item");         builder.setMultiChoiceItems(R.array.ITEMS, null, new DialogInterface.OnMultiChoiceClickListener() {             @Override             public void onClick(DialogInterface dialog, int which, boolean isChecked) {                 if (isChecked) {                     selectedItems.add(which);                 } else
if (selectedItems.contains(which)) {           selectedItems.remove(Integer.valueOf(which));                 }             }         });         builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {             @Override             public void onClick(DialogInterface dialog, int which) {                 for (Integer i : selectedItems) {     String[] ITEMS = getResources().getStringArray(R.array.ITEMS);                     textView.append(ITEMS[i] + "\n");               }             }         });         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {             @Override       public void onClick(DialogInterface dialog, int which) {                 selectedItems.clear();             }         });         builder.create().show();     } }

Tuesday, 20 June 2017

Bottom NavigationView Example

Download Complete Project Here :BottomNavigationView.zip

screenshot:


build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion
25
   
buildToolsVersion "25.0.2"
   
defaultConfig {
        applicationId
"com.ramsandroid.bottomnavigationviewexample"
       
minSdkVersion 15
       
targetSdkVersion 25
       
versionCode 1
       
versionName "1.0"
       
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
       
vectorDrawables.useSupportLibrary = true
   
}
    buildTypes {
        release {
            minifyEnabled
false
           
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       
}
    }
}

dependencies {
    compile fileTree(
dir: 'libs', include: ['*.jar'])
    androidTestCompile(
'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude
group: 'com.android.support', module: 'support-annotations'
   
})
    compile
'com.android.support:appcompat-v7:25.1.1'
   
compile 'com.android.support:design:25.1.1'
   
compile 'com.android.support:support-vector-drawable:25.1.1'
   
testCompile 'junit:junit:4.12'
}


strings.xml:

<resources>
    <string name="app_name">Bottom NavigationView Example</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Search</string>
    <string name="title_notifications">Notifications</string>
</resources>


create navigation.xml file under menu folder and keep following code and you can icons from downloaded project
  

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

    <
item
       
android:id="@+id/navigation_home"
       
android:icon="@drawable/home"
       
android:title="@string/title_home" />

    <
item
       
android:id="@+id/navigation_dashboard"
       
android:icon="@drawable/search"
       
android:title="@string/title_dashboard" />

    <
item
       
android:id="@+id/navigation_notifications"
       
android:icon="@drawable/notifications"
       
android:title="@string/title_notifications" />

</
menu>


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/container"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context="com.ramsandroid.bottomnavigationviewexample.MainActivity">

    <
FrameLayout
       
android:id="@+id/content"
       
android:layout_width="match_parent"
       
android:layout_height="0dp"
       
android:layout_weight="1">

        <
TextView
           
android:id="@+id/message"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_marginBottom="@dimen/activity_vertical_margin"
           
android:layout_marginLeft="@dimen/activity_horizontal_margin"
           
android:layout_marginRight="@dimen/activity_horizontal_margin"
           
android:layout_marginTop="@dimen/activity_vertical_margin"
           
android:text="@string/title_home" />

    </
FrameLayout>

    <
android.support.design.widget.BottomNavigationView
       
android:id="@+id/navigation"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_gravity="bottom"
       
android:background="?android:attr/windowBackground"
       
app:menu="@menu/navigation" />

</
LinearLayout>

MainActivity.java:
package com.ramsandroid.bottomnavigationviewexample;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    @Override
   
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
           
= new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
       
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:

                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }

    };



}

Thursday, 16 February 2017

Service Example with Getting Running Services from the device

MyService.java:
 package com.ramsandroid.demo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(getApplicationContext(),
        "service created",Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent,  int flags, 
                                           int startId) {
        Toast.makeText(getApplicationContext(),
             "service started",Toast.LENGTH_SHORT).show();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(),
              "service stopped",Toast.LENGTH_SHORT).show();

    }
}



activity_main.java:
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ramsandroid.demo.MainActivity">


    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="27dp"
        android:text="Switch"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    
</RelativeLayout>


MainActivity.java:
 package com.ramsandroid.demo;

import android.app.ActivityManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.List;

public class MainActivity extends AppCompatActivity {

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

        ActivityManager am = (ActivityManager)
                  this.getSystemService(ACTIVITY_SERVICE);

        List<ActivityManager.RunningServiceInfo> rs = 
                  am.getRunningServices(Integer.MAX_VALUE);

        Switch aSwitch=(Switch)findViewById(R.id.switch1);

        for(ActivityManager.RunningServiceInfo runningServiceInfo:rs){

            String sname = runningServiceInfo.service.getClassName();
            Log.d("MainActivity",sname);

            if(sname.equals("com.ramsandroid.demo.MyService")){
                aSwitch.setChecked(true);
            }
        }


        aSwitch.setOnCheckedChangeListener
               (new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged
                  (CompoundButton compoundButton, boolean b) {
                Intent intent=
                   new Intent(getApplicationContext(),MyService.class);

                if(b){
                    startService(intent);
                }else{
                    stopService(intent);
                }
            }
        });
    }

    
}