Main Activity
package com.aabit.handler; import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText un,pwd; Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init(){ un=(EditText)findViewById(R.id.un); pwd=(EditText)findViewById(R.id.pwd); login=(Button)findViewById(R.id.login); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new ThreadLogin(un.getText().toString(),pwd.getText().toString(),new myHandler()).execute((Object[])null); } }); } private class myHandler extends Handler { @Override public void handleMessage(Message msg) { super.handleMessage(msg); boolean status=(boolean)msg.obj; if(status){ Toast.makeText(getApplicationContext(),"Good",Toast.LENGTH_LONG).show(); } else{ Toast.makeText(getApplicationContext(),"Bad",Toast.LENGTH_LONG).show(); } } } }
ThreadLogin.class
package com.aabit.handler; import android.os.AsyncTask; import android.os.Message; import android.os.Handler; import android.text.Editable; /** * Created by aabit on 2/27/16. */public class ThreadLogin extends AsyncTask<Object,Object,Boolean> { String uname; String pwd; Handler myHandler; public ThreadLogin(String uname, String pwd, Handler myHandler) { this.uname = uname; this.pwd = pwd; this.myHandler = myHandler; } @Override protected Boolean doInBackground(Object... params) { if(this.uname.equals(this.pwd)) return true; else return false; } @Override protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); } @Override protected void onPostExecute(Boolean aBoolean) { super.onPostExecute(aBoolean); Message msg = new Message(); msg.obj = aBoolean; myHandler.sendMessage(msg); } @Override protected void onPreExecute() { super.onPreExecute(); } }LayoutActivity_main<?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: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.aabit.handler.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="160dp" android:padding="20dp"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="Name" android:ems="10" android:id="@+id/un" android:layout_gravity="center" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/pwd" android:layout_gravity="center" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/login" android:layout_gravity="center_horizontal" /> </LinearLayout> </RelativeLayout>
Saturday, February 27, 2016
Android Tutorial #1 Handler Description
Subscribe to:
Post Comments (Atom)
Very good tutorial for every learner
ReplyDelete