top of page

Learn Android App dev: How to use wrap content, fill parent, password

Hey guys! This is Amey Lokhande. This post is mainly about what is meant by wrap content and fill parent in the layout height and width of any TextView or Button or anything else. Also I will be guiding on how to use the password EditText box in your app.

Wrap Content and Fill Parent:

So what is meant by Wrap Content and Fill Parent. Its just a few experiments away. Well, what it means is that whenever we take a Button or TextView widget then we can change the dimensions of that widget by using this feature.

Here are the steps on how to do it:

1. Drag a button widget to your MainActivity.xml. Then when you select the button you can see the layout height and layout width in the properties of that button on the right side of your screen.

2. Then in the drop down menu of the width and height you can see the two options. When you select match_parent for layout_width and layout_height, the button occupies whole screen of your activity.

Similar experiments will guide you how the size of your widgets vary by changing the layout height and width.

How to use Password and Toast Message :

Here I will be telling you how to use a password field in your app and how to display it using a toast message. Following are the steps:

1. Drag a password EditText, TextView box and a button to show the password from text fields and widgets respectively. And design your app as below:

2. Now we have to write the java code to make the App work accordingly.

i) First of all, we need to declare our password EditText and Button in our MainActivity.java.

ii) Now create a Method for displaying the password as a Toast Message. In the method cast your password and button variables. As we know Android Studio gives you the feature of auto-completing the code. So you will be able to write the code automatically. All you need is to give a hint to Android Studio.

iii) So now create a SetOnClickListener method for the button and in the Arguments write new OnClickListener and the Android Studio will complete it automatically.

iv) Now write Toast.makeText method and following code as follows.

v) Now you need to call the ShowToast() method in your OnCreate method.

Thats all!

Java Code:

public class MainActivity extends AppCompatActivity { private EditText pass_wrd; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

ShowToast(); } public void ShowToast(){ pass_wrd = (EditText)findViewById(R.id.editText); btn = (Button)findViewById(R.id.button2); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,pass_wrd.getText(),Toast.LENGTH_SHORT).show(); } }); } }

If your app has some troubles working then import following:

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;

Screenshots of the app:

Okay, this is it for this post.Thanks for reading!

bottom of page