Showing posts with label Android Tutorial From Skratch -beginner to pro. Show all posts
Showing posts with label Android Tutorial From Skratch -beginner to pro. Show all posts

Write a program in PROLOG to implement palindrome (L) which checks whether a list L is a palindrome or not.

13.Write a program in PROLOG to implement palindrome (L) which checks whether a list L is a palindrome or not.

%Write a program in PROLOG to implement palindrome (L) which checks whether 
  a list L is a palindrome or not.

%  below palindrome function is not predefined you can use any name as function

palind([]):- write('palindrome').
palind([_]):- write('palindrome').
palind(L) :-
append([H|T], [H], L),
palind(T);
write('Not a palindrome').

% Output


Create a login application (check username and password). On successful login, pop up the message(“Welcome username”).

/*Create a login application (check username and password). On successful login, pop up the message(“Welcome username”).*/

FileName: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="15"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="190dp"
        android:id="@+id/editTextPass1"
        android:hint="password" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="15"
        android:layout_alignBottom="@+id/editTextPass"
        android:layout_alignEnd="@+id/editTextPass"
        android:layout_marginBottom="90dp"
        android:id="@+id/editTextUser1"
        android:hint="username" />

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPass1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:id="@+id/button1" />
</RelativeLayout>

FileName: MainActivity.java

/**
 * Created by Parikshit on 01-01-2019.
 */
package com.ducskecode.login_page;
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.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText username, password;
Button login;

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

username = (EditText) findViewById(R.id.editTextUser1);
password = (EditText) findViewById(R.id.editTextPass1);
login = (Button) findViewById(R.id.button1);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")){
Toast.makeText(MainActivity.this, "Welcome to the login App " +
username.getText().toString(),
Toast.LENGTH_SHORT().show();
     }
else{
    Toast.makeText(MainActivity.this, "Invalid Details,sorry!!",
                            Toast.LENGTH_SHORT).show();
               }
           }
        });
    }
}

Create a login application as above, on successful login redirect to another activity with logout button a dialog appears with OK and CANCEL button. On OK button click go to login activity and CANCEL stay at same activity.

/*Create a login application as above, on successful login redirect to 
another activity with logout button a dialog appears with OK and CANCEL button. On OK button click go to 
login activity and CANCEL stay at same activity.*/

File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ducskecode.login_logout.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="15"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="188dp"
        android:id="@+id/editTextPass1"
        android:hint="password" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="15"
        android:layout_alignBottom="@+id/editTextPass"
        android:layout_alignEnd="@+id/editTextPass"
        android:layout_marginBottom="85dp"
        android:id="@+id/editTextUser1"
        android:hint="username" />

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:id="@+id/button1" />
</RelativeLayout>

File Name: MainActivity.java

package com.ducskecode.login_logout;
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.EditText;
import android.widget.Toast;

import static android.R.attr.value;

public class MainActivity extends AppCompatActivity {

    EditText username, password;
    Button login;

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

    username = (EditText) findViewById(R.id.editTextUser1);
    password = (EditText) findViewById(R.id.editTextPass1);
    login = (Button) findViewById(R.id.button1);

    login.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v) {
    if(username.getText().toString().equals("admin") &&
    password.getText().toString().equals("admin")){
    Intent i = new Intent(MainActivity.this.getApplicationContext(),
    Logout.class);
    i.putExtra("message", value);
    startActivity(i);
            }
      else{
       Toast.makeText(MainActivity.this, "Invalid Details",
             Toast.LENGTH_SHORT().show();
               }
            }
        });
    }
}

File Name: logout.xml

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

    <Button
        android:text="Logout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp"
        android:id="@+id/buttonLogout" />
</RelativeLayout>

File Name: Logout.java
package com.ducskecode.login_logout;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import static android.content.ContentValues.TAG;

/**
 * Created by Parikshit.
 */

public class Logout extends MainActivity {

Button Logout;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logout);
        Logout = (Button) findViewById(R.id.buttonLogout);

        Intent in = getIntent();
        String string = in.getStringExtra("message");
        Logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Logout.this);
                builder.setTitle("Confirmation PopUp!").
                        setMessage("You sure, that you want to logout?");
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent i = new Intent(getApplicationContext(),
                                        MainActivity.class);
                                startActivity(i);
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert11 = builder.create();
                alert11.show();
            }
        });
    }
}



Create an application with three buttons vertically aligned, on selecting a button color of the screen will change.

/*Create an application with three buttons vertically aligned, on selecting a button color of the screen will change*/

File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RL1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="Red"
        android:layout_centerHorizontal="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:layout_centerHorizontal="true"
        android:id="@+id/btn2"
        android:layout_marginTop="30dp"
        android:text="Blue"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="Green"
        android:layout_below="@+id/btn2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"/>

</RelativeLayout>    


File Name: MainActivity.java

package com.ducskecode.color_changer_vertical;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button b1,b2,b3;
    RelativeLayout r1;

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

        b1=(Button)findViewById(R.id.btn1);
        b2=(Button)findViewById(R.id.btn2);
        b3=(Button)findViewById(R.id.btn3);
        r1=(RelativeLayout)findViewById(R.id.RL1);

        b1.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.RED);
            }
        });

        b2.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.BLUE);
            }
        });

        b3.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.GREEN);
            }
        });
    }
        public void send()
        {
            Toast.makeText(this,"color will change now",Toast.LENGTH_LONG).show();
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}        


Create an application with three buttons horizontally aligned, on selecting a button colour of the screen will change.

/*Create an application with three buttons horizontally aligned, on selecting a button colour of the screen will change*/

File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/RL1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="Red"
        android:layout_centerHorizontal="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="Blue"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn3"
        android:text="Green"
        android:layout_alignParentRight="true"/>

</RelativeLayout>    


File Name: MainActivity.java

package com.ducskecode.color_changer;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button b1,b2,b3;
    RelativeLayout r1;

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

        b1=(Button)findViewById(R.id.btn1);
        b2=(Button)findViewById(R.id.btn2);
        b3=(Button)findViewById(R.id.btn3);
        r1=(RelativeLayout)findViewById(R.id.RL1);

        b1.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.RED);
            }
        });

        b2.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.BLUE);
            }
        });

        b3.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v)
            {
                r1.setBackgroundColor(Color.GREEN);
            }
        });
    }
        public void send()
        {
            Toast.makeText(this,"color will change now",Toast.LENGTH_LONG).show();
        }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}        


Create a radio button group with radio button of all courses in your college and on selecting a particular course, teacher-in-charge of that course should appear at the bottom of the screen.

/* Create a radio button group with radio button of all courses in your college and on selecting a particular course, teacher-in-charge of that course should appear at the bottom of the screen.*/

File Name: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rg1">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Night mode App"
            android:id="@+id/RB1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Eye Filter App"
            android:id="@+id/RB2"
            android:layout_centerHorizontal="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Night Screen Mode"
            android:layout_alignParentRight="true"
            android:id="@+id/RB3"/>
    </RadioGroup>

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="500dp"
        android:textSize="35dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/T1"/>

</RelativeLayout>    

File Name: MainActivity.java

package com.ducskecode.radio_example;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private RadioGroup rgrp;
    private RadioButton r1,r2,r3;
    private TextView t1;

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

   rgrp = (RadioGroup) findViewById(R.id.rg1);
   r1=(RadioButton)findViewById(R.id.RB1);
   r2=(RadioButton)findViewById(R.id.RB2);
   r3=(RadioButton)findViewById(R.id.RB3);
   t1= (TextView)findViewById(R.id.T1);

   rgrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
     public void onCheckedChanged(RadioGroup group, int checkedId) {

     RadioButton nameRadio = (RadioButton) findViewById(checkedId);

     String s1=nameRadio.getText().toString();

     if(checkedId==r1.getId())
                {
                    String s2="Parikshit android class";
                    t1.setText(s2);
                }
                else if(checkedId==r2.getId()) {
                String s3="Parikshit c++ class";
                    t1.setText(s3);
                }
                else{
                    String s4="Parikshit system programming class";
                    t1.setText(s4);
                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}        

Create a menu with 5 options and selected option should appear in textbox.

/*Create a menu with 5 options and selected option should appear in textbox.

*/

File Name: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fuzzycode.options_menu" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
             

File Name: activity_main.xml

<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
<TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="30dp"/>
</RelativeLayout>  



File Name: menu_main.xml

<menu>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"
    tools:context=".MainActivity">
    <item
        android:id="@+id/settings"
        android:title="@string/action_settings" />
    <item
        android:id="@+id/help"
        android:title="Help"/>
    <item
        android:id="@+id/search"
        android:title="Search"/>
    <item
        android:id="@+id/contact_us"
        android:title="Your Contacts"/>
    <item
        android:id="@+id/take_care"
        android:title="Bye"/>
</menu>
 
File Name: MainActivity.java
package com.fuzzycode.options_menu;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.search:
                Uri ur = Uri.parse("https://www.thefuzzycode.blogspot.com");
                Intent ab = new Intent(Intent.ACTION_VIEW, ur);
                startActivity(ab);
                break;

            case R.id.help:
                break;

            case R.id.settings:
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(in);
                break;

            case R.id.contact_us:
                Intent cn = new Intent();
                cn.setAction(Intent.ACTION_VIEW);
                cn.setData(ContactsContract.Contacts.CONTENT_URI);
                startActivity(cn);
                break;

            case R.id.take_care:
                Toast.makeText(this, "Take Care child", Toast.LENGTH_SHORT).show();
                this.finish();
                break;

            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
}

Create spinner with strings taken from resource folder and on changing the spinner value, image will change.

/*Create spinner with strings taken from resource folder and on changing the spinner value, image will change.*/

File Name: AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:largeHeap="true"
        android:hardwareAccelerated="false">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>                

File Name: activity_main.xml

<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/sp"
        android:background="#F0F0"
        />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/background"
        android:scaleType="fitXY"
        android:layout_below="@+id/sp"
        />

</RelativeLayout>    

File Name: MainActivity.java

package com.ducskecode.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity implements OnItemSelectedListener {

    Spinner spin;
    ImageView layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spin=(Spinner)findViewById(R.id.sp);
        String[] objects = { "Image One", "Image Two", "Image Three" };
        ArrayAdapter adapter = new ArrayAdapter(
                getApplicationContext(),android.R.layout.simple_list_item_1 ,objects);

        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(this);

            layout = (ImageView) findViewById(R.id.background);

    }

    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
        Toast.makeText(getApplicationContext(),spin.getItemAtPosition(pos).toString(),                          Toast.LENGTH_LONG).show();
        int position= spin.getSelectedItemPosition();
        switch(position){
            case 0:
                layout.setImageResource(R.drawable.photo);
                break;

            case 1:
                layout.setImageResource(R.drawable.img1);
                break;

            case 2:
                layout.setImageResource(R.drawable.img2);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView arg0) {

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}        

output: 





Create an application with first activity with an edit text and send button. On click of send button, make use of implicit intent that uses a SEND ACTION and let user select app from chooser and navigate to that application.

/*Create an application with first activity with an edit text and send button. On click of send button, make use of implicit intent that uses a SEND ACTION and let user select app from chooser and navigate to that application.*/

File Name: activity_main.xml

<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="22dp"
        android:layout_marginTop="16dp"
         />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="20dp"
        >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="30dp"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentLeft="true"
        android:text="To:" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:text="Subject:" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText3"
        android:layout_alignBottom="@+id/editText3"
        android:layout_alignParentLeft="true"
        android:text="Message:" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_below="@+id/editText3"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="25dp"
        android:text="Send" />
</RelativeLayout>    

File Name: MainActivity.java

package com.ducskecode.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText editTextTo,editTextSubject,editTextMessage;
    Button send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextTo=(EditText)findViewById(R.id.editText1);
        editTextSubject=(EditText)findViewById(R.id.editText2);
        editTextMessage=(EditText)findViewById(R.id.editText3);

        send=(Button)findViewById(R.id.button1);

        send.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                String to=editTextTo.getText().toString();
                String subject=editTextSubject.getText().toString();
                String message=editTextMessage.getText().toString();
                Intent email = new Intent(Intent.ACTION_SEND);
                email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
                email.putExtra(Intent.EXTRA_SUBJECT, subject);
                email.putExtra(Intent.EXTRA_TEXT, message);
                email.setType("message/rfc822");
                startActivity(Intent.createChooser(email, "Choose an Email client :"));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

output:




Create an application with first activity with an edittext and send button. On click of send button, make use of explicit intent to send text to second activity and display there in text view.

/*Create an application with first activity with an edittext and send button. On click of send button, make use of explicit intent to send text to second activity and display there in text view.*/

File Name: AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".ExplicitActivity2"
            android:label="ExplicitApp2"/>
    </application>

</manifest>        

File Name: activity_main.xml

<RelativeLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView1"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"
        android:layout_above="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="77dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send It"
        android:id="@+id/button1"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:onClick="onClick"
        android:layout_marginTop="60dp" />

</RelativeLayout>

File Name: MainActivity.java

package com.ducskecode.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    private static final int request_code = 5;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_explicit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void onClick(View view) {

        Intent i = new Intent(this, ExplicitActivity2.class);

        final EditText editText1 = (EditText)findViewById(R.id.editText1);
        String myString = editText1.getText().toString();
        i.putExtra("qString", myString);
        startActivityForResult(i, request_code);
    }

File Name: activity_explicit_2.xml

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

    <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="86dp"
    android:text="Back TO Previous" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>        
 
File Name: ExplicitActivity2.java

package com.ducskecode.lifecycle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ExplicitActivity2 extends Activity {

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

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }

        String qString = extras.getString("qString");

        final TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setText(qString);
        b1=(Button)findViewById(R.id.button1);

        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent in = new Intent(ExplicitActivity2.this, ExplicitActivity.class);
                startActivity(in);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_explicit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

output:



Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...