Lesson 12. List and Popup Messages

Lesson 12. List and Popup Messages


In this lesson we:

– Consider application logs and pop-up messages

Let’s create a project:

Project name: P0121_LogAndMess
Build Target: Android 2.3.3
Application name: LogAndMess
Package name: ru.startandroid.develop.logandmess
Create Activity: MainActivity

let’s create in main.xml screen familiar to us over past handler lessons:



    
        
        
        
        
    

The algorithm of the program will be the same. Pressing the buttons changes the text. Handler – Activity.

public class MainActivity extends Activity implements OnClickListener {

  TextView tvOut;
  Button btnOk;
  Button btnCancel;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // найдем View-элементы
    tvOut = (TextView) findViewById(R.id.tvOut);
    btnOk = (Button) findViewById(R.id.btnOk);
    btnCancel = (Button) findViewById(R.id.btnCancel);

    // присваиваем обработчик кнопкам
    btnOk.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    // по id определяем кнопку, вызвавшую этот обработчик
    switch (v.getId()) {
    case R.id.btnOk:
      // кнопка ОК
      tvOut.setText("Нажата кнопка ОК");
      break;
    case R.id.btnCancel:
      // кнопка Cancel
      tvOut.setText("Нажата кнопка Cancel");
      break;
    }
  }

}

Save, run. Make sure everything works.

List of applications

When you test the application, you can see logs work. They are displayed in the window LogCat. Open the menu to display the window Window> Show View> Other … In the window, select Android> LogCat

The LogCat tab should appear

Let’s take a closer look at this tab. The list has different levels of importance: ERROR, WARN, INFO, DEBUG, VERBOSE (descending). The buttons V D I W E (in circles) are filters and correspond to the types of logs. Try them and note that the filter shows logs not only of their level, but also of the highest importance. You can also create, edit, and delete your filters – we’ll take a look at this later.

Let’s look at how to write logs ourselves. This is quite easy with the Log class and its methods Log.v () Log.d () Log.i () Log.w () and Log.e (). The names of the methods correspond to the level of beams they will record.

change the code MainActivity.java. Let’s take all the comments from the code and add it to the DEBUG logs using the Log.d method. The method requires input tag and text message. A tag is a type of tag so that it can be easier to find our message in the heap of system logs. Add a tag description (TAG) and write all the comment texts in the log.

public class MainActivity extends Activity implements OnClickListener {

  TextView tvOut;
  Button btnOk;
  Button btnCancel;

  private static final String TAG = "myLogs";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // найдем View-элементы
    Log.d(TAG, "найдем View-элементы");
    tvOut = (TextView) findViewById(R.id.tvOut);
    btnOk = (Button) findViewById(R.id.btnOk);
    btnCancel = (Button) findViewById(R.id.btnCancel);

    // присваиваем обработчик кнопкам
    Log.d(TAG, "присваиваем обработчик кнопкам");
    btnOk.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    // по id определяем кнопку, вызвавшую этот обработчик
    Log.d(TAG, "по id определяем кнопку, вызвавшую этот обработчик");
    switch (v.getId()) {
    case R.id.btnOk:
      // кнопка ОК
      Log.d(TAG, "кнопка ОК");
      tvOut.setText("Нажата кнопка ОК");
      break;
    case R.id.btnCancel:
      // кнопка Cancel
      Log.d(TAG, "кнопка Cancel");
      tvOut.setText("Нажата кнопка Cancel");
      break;
    }
  }

}

Eclipse swears he doesn’t know the Log class. Update Import (CTRL + SHIFT + O) and, if prompted, select android.util.Log. Let’s launch the application, press the buttons and look at the logs

It is clear that everything is well written. For your convenience, let’s create our own filter. click icon +

filter name arbitrary, for example, “My logs». Log Tag is just the value of the TAG constant described in our code and used in the Log.d method, that is – “myLogs“. Pid leave empty, This is the process id. level we will put Debug

and click OK. A new My logs tab appears showing logs that match the filter you just created.

We have logged the text, but of course you can write, for example, values ​​you are interested in variables (reduced to String).

Sometimes logs do not appear in the LogCat tab, although AVD is running, the application works without problems. In this case, the following should help: in Eclipse go to Window> Open Perspective> Other> DDMS. A slightly different set of windows will open than usual. Find the Devices tab there and your AVD device should be visible, click on it and logs should appear. To go back to development: Window> Open Perspective> Java.

pop-up messages

The application can show pop-up messages using the class Toast. Let’s edit the onClick method. Let’s make a message pop up about which button was clicked.

  public void onClick(View v) {
    // по id определяем кнопку, вызвавшую этот обработчик
    Log.d(TAG, "по id определяем кнопку, вызвавшую этот обработчик");
    switch (v.getId()) {
    case R.id.btnOk:
      // кнопка ОК
      Log.d(TAG, "кнопка ОК");
      tvOut.setText("Нажата кнопка ОК");
      Toast.makeText(this, "Нажата кнопка ОК", Toast.LENGTH_LONG).show();
      break;
    case R.id.btnCancel:
      // кнопка Cancel
      Log.d(TAG, "кнопка Cancel");
      tvOut.setText("Нажата кнопка Cancel");
      Toast.makeText(this, "Нажата кнопка Cancel", Toast.LENGTH_LONG).show();
      break;
    }
  }

Let’s parse the syntax of the call. The static makeText method creates a Toast View element. Method parameters:

context – until we get into the details of what it is and use the current Activity, ie this.
text – the text to be displayed
duration – display duration (Toast.LENGTH_LONG is long, Toast.LENGTH_SHORT is short)

Toast is created and the method is called to display it show(). Save, run, check.

If you have an Android smartphone, I think you have seen similar messages. Now you know how it’s done)

In the next lesson:

– create menu items




Discuss in the forum [160 replies]

Leave a Comment