title: Linking TextViews to Buttons Android
body: <p>So I have this output: <a href="http://imgur.com/MM6QwgQ" rel="nofollow">http://imgur.com/MM6QwgQ</a></p>
  
  <p>Which is represented by a ToggleButton and a TextView on top. Overall I have 18 buttons and 18 textViews. What I am trying to do is somehow link each TextView to a Button, in a way that whenever I press a button, make it possible to get the Text from the textView, so I can display it in another activity. The way this works is like a booking. It's just picking an hour.</p>
  
  <p>In order to generate the TextView and the Button, I'm using an adapter: </p>
  
  <pre><code>public class TimeActivity extends Activity  {
  
      private GridView gridView;
      private final String[] items = new String[] {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
      private final String[] hours = new String[] {"06:00", "12:00", "18:00", "07:00", "13:00", "19:00", "08:00", "14:00", "20:00", "09:00", "15:00", "21:00", "10:00", "16:00", "22:00", "11:00", "17:00", "23:00"};
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_time);
          setFonts();
  
          gridView = (GridView) this.findViewById(R.id.timeInputView);
          CustomGridAdapter adapter = new CustomGridAdapter(TimeActivity.this, items, hours);
          gridView.setAdapter(adapter);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.time, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      private void setFonts() {
          Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Medium.ttf");
          TextView dateOutput = (TextView) findViewById(R.id.dateOutput);
          dateOutput.setTypeface(tf);
      }
  }
  </code></pre>
  
  <p>The adapter:</p>
  
  <pre><code>public class CustomGridAdapter extends BaseAdapter {
  
      private Context _context;
      private String[] _items;
      private String[] _hours;
      LayoutInflater _inflater;
  
      public CustomGridAdapter(Context context, String[] items, String[] hours) {
          this._context = context;
          this._items = items;
          this._hours = hours;
          _inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public int getCount() {
          return _items.length;
      }
  
      @Override
      public Object getItem(int position) {
          return _items[position];
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              convertView = _inflater.inflate(R.layout.screen_gridcell_time, null);
          }
          Button button = (Button) convertView.findViewById(R.id.calendar_day_gridcell);
          button.setText(_items[position]);
  
          TextView tv = (TextView) convertView.findViewById(R.id.top_time);
          tv.setText(_hours[position]);
  
          return convertView;
      }
  }
  </code></pre>
  
  <p>And the layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:background="@android:color/transparent"&gt;
  
      &lt;TextView
          android:id="@+id/top_time"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:textSize="8dp"
          android:textColor="@color/toggle_color"/&gt;
  
      &lt;ToggleButton
          android:id="@+id/calendar_day_gridcell"
          android:layout_width="100dp"
          android:layout_height="40dp"
          android:layout_gravity="center"
          android:layout_below="@id/top_time"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/wampWhite"
          android:background="@drawable/time_input_button_selector"
          android:layout_marginLeft="2dp"
          android:textOff=""
          android:textOn="\u2713"
          /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>