Register Login

wrap_content, fill_parent, Password Field, Toast in Android

Updated Jul 30, 2018

Wrap_content and Fill_parent properties

First time when we drag and drop a button in the activity screen, it’s layout property. When you go to the property called layout width it’s property is set to wrap_content. And wrap content just means that the component just wants to display big enough to enclose its content. So its button content is the only button and the button size or width is enough to show the button text on this button. In the same way, the layout height is also wrap_content here. So the height is also enough to show the button content.

But when we change the layout width, for example, changing it to fill_parent it fills all the width of our activity. So the fill parent method the component or in the fill_parent method want to display as big as its parent. And the parent is activity here, right and fills the remaining space. So whenever you change the layout width to fill_parent it fills all the space in the parent.

Now change the layout height of this button to fill_parent and now whenever you do this for layout height it fills all the height all the space which is available at the height of the parent.

And when you do fill_parent for both height and width, it’ll fill all the space of your parent.

So these are some of the properties to resize and adjust the layout width, layout height of your widget.

Use Password field in Android activity.

Please follow the steps given below in order to use password field in Android activity:

Step 1) First, go to the text field category and drag and drop the password category in the activity screen,give the width as fill_parent so it fills the parent. And now this password whenever I run the activity, here always comes the, it hides the characters and it only shows the specific character, which you want to display.

Step 2) So now when we run this app on an emulator, you can see that whenever you want to run some text in this password text field you will see every character is replaced by this dot.

Step 3) Now we want to use this button to show whatever we have entered in the password field so what we have to go to main activity here so in the project we are going to go to MainActivity.java file and in here in the main activity itself we are going to declare two variables.

public class MainActivity extends ActionBarActivity {
     private EditText pass_word;
     private Button button_sbm;
     @Override
     protected void onCreate (Bundle savedInstanceState) {
          super.onCreate (savedInstanceState) ;
          setContentView (R.layout.activity_main) ;
     
       }

Step 4) Now we have to create a method so that on button click event, this method is called and then we are going to display this text.We also have to add a listener to this button

public void addListnerOnButton () {
     pass_word = (EditText) findViewById (R.id.editText) ;
     button_sbm = (Button) findViewById (R.id.button) ;
     button_sbm.setOnClickListener (
             new View.OnClickListener () {
                 @Override
                 public void onClick (View v) {

Step 5) Now inside your onClick method, we can also display a message. Now to display a message on Android you need to import a library called toast and toast is a library which we can use to show messages.

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;

Step 6) So to display a message you just need to write Toast.maketext and inside this make text you can display a message. So what you can do here is you just need to give the name of your activity, pass that as the first argument dot this and the second argument we are going to pass is the password field which we are going to take. Now outside the make text method and just write .show(). And this will show this text message whatever we have entered in the password field. So this is also one kind of button listener we have made indirectly and this button listener we can add this method to our on create method here like this and then your activity will listen for this button click event.

public void onClick (View v) {
     Toast.makeText(
             MainActivity.this, pass_word.getTex
             Toast.LENGTH_SHORT
     ) .show () ;



So our app is running now and we are going to enter some text here as a password, for example, youtube and click okay and when you press the button, it shows the message youtube and will disappear after some time.

Read Next About Android Checkbox Basics


×