1

I'm using Firebase RealTime database, and using retrieving data, I'm asking about if I want to add If method on OnClick() for the button and get the ActionView link from real time database, and if there's no link in the database give a toast message that "There's no link available ATM"

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.res_screen);

    String data0= getIntent().getStringExtra("Name");
    String data1= getIntent().getStringExtra("Address");
    String data2= getIntent().getStringExtra("Phone1");
    String data3= getIntent().getStringExtra("Phone2");
    String data4= getIntent().getStringExtra("Phone3");
    String data5= getIntent().getStringExtra("Offer");
    final String data6= getIntent().getStringExtra("Facebook");
    final String data7= getIntent().getStringExtra("Menu");
    final String data8 = getIntent().getStringExtra("Menu");

    TextView nm = findViewById(R.id.name);
    TextView ad = findViewById(R.id.address);
    TextView ph1 = findViewById(R.id.phone1);
    TextView ph2 = findViewById(R.id.phone2);
    TextView ph3 = findViewById(R.id.phone3);
    TextView off = findViewById(R.id.closeoffer);
    ImageView fb = findViewById(R.id.facebookbut);
    Button menu = findViewById(R.id.menubut);

    nm.setText(data0);
    ad.setText(data1);
    ph1.setText(data2);
    ph2.setText(data3);
    ph3.setText(data4);
    off.setText(data5);
    fb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
            startActivity(intent);
        }
    });
    menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("Menu")));
            startActivity(i);
        }
    });
}

Maybe am a little confuse because my bad language. I want to add if else into OnClick(). Mean.. if this button have link to visit ACTION_VIEW, then go. But if haven't link from realtime database, then give a Toast message "Sorry there's no link available at this moment"

1 Answers1

0

Maybe you need to do this :

fb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //data6 may be having link to fb ,here we check if it is not null 
           if(data6!=null )
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
            startActivity(intent);
        }else{
         //toast a message
          Toast.makeText(Yourclass.this, "Sorry there's no link available at this moment", Toast.LENGTH_SHORT).show();
         }
    });
Swati
  • 28,069
  • 4
  • 21
  • 41