Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am working on a small basic program to display a calculator through in prefix of log in form in which i am having the following code.here i am having three .java files
1.Mainactivity.java
2.Signinactivity.java
3.Signupactivity.java

1.here is content of Maainactivity.java

Java
package com.hmkcode.android.sign;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button btnSignIn;
Button btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = (Button) findViewById(R.id.btnSingIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignIn.setOnClickListener(this);
btnSignUp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent i = null;
switch(v.getId()){
case R.id.btnSingIn:
i = new Intent(this,SignInActivity.class);
break;
case R.id.btnSignUp:
i = new Intent(this,SignUpActivity.class);
break;
}
startActivity(i);
}
}


2.here is content of Signinactivity.java

Java
package com.hmkcode.android.sign;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SignInActivity extends Activity {

	   private EditText  username=null;
	   private EditText  password=null;
	   private TextView attempts;
	   private Button login;
	   int counter = 3;
	   @Override
	   protected void onCreate(Bundle savedInstanceState) {
	      super.onCreate(savedInstanceState);
	      setContentView(R.layout.activity_sign_in_screen);
	      username = (EditText)findViewById(R.id.editText1);
	      password = (EditText)findViewById(R.id.editText2);
	      attempts = (TextView)findViewById(R.id.textView5);
	      attempts.setText(Integer.toString(counter));
	      login = (Button)findViewById(R.id.button1);
	   }

	   public void login(View view){
	      if(username.getText().toString().equals("admin") && 
	      password.getText().toString().equals("admin")){
	      Toast.makeText(getApplicationContext(), "Redirecting...", 
	      Toast.LENGTH_SHORT).show();
	   }	
	   else{
	      Toast.makeText(getApplicationContext(), "Wrong Credentials",
	      Toast.LENGTH_SHORT).show();
	      attempts.setBackgroundColor(Color.RED);	
	      counter--;
	      attempts.setText(Integer.toString(counter));
	      if(counter==0){
	         login.setEnabled(false);
	      }

	   }

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

	}



3.here is content of Signupactivity.java
Java
package com.hmkcode.android.sign;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class SignUpActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up_screen);
}
}


And the three XML layouts namely in res/layout
1.activity_main.xml
2.activity_sign_in_screen.xml
3.activity_sign_up_screen.xml

1.here is content of activity_main.xml
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=".MainActivity" 

    android:removed="@drawable/balloon_bg">

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:removed="@drawable/linearlayout_bg"
        android:padding="10dp"
        >

        <Button 
               android:id="@+id/btnSignUp"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:padding="10dp"
            android:layout_margin="4dp"
               android:text="Sign Up"
               android:background="@drawable/button_default_bg"
               style="@style/DefaultButtonText"
           />
        <Button 
               android:id="@+id/btnSingIn"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
            android:padding="10dp"
            android:layout_margin="4dp"
               android:text="Sign In"
            style="@style/DefaultButtonText"
               android:background="@drawable/button_default_bg"
           />
        </LinearLayout>

</RelativeLayout>

1.here is content of activity_sign_in_screen.xml
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=".MainActivity" >

   <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="18dp"
      android:text="@string/hello_world"
      android:textAppearance="?android:attr/textAppearanceLarge" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_below="@+id/textView1"
      android:layout_marginTop="50dp"
      android:text="@string/username"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/textView2"
      android:layout_marginLeft="32dp"
      android:layout_toRightOf="@+id/textView2"
      android:ems="10" >

      <requestFocus />
   </EditText>

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView2"
      android:layout_below="@+id/textView2"
      android:layout_marginTop="38dp"
      android:text="@string/password"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/editText2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/textView3"
      android:layout_alignLeft="@+id/editText1"
      android:ems="10"
      android:inputType="textPassword" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/editText2"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="94dp"
      android:/>

   <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/textView3"
      android:layout_below="@+id/textView3"
      android:layout_marginLeft="30dp"
      android:layout_marginTop="48dp"
      android:text="@string/attempts"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/textView1"
      android:layout_alignTop="@+id/textView4"
      android:text="TextView" />

</RelativeLayout>


3.here is content of activity_sign_up_screen.xml

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=".MainActivity"
android:removed="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
>
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:removed="@drawable/edittext_top_bg"
android:padding="10dp"
android:hint="Email"
android:textColorHint="#bbbbbb"
android:drawableLeft="@drawable/email"/>
<EditText
android:id="@+id/etUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:removed="@drawable/edittext_default_bg"
android:layout_marginTop="-2dp"
android:padding="10dp"
android:hint="User Name"
android:textColorHint="#bbbbbb"
android:drawableLeft="@drawable/user"/>
<EditText
android:id="@+id/etPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:removed="@drawable/edittext_bottom_bg"
android:layout_marginTop="-2dp"
android:padding="10dp"
android:hint="Password"
android:textColorHint="#bbbbbb"
android:password="true"
android:drawableLeft="@drawable/password"/>
<Button
android:id="@+id/btnSingIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="Sign Up"
style="@style/DefaultButtonText"
android:background="@drawable/button_default_bg"
/>
</LinearLayout>
</RelativeLayout>



And also here is content of res/values/string.xml

XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sign In&Up</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="username">Username:</string>
<string name="password">Password:</string>
<string name="Login">Login:</string>
<string name="attempts">Attempts Left:</string>
<style name="DefaultButtonText">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#979797</item>
<item name="android:gravity">center</item>
<item name="android:layout_margin">3dp</item>
<item name="android:textStyle">normal</item>
<item name="android:textSize">20sp</item>
<item name="android:shadowColor">#f9f9f9</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
</style>
</resources>


And the content of Androidmanifest.xml is

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hmkcode.android.sign"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hmkcode.android.sign.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.hmkcode.android.sign.SignUpActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.SignUpActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.hmkcode.android.sign.SignInActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.SignInActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>


Iam getting the output as it is written in code of activity_main.xml,when i am pressing the sign in button then it goes to next layout of activity_sign_up_screen.xml
then login screen appears ,then when i click the login button it is diplayin redirecting according to the given code but the main problem is i need to keep the simple calculator project which i go from here


As iam totally new and dont have knowledge of android and also java,would anyone please help me ..
Thanks in advance
Posted
Comments
Sinisa Hajnal 27-Nov-14 2:36am    
Just to clarify (without going through your code): do you want to write your own calculator or simply open device calculator whatever that is?
[no name] 27-Nov-14 3:12am    
its not my own i got to implement just the one whick i got from the link ib had gave.
Sinisa Hajnal 27-Nov-14 4:50am    
Then please remove all that code above and post only the parts you're having trouble with along with the explanation of what you see (current behaviour) and what you expected (wanted behaviour)

Thank you.

1 solution

I would suggest you go to http://download.oracle.com/javase/tutorial/index.html[^] to learn Java. Then take a look at some of the excellent tutorials on Android at http://www.codeproject.com/KB/android/#Android+Tutorial+Contest[^]. You also need to spend time learning how to debug or diagnose problems in your code and just post the relevant parts of your program. Few people here have time to go through something as long as you have posted.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900