Wednesday, 11 January 2023

Android starting questions

 Hi All


1. Print toast message on the click of button

2. Create login activity and registration activity.

On the click of button move to registration activity.

3. 2. Create login activity and registration activity.

On the click of button check username and password and move to registration activity.

4. Set background image to activity.


Homework

1. Show the working of four function calculator


Innovation work

---------------

1. After login show the user name in the second activity.

[Passing values between activities]



Monday, 10 October 2022

Third - Displayactivity.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: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.example.hupandroid1.DisplayActivity" >


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="29dp"

        android:text="Total Salary" />


    <EditText

        android:id="@+id/totalsalary"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="35dp"

        android:ems="10" >


        <requestFocus />

    </EditText>


    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/editText1"

        android:layout_below="@+id/editText1"

        android:layout_marginTop="48dp"

        android:text="Back" />


</RelativeLayout>


Second - Salary Activity.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: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.example.hupandroid1.SalaryActivity" >


    <EditText

        android:id="@+id/bp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_alignParentRight="true"

        android:layout_below="@+id/textView1" >


        <requestFocus />

    </EditText>


    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/editText2"

        android:layout_alignParentLeft="true"

        android:text="DA" />


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView2"

        android:layout_alignParentTop="true"

        android:text="Basic Pay" />


    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/bp"

        android:layout_alignParentBottom="true"

        android:onClick="calc"

        android:text="Calculate" />


    <EditText

        android:id="@+id/da"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/button1"

        android:layout_alignBottom="@+id/button1"

        android:layout_alignRight="@+id/bp"

        android:layout_marginLeft="22dp"

        android:layout_toRightOf="@+id/button1" />


</RelativeLayout>


First - Main Activity Layout.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: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.example.hupandroid1.MainActivity" >


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="17dp"

        android:text="User Name" />


    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/textView1"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="93dp"

        android:text="Password" />


    <EditText

        android:id="@+id/editText1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView1"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="23dp"

        android:ems="10" >


        <requestFocus />

    </EditText>


    <EditText

        android:id="@+id/editText2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/textView2"

        android:layout_below="@+id/textView2"

        android:layout_marginTop="22dp"

        android:ems="10" />


    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/editText2"

        android:layout_below="@+id/editText2"

        android:layout_marginTop="56dp"

        android:text="Login"

        android:onClick="login" />


</RelativeLayout>


Third - Display Activity

 package com.example.hupandroid1;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.EditText;

import android.widget.Toast;


public class DisplayActivity extends Activity {

EditText edtotal;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_display);

Intent in=getIntent();

String total=in.getStringExtra("total");

edtotal=(EditText)findViewById(R.id.totalsalary);

edtotal.setText(total);

Toast.makeText(this, ""+total, Toast.LENGTH_LONG).show();

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.display, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}


Second - Salary Activity

 package com.example.hupandroid1;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;


public class SalaryActivity extends Activity {

EditText bp,da;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_salary);

}


public void calc(View v)

{

bp=(EditText)findViewById(R.id.bp);

da=(EditText)findViewById(R.id.da);

int bpay=Integer.parseInt(bp.getText().toString());

int daval=Integer.parseInt(da.getText().toString());

int total=bpay+daval;

String stotal=""+total;

Toast.makeText(this, ""+total, Toast.LENGTH_SHORT).show();

Intent in=new Intent(SalaryActivity.this,DisplayActivity.class);

in.putExtra("total", stotal);

startActivity(in);

 

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.salary, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}


First - Main Activity

 package com.example.hupandroid1;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;


public class MainActivity extends Activity {

EditText username,password;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void login(View v)

{

username=(EditText)findViewById(R.id.editText1);

password=(EditText)findViewById(R.id.editText2);

String uname=username.getText().toString();

String pass=password.getText().toString();

if(uname.equals("admin") && pass.equals("123"))

{

//Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();

Intent in=new Intent(MainActivity.this,SalaryActivity.class);

startActivity(in);

}

else

{

Toast.makeText(this, "Invalid User", Toast.LENGTH_SHORT).show();

}

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}