Showing posts with label Dialog. Show all posts
Showing posts with label Dialog. Show all posts

Saturday, February 23, 2019

Using popup

We can use Dialog to get data from user, and also with popup.
In folder drawable, create file name popup_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_above_anchor="true" android:drawable="@drawable/speech_background_top" />
    <item android:drawable="@drawable/speech_background_bottom" />
</selector>
Copy in to drawable 2 icon, name them speech_background_top, speech_background_bottom.


In folder layout, create file popup.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="This is a PopupWindow" />

    <EditText
        android:id="@+id/e"
        android:layout_width="230dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="close"
        android:text="Close" />

</LinearLayout>
File activity_main.xml sẽ như sau.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show PopupWindow"
        android:onClick="onShowWindowClick" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Show PopupWindow"
        android:onClick="onShowWindowClick" />
</FrameLayout>
Class main like this.
public class MainActivity extends Activity implements View.OnTouchListener {
     PopupWindow mOverlay;
     EditText e;
     @SuppressWarnings("deprecation")
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          View popupContent = getLayoutInflater().inflate(R.layout.popup, null);
          mOverlay = new PopupWindow();
mOverlay.setWindowLayoutMode(WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
mOverlay.setWidth(getResources().getDimensionPixelSize(
                     R.dimen.popupWidth));
mOverlay.setHeight(getResources().getDimensionPixelSize(
                     R.dimen.popupHeight));
mOverlay.setContentView(popupContent);
mOverlay.setBackgroundDrawable(getResources().getDrawable(
                     R.drawable.popup_background));
     mOverlay.setAnimationStyle(R.style.PopupAnimation);
     mOverlay.setTouchInterceptor(this);
     mOverlay.setFocusable(true);
     e = (EditText) popupContent.findViewById((R.id.e));
     }
public void close(View v) {
String nhap = e.getText().toString();
if (nhap == null || nhap.trim().equals("")) {
Toast.makeText(this, "You enter nothing", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "Enter " + nhap, Toast.LENGTH_SHORT).show();
e.setText("");
mOverlay.dismiss();
}
}
@Override
protected void onPause() {
     super.onPause();
// PopupWindow is like Dialog, it will leak if left visible while the Activity finishes
mOverlay.dismiss();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
     // Handle any direct touch events passed to the PopupWindow
return false;
}
public void onShowWindowClick(View v) {
          if (mOverlay.isShowing()) {
               mOverlay.dismiss();
          } else {

          mOverlay.showAsDropDown(v);
          }
     }
}

Popup now look like this, enter some text to see toast.

Friday, February 22, 2019

Show ProgressDialog to wait load

We need to read an txt file online, in wait time, we want to show Dialog Progress.
Declare a textView and a progressdialog to above Override.
TextView t;
ProgressDialog mProgressDialog;
In xml file create a textView.
<TextView
 android:id="@+id/te"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="15sp"
 android:text="@string/hello_world" />
Copy to below setContentView.
t = (TextView) findViewById(R.id.te);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading, please wait...");
mProgressDialog.show();
Create a thread.
new Thread() {
        @Override
        public void run() {
             String path = "http://www.oracle.com/technetwork/java/readme-2-149793.txt";
         
   URL u = null;
    try {
    u = new URL(path);
  HttpURLConnection c = (HttpURLConnection) u.openConnection();
   c.setRequestMethod("GET");
   c.connect();
   InputStream in = c.getInputStream();
 final ByteArrayOutputStream bo = new ByteArrayOutputStream();
   byte[] buffer = new byte[4096];
    in.read(buffer); // Read from Buffer.
   bo.write(buffer); // Write Into Buffer.
      runOnUiThread(new Runnable() {
                    @Override
                    public void run() {                                                                                           
                      String a=bo.toString();
                      t.setText(a);      
                        try {
                            bo.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
       if(mProgressDialog.isShowing())
        {
        mProgressDialog.dismiss();
        }
    }
}.start(); 
We need internet permission, copy to Androidmanifest.
<uses-permission android:name="android.permission.INTERNET" />
Run to see result.