It is required that when selecting the context menu for the selected city, a link to Glide is inserted (additional library for displaying hyphae). The context menu when a long tap is called, but then after selecting from the list it does not go.

Ideally, I would like the button to call up a list on the screen and select a city, but so far only the context menu could call up and make a list in it.

Here are my ideas:

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; public class MainActivity extends AppCompatActivity { private ImageView ImageView; private Button getRadar; private TextView city; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getRadar = (Button) findViewById(R.id.getRadar); ImageView = (ImageView) findViewById(R.id.ImageView); city = (TextView) findViewById(R.id.city); registerForContextMenu(city); } final int brest = 1; final int moskva = 2; final int minsk = 3; @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { switch (v.getId()) { case R.id.city: menu.add(0, brest, 0, "BREST"); menu.add(0, moskva, 0, "MOSKOW"); menu.add(0, minsk, 0, "MINSK"); break; } } public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case brest: Glide.with(this).load("http://site.ru/1.gif").asGif().into(ImageView); break; case moskva: Glide.with(this).load("http://site.ru/2.gif").asGif().into(ImageView); break; case minsk: Glide.with(this).load("http://site.ru/3.gif").asGif().into(ImageView); break; } return super.onContextItemSelected(item); } 
  • The context menu is not quite suitable for your idea and it’s better not to rush to learn something new for yourself, and then try to apply it. It’s better to use the list, and it’s up to you to decide .. either it will be a dialogue or a listview or recyclerview for example - ZigZag
  • If the picture is on the same screen as the choice of a city, then it is most appropriate to use Spinner as a selector. The names of class instances are usually written in small letters ImageView imageView; (in lowCamelCase style) / Resource names are also usually written with a small letter R.id.imageView - pavlofff
  • @pavlofff, I tried Spinner yesterday, yes, it’s convenient, but I didn’t manage to pass the link to the city in Glide. Could you give me even the simplest example of how this is implemented? - Pollux
  • You can not link the choice in the spinner with the image that you want to show? - pavlofff
  • @pavlofff, yes = ( - Pollux

1 answer 1

The code might look something like this:

 public class MainActivity extends AppCompatActivity { ImageView imageView; Spinner spinner; ArrayAdapter adapter; boolean isRun; final String[] city = {"Moscow", "Minsk", "Brest", "Selected city .."}; final String[] imageURL = {"http://site.ru/1.gif","http://site.ru/2.gif","http://site.ru/3.gif"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner)findViewById(R.id.spinner); imageView = (ImageView)findViewById(R.id.imageView); adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, city){ @Override public int getCount() { int count = super.getCount(); return count > 0 ? count - 1 : count; } }; adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setSelection(adapter.getCount()); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { if (isRun) Glide.with(view.getContext()).load(imageURL[position]).placeholder(R.drawable.placeholder_image).into(imageView); isRun = true; } public void onNothingSelected(AdapterView<?> parent) { } }); } } 

In the city array we place the names of cities, the corresponding images in the imageURL array. Since the image loading takes some time, it is best to place a local dummy image during the loading time, which you need to select yourself and place in the res / drawable / folder - in the example, a picture named placeholder_image .

UPD In order to show nothing at the start in the ImageView , enter the isRun flag, which is triggered after selecting a value in the spinner.
A small hack was also applied so that when you first started, it was not the name of the city that appeared in the spinner, but the offer to choose it. To do this, we add a text with a suggestion of choice to the end of the list of cities and slightly redefine the adapter so that this inscription does not appear in the drop-down list, and in the spinner at start we assign this entry to be displayed.

To work with the Internet, to upload a picture, you need to add a resolution in androidManifest.xml :

 <uses-permission android:name="android.permission.INTERNET"/> 
  • Thank you very much, the example works, everything is clear! Tell me please, is it possible that .placeholder not displayed when launching the application? But it turns out that nothing has been selected yet, and loading on the imageView already displayed. - Pollux
  • @Pollux Completed the answer - pavlofff
  • and once again many thanks to you !!! It would help, he would not have mastered. Can you, as a specialist, advise any sensible resource / lessons for learning java to a beginner? - Pollux
  • @Pollux For education, I have some such recommendations (now all the books mentioned there have more recent editions). As for the implementation of algorithms directly, it is more difficult to learn, here you need the ability to think logically and the ability to explain to the “machine” what you want from it. The best practice, after studying the theory (having a strong knowledge of Java and understanding the capabilities of the Android framework), is to study someone else's code and figure out what exactly is happening there, on the same Github, in abundance. - pavlofff
  • I would not recommend Internet resources for learning, since, unlike books, they do not provide a complete picture of the subject, but, as a rule, they follow a simplified example focused directly on it. Then there is such a problem that the pieces are clear, and how it works with each other is not at all clear. Generally on Internet resources (as an additional reading and a more detailed analysis of specific narrow topics) - the lessons of Klimov and startandroid.ru - pavlofff