Skip to main content

Login Activity Example Using Intent

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

  <RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout1" android:layout_height="match_parent" android:background="@color/bgcolor"> 
     
 <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginTop="45dp" android:text="Enter User Name :"></TextView>       

 <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:layout_below="@+id/textView1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:id="@+id/txtusername" android:hint="Enter UserName"/>                           

<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_below="@+id/txtusername"
android:layout_alignParentLeft="true" android:layout_marginTop="20dp" android:text="Enter Password :"/>        

<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:layout_below="@+id/textView2" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:id="@+id/txtpassword" android:hint="Enter Password"/>    
  
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/btnlogin" android:layout_toRightOf="@+id/textView1" android:text="Clear" android:id="@+id/btnclear"/>   

 <Button android:id="@+id/btnlogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:layout_below="@+id/txtpassword" android:layout_alignRight="@+id/textView2" android:layout_marginRight="33dp" android:layout_marginTop="21dp"/>   
</RelativeLayout>
</LinearLayout>
**********************************************************************************
JAVA :
SecondActivity.java :
public class SecondActivity extends Activity implements OnClickListener {  
    public void onCreate(Bundle savedInstanceState)
     {        
     super.onCreate(savedInstanceState);        
     setContentView(R.layout.main);        
     Button login = (Button)findViewById(R.id.btnlogin);        
     Button clear = (Button)findViewById(R.id.btnclear);       
     login.setOnClickListener(this);       
     clear.setOnClickListener(this);   
    }
   public void onClick(View v)    
   {     
     EditText username = (EditText)findViewById(R.id.txtusername);   
     EditText password =(EditText)findViewById(R.id.txtpassword);
     if(v.getId()==R.id.btnlogin)     
    {
           if(username.getText().toString().equals("keval")&&password.getText().toString().equals("nagaria"))       {      
      //Toast.makeText(this, "ok", Toast.LENGTH_LONG).show();      
     Intent i = new Intent(this,S2.class);       
     i.putExtra("username", username.getText().toString());
     startActivity(i);   
 }     
      else      
{       \
      Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();    
       }      }    
     if(v.getId()==R.id.btnclear)   
     {      
           username.setText("");      
           password.setText("");    
      }    
}
}
**********************************************************************************
S2.java :
public class S2 extends Activity  {   
     public void onCreate(Bundle savedInstanceState)
    {       
             super.onCreate(savedInstanceState);        
             setContentView(R.layout.main);
             Intent i = getIntent();        
           //Toast.makeText(this, "Welcome",Toast.LENGTH_LONG).show();     
             String username=i.getStringExtra("username");              
            TextView tv=new TextView(this);      
            Button btnBack = new Button(this);       
            btnBack.setText("Back");       
            tv.setText("Welcome "+username+" !");       
            tv.setTextColor(Color.rgb(255, 255, 100));                  
            tv.setTextSize(25);        
            LinearLayout ll = new LinearLayout(this);       ll.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));         ll.setOrientation(LinearLayout.VERTICAL);        
             ll.addView(tv);      
             ll.addView(btnBack);              
             setContentView(ll);
             btnBack.setOnClickListener(new Button.OnClickListener()     
            {       
            public void onClick(View v)       
           {         
                 setContentView(R.layout.main);        
           }     
            }); 

}
  

Comments

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"