Advertisement
Baru_Berbagi

MainActivity.java

Jul 28th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package com.baruberbagi.sharetext;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.  
  13.     EditText editText;
  14.     Button btnShareText, btnSharePermanent;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.  
  21.         editText = findViewById(R.id.edtText);
  22.         btnShareText = findViewById(R.id.btnShareText);
  23.         btnSharePermanent = findViewById(R.id.btnSharePermanent);
  24.  
  25.         btnShareText.setOnClickListener(new View.OnClickListener() {
  26.             @Override
  27.             public void onClick(View v) {
  28.                 shareText("Harus di isi");
  29.             }
  30.         });
  31.  
  32.         btnSharePermanent.setOnClickListener(new View.OnClickListener() {
  33.             @Override
  34.             public void onClick(View v) {
  35.                 sharePermanent();
  36.             }
  37.         });
  38.     }
  39.  
  40.     private void shareText(String error){
  41.  
  42.         String text = editText.getText().toString();
  43.         if (text.equals("")){
  44.             editText.setError(error);
  45.         }else {
  46.             Intent intentShareText = new Intent(Intent.ACTION_SEND);
  47.             intentShareText.setType("text/plaint");
  48.             intentShareText.putExtra(Intent.EXTRA_TEXT,text);
  49.             startActivity(Intent.createChooser(intentShareText, "Bagikan ke: "));
  50.         }
  51.     }
  52.  
  53.     private void sharePermanent(){
  54.         Intent intentPermanent = new Intent(Intent.ACTION_SEND);
  55.         intentPermanent.setType("text/plaint");
  56.         intentPermanent.putExtra(Intent.EXTRA_TEXT, "ini adalah uji coba berbagi text");
  57.         startActivity(Intent.createChooser(intentPermanent, "Bagikan ke: "));
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement