LoginCounters in Android






 


The Xml code for Creating LoginCounters In Android

<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:background="#003333"

    android:orientation="vertical"

    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:id="@+id/editText"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:focusable="true"

        android:hint="Enter Name"

        android:textColorHighlight="#ff7eff15"

        android:textColorHint="#ffff25e6" />

    <EditText

        android:id="@+id/editText2"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:ems="10"

        android:hint="Password"

        android:inputType="textPassword"

        android:textColorHint="#ffff299f" />



    <TextView

        android:id="@+id/textView3"

        android:layout_width="260dp"

        android:layout_height="wrap_content"

        android:text="New Text"

        android:textColor="#FFFFFF"

        android:textSize="25dp" />

    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Attempts Left:"

        android:textColor="#FFFFFF"

        android:textSize="25dp" />

    <Button

        android:id="@+id/button"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="24dp"

        android:text="login" />

    <Button

        android:id="@+id/button2"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Cancel" />

</LinearLayout>

The code below is for MainActivity.Java
package example.logincounters;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity  {
   Button b1,b2;
   EditText ed1,ed2;
   TextView tx1;
   int counter = 3;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1=(Button)findViewById(R.id.button);
      ed1=(EditText)findViewById(R.id.editText);
      ed2=(EditText)findViewById(R.id.editText2);
      b2=(Button)findViewById(R.id.button2);
      tx1=(TextView)findViewById(R.id.textView3);
      tx1.setVisibility(View.GONE);
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if(ed1.getText().toString().equals("admin") &&
            ed2.getText().toString().equals("admin")) {
               Toast.makeText(getApplicationContext(),"Redirecting...",Toast.LENGTH_SHORT).show();
            }
            else{
               Toast.makeText(getApplicationContext(),"Wrong Credentials",Toast.LENGTH_SHORT).show();
               tx1.setVisibility(View.VISIBLE);
               tx1.setBackgroundColor(Color.RED);
               counter--;
               tx1.setText(Integer.toString(counter));
               if (counter == 0) {
                  b1.setEnabled(false);
               }
            }
         }
      });
      b2.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
            finish();
         }
      });
   }
   @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();
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment