In the last article we saw how to create an activity with text fields , access the data from those text fields and then display that text.
Creating an activity with text fields
If you are learning android from starting and haven't read the first article then I recommend to do so.
Start developing android applications.
To transfer data from one activity to another we need to create another activity. Let's name this activity as DisplayActivity.
To display the transferred text on this activity, create a textview. Set the font size, font color and other parameters.
activity_display.xml
TextView will display the text passed to this activity with text size as 20dp and in holo blue color. Margin will leave the 20dp of empty space around the textview.
layout_centerInParent will align the textview in center, vertically and horizontally both.
Now on click of login button, we will call the function startAnotherActivity.
MainActivity.java (modified)
Now in DisplayActivity we will collect the data and display it.
DisplayActivity.java
You can also use the captured data for validation purpose. Store the actual email and password in a constants file and match them with entered value. If successful then proceed else redirect them to login screen.
Run the code and comment about your findings.
Creating an activity with text fields
If you are learning android from starting and haven't read the first article then I recommend to do so.
Start developing android applications.
To transfer data from one activity to another we need to create another activity. Let's name this activity as DisplayActivity.
To display the transferred text on this activity, create a textview. Set the font size, font color and other parameters.
activity_display.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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="mycompany.helloworld.DisplayActivity"> <TextView android:id="@+id/displaymsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" android:textColor="@android:color/holo_blue_light" android:layout_margin="20dp" android:layout_centerInParent="true"/> </RelativeLayout>
TextView will display the text passed to this activity with text size as 20dp and in holo blue color. Margin will leave the 20dp of empty space around the textview.
layout_centerInParent will align the textview in center, vertically and horizontally both.
We will use the MainActivity created in previous article to enter the data. For this article we will modify the code a little bit. Instead of displaying the data, we will start another activity.
MainActivity.xml (modified)
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:text="@string/signin" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="@android:color/background_dark" android:gravity="center" android:padding="10dp"/> <EditText android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:hint="Email" android:inputType="textEmailAddress"/> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:hint="Password" android:inputType="textWebPassword"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/login" android:background="@android:color/holo_blue_dark" android:onClick="startAnotherActivity" android:layout_margin="10dp"/> </LinearLayout> </RelativeLayout>
Now on click of login button, we will call the function startAnotherActivity.
MainActivity.java (modified)
package mycompany.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void startAnotherActivity(View view) { String email = getEmail(); String password = getPassword(); Intent intent = new Intent( this, DisplayActivity.class); intent.putExtra("email", email); intent.putExtra("pass",password); startActivity(intent); } public String getEmail() { EditText editText = (EditText)findViewById(R.id.email); String email = editText.getText().toString(); return email; } public String getPassword() { EditText editText = (EditText)findViewById(R.id.password); String password = editText.getText().toString(); return password; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.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); } }
Now in DisplayActivity we will collect the data and display it.
DisplayActivity.java
package mycompany.helloworld; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import org.w3c.dom.Text; public class DisplayActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); Intent intent = getIntent(); String email = intent.getStringExtra("email"); String password = intent.getStringExtra("pass"); TextView tv = (TextView) findViewById(R.id.displaymsg); tv.setText("Email : "+email+"\nPassword : "+password); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
You can also use the captured data for validation purpose. Store the actual email and password in a constants file and match them with entered value. If successful then proceed else redirect them to login screen.
Run the code and comment about your findings.
good codes
ReplyDelete