Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. package com.jtmnf.petapp;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuInflater;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.Spinner;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import androidx.annotation.Nullable;
  18. import androidx.appcompat.app.ActionBar;
  19. import androidx.appcompat.app.AppCompatActivity;
  20. import androidx.constraintlayout.widget.ConstraintLayout;
  21. import androidx.fragment.app.FragmentTransaction;
  22.  
  23. import com.google.android.material.snackbar.Snackbar;
  24.  
  25. public class MainActivity extends AppCompatActivity implements search.OnFragmentInteractionListener{
  26.  
  27. private ImageView imageView;
  28. private Button button;
  29. private TextView textView;
  30. private Spinner spinner;
  31.  
  32.  
  33. // Code for the new activity
  34. private final int REQ_CODE = 123;
  35.  
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_main);
  40.  
  41. // Init the various views in this activity
  42. imageView = findViewById(R.id.imageView);
  43. button = findViewById(R.id.button);
  44. textView = findViewById(R.id.textView);
  45. spinner = findViewById(R.id.spinner);
  46.  
  47. // Set a click listener for the button
  48. button.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. // Place the intent(ion) to go from this activity to the other.
  52. Intent intent = new Intent(MainActivity.this, ChangeName.class);
  53.  
  54. // Pass the selected item in the spinner
  55. intent.putExtra("selected", spinner.getSelectedItem().toString());
  56.  
  57. // Start the new activity waiting for the result REQ_CODE
  58. startActivityForResult(intent, REQ_CODE);
  59. }
  60. });
  61.  
  62. // Set a item selected listener to the spinner
  63. // Everytime it changes, it will enter here
  64. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  65. @Override
  66. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  67. switch (parent.getSelectedItem().toString()) {
  68. case "Snail":
  69. imageView.setImageResource(R.drawable.snail);
  70. textView.setText("This is my lovely Snail");
  71. break;
  72. case "Rhino":
  73. imageView.setImageResource(R.drawable.rhino);
  74. textView.setText("This is my lovely Rhino");
  75. break;
  76. case "Frog":
  77. imageView.setImageResource(R.drawable.frog);
  78. textView.setText("This is my lovely Frog");
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84.  
  85. @Override
  86. public void onNothingSelected(AdapterView<?> parent) {
  87.  
  88. }
  89. });
  90. }
  91.  
  92. @Override
  93. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  94. super.onActivityResult(requestCode, resultCode, data);
  95.  
  96. // If the returning activity has the code equals to our defined REQ_CODE
  97. if(requestCode == REQ_CODE){
  98.  
  99. // If everything went well
  100. if(resultCode == RESULT_OK){
  101.  
  102. // Define a new sentence for the textView, with the new name and owner
  103. String newOwner = data.getStringExtra("owner");
  104. String newName = data.getStringExtra("petName");
  105.  
  106. textView.setText("This is my pet " + newName + ", owned by " + newOwner + ".");
  107. }
  108. }
  109. }
  110.  
  111. @Override
  112. public boolean onCreateOptionsMenu(Menu menu) {
  113. // Inflate the menu; this adds items to the app bar if it is present.
  114. //getMenuInflater().inflate(R.menu.menu_my, menu);
  115. // Inflate the menu items for use in the app bar
  116. MenuInflater inflater = getMenuInflater();
  117. inflater.inflate(R.menu.menu_activity, menu);
  118.  
  119. //Our super action bar also has a new icon
  120. ActionBar ab = getSupportActionBar();
  121. //change the app icon in the app bar
  122. ab.setHomeAsUpIndicator(R.mipmap.ic_launcher_tool);
  123. //Show the icon - selecting "home" returns a single level
  124. ab.setDisplayHomeAsUpEnabled(true);
  125. ab.setTitle("Barra");
  126. return super.onCreateOptionsMenu(menu);
  127. }
  128.  
  129. @Override
  130. public boolean onOptionsItemSelected(MenuItem item) {
  131. switch (item.getItemId()){
  132. case R.id.app_bar_search:
  133. OpenSearch();
  134. return true;
  135. case R.id.app_settings:
  136. // Get the id and the name of the item selected
  137. String name = (item.getTitle() == null) ? "null" : item.getTitle().toString();
  138. // Show the id and the name in a short toast
  139. Toast.makeText(this, "Carregou em " + name + "!", Toast.LENGTH_SHORT).show();
  140. return true;
  141. case R.id.app_images:
  142. // Get the id and the name of the item selected
  143. String name1 = (item.getTitle() == null) ? "null" : item.getTitle().toString();
  144. ConstraintLayout layout = findViewById(R.id.main);
  145. Snackbar snack = Snackbar.make(layout, "Carregou em "+ name1 +"!",Snackbar.LENGTH_SHORT);
  146. snack.show();
  147. return true;
  148. default:
  149. return super.onOptionsItemSelected(item);
  150. }
  151. }
  152.  
  153. public void OpenSearch() {
  154. //Do something in response to button this, Search.class)
  155. search searchFragment = search.newInstance("lista"); //factory method !
  156. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  157. ft.replace(R.id.main, searchFragment);
  158. ft.addToBackStack("MAIN");
  159. ft.commit();
  160. }
  161.  
  162. @Override
  163. public void onFragmentInteraction(String text) {
  164.  
  165. }
  166.  
  167.  
  168. public void setName(String texto) {
  169. textView.setText(("This is "+texto+"!"));
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement