How to handle onClick () in 100 buttons?

I have 100 buttons in the layout and OnClick() for all this.

If I use switch , I need to make case R.id.button1, ..., case R.id.button100 for all 100 buttons. How to shorten this code?

 public void webClick(View v) { switch(v.getId()) { case R.id.button1: Intent intent = new Intent(this, Webview.class); intent.putExtra("weblink","file:///android_asset/chapter/chapter1.html"); startActivity(intent); break; case R.id.button2: Intent intent2 = new Intent(this, Webview.class); intent2.putExtra("weblink","file:///android_asset/chapter/chapter2.html"); startActivity(intent2); break; // ... case R.id.button100: Intent intent100 = new Intent(this, Webview.class); intent100.putExtra("weblink","file:///android_asset/chapter/chapter100.html"); startActivity(intent100); break; } } 
+4
source share
6 answers

If the URL directly depends on the identifier, try the following:

 public void webClick(View v) { Intent intent = new Intent(this, Webview.class); intent.putExtra("weblink","file:///android_asset/chapter/chapter" + v.getId() + ".html"); startActivity(intent); } 

edited

If the URL does not depend directly on the identifier, try matching the identifiers of the buttons with the URLS, for example:

 Map<Integer, String> urls = new HashMap(); urls.put(R.id.button1, "file:///android_asset/chapter/chapter100.html"); // ... 1 to 100 ... 

and change the above code as follows:

 public void webClick(View v) { Intent intent = new Intent(this, Webview.class); intent.putExtra("weblink", urls.get(v.getId())); startActivity(intent); } 

EDITED # 2

If you already have a URL on the shortcut of your buttons, sugestion (not mine, but made by @pad) will use it to calculate the URL like this:

 public void webClick(View v) { Intent intent = new Intent(this, Webview.class); intent.putExtra("weblink", "file:///android_asset/chapter/chapter" + v.getText().replaceAll("Chapter ","") + ".html"); // Assuming your text is like "Chapter 50" startActivity(intent); } 
+5
source

For more buttons in your case, dynamically create a button in a loop and assign an onclick event, for example ..

Create one LinearLayout in your XML file.

Now add all the buttons in this LinearLayout, like ..

 String[] urls={url1,url2.....url100}; // here write your all URLs Button button[]= new Button[100]; LinearLayout mainlinear=(LinearLayout) findViewById(R.id.main_layer); for (int i = 0; i < 100; i++) { button[i] = new Button(this); button[i].setText(i); button[i].setTag(i+":"+URL); // URL = What you need to pass when button is click button[i].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView selected = (TextView) v; String tag = selected.getTag().toString(); //Here you need to write your code when button is pressed. Intent intent = new Intent(this, Webview.class); intent2.putExtra("weblink",urls[i]); startActivity(intent2); } } mainlinear.addView(button[i]); 
+2
source

I think this should work:

 public void webClick(View v) { String stringID = String.valueOf(v.getId()); String[] temp = stringID.split("utton"); stringID = "file:///android_asset/chapter/chapter" +temp[1]+ ".html" Intent intent = new Intent(this, Webview.class); intent.putExtra("weblink",stringID); startActivity(intent); } 
0
source

You might want to use ListView. If you want to have buttons, use a ListView that has one button in each layout of the item.

Then you configure onItemClickListener. You get the position of this button (the first is at position 0, the second is at position 1, etc.). Then create a link:

 String link= "file:///android_asset/chapter/chapter" + (position +1) + ".html"; 

The rest of your code will be the same ( intent.putExtra("weblink", link); )

0
source

My suggestion is to use ListView or GridView for this case and save the file names in an ArrayList. Then you do something like this:

 List<String> urlList = new ArrayList<String>(); urlList.add("file:///android_asset/chapter/chapter1.html"); urlList.add("file:///android_asset/chapter/chapter2.html"); // and so on ListView listView = (ListView)findViewById(R.id.listView); ArrayAdapter adapter = new ArrayAdapter(this, android.R.id.simple_list_item_1, urlList); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(this, Webview.class); intent.putExtra("weblink", urlList.get(position)); startActivity(intent); } }); 

This would be a good implementation because you would not use 100 buttons and OnClickListeners to save some resources.

Edit: this code should be running, just put it in onCreate () for some testing. All you have to do is define a ListView in your .xml layout by entering 100 of these buttons:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listView"> </ListView> </LinearLayout> 

If you really want to use this with buttons, than follow the advice of morgano.

0
source
 public void webClick(View v) { Intent intent = new Intent(this, Webview.class); switch(v.getId()) { case R.id.button1: intent.putExtra("weblink","file:///android_asset/chapter/chapter1.html"); startActivity(intent); break; case R.id.button2: intent.putExtra("weblink","file:///android_asset/chapter/chapter2.html"); startActivity(intent); break; case R.id.button3: intent.putExtra("weblink","file:///android_asset/chapter/chapter3.html"); startActivity(intent); break; . . . . case R.id.button100: intent.putExtra("weblink","file:///android_asset/chapter/chapter100.html"); startActivity(intent); break; default: break; } } 
-5
source

Source: https://habr.com/ru/post/1495298/


All Articles