skip to Main Content

Through out the whole android application I want to capture button click, radio button clicks, link click etc… basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?

4

Answers


  1. Try using onCickListener() on the buttons.

    Login or Signup to reply.
  2. In Kotlin:

    1. Add id to button in .xml files with android:id="@+id/nameOfButton". Every button needs an unique name.
    2. In .kt file, use setOnClickListener with the id to set up the action when user click the button.
    3. If there are several buttons, just follow step 1 and 2.

    Example:
    step 1

       <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />
    

    step 2

    buttonSave.setOnclickListenter {
        //TODO: your code goes here
    }
    
    Login or Signup to reply.
  3. Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
    XML:

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button"
      android:onclick="getid"
      android:text="Save" />
    

    JAVA:

    public void getid(View v){
      int id = v.getId();
      String text = v.getText();
    }
    
    Login or Signup to reply.
  4. As @Muthukumar Lenin asked here is listview in xml and java

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/rl"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:padding="10dp"
       tools:context=".MainActivity">
       <TextView
          android:id="@+id/textview"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textStyle="italic"
          android:textColor="#5f65ff"
          android:padding="5dp"
          android:text="Choose is the best football player?"/>
       <ListView
          android:id="@+id/listview"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/textview" />
    </RelativeLayout>
    
    

    JAVA:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    public class MainActivity extends AppCompatActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ListView listView = (ListView) findViewById(R.id.listview);
          final TextView textView = (TextView) findViewById(R.id.textview);
          String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
          List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
          ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
          listView.setAdapter(arrayAdapter);
          listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                textView.setText("The best football player is : " + selectedItem);
             }
          });
       }
    }
    

    Some of these code are from tutorialspoint and some are edited by me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search