1

I have an XML layout with an EditText on top and then a lot of buttons and a bunch of other views after the keyboard. The root element is ConstraintLayout.

<ConstraintLayout ...>
    <EditText .../>

    <!-- Here is the list of buttons -->
    <!-- For placement constraints purposes, I may have id's in the buttons -->
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"/>
    ... <!-- About 26 of them -->
    <Button .../>

</ConstraintLayout>

Hard-coding the buttons ids and the findViewById(resId) will leave a lot of boilerplates and doesn't seem to be the best way.

How do I get this much of buttons in an array of Button[] in the ActivityName.java? What would be the best way to do that?


EDIT:

Below is a single actual button which I'm am using in my XML. I think it will be hard to programmatically add the button to the ConstraintLayout. If it is possible, please share the way of doing it programmatically.

<Button
        android:id="@+id/keyboard_button_Q"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:minWidth="0dp"
        style="?android:attr/buttonBarButtonStyle"
        android:padding="0dp"
        android:text="Q"
        app:layout_constraintEnd_toStartOf="@id/keyboard_button_W"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
Rahat Zaman
  • 1,322
  • 14
  • 32
  • 1
    You could just create all the buttons programmatically instead of through xml. [Add button to a layout programmatically](https://stackoverflow.com/questions/4907609/add-button-to-a-layout-programmatically) – takendarkk Oct 25 '17 at 21:51
  • Edited the description after getting a lot of response about adding the buttons programmatically. It will be much more complex to add 26 buttons programmatically and making a `constraint Chain`. – Rahat Zaman Oct 25 '17 at 22:23

3 Answers3

1

Create a simple button.xml file which contains only one button. Then build your buttons runtime how many times you want. That's why the LayoutInflater class.

Button button = (Button) LayoutInflater.from(this).inflate(R.layout.button, null); 
Vahe Gharibyan
  • 5,277
  • 4
  • 36
  • 47
  • I think this is going outside the context of the topic, but I need to make those buttons a `ConstraintLayout Chain`. How do I do that if I add those buttons programmatically using the `LayoutInflater`? I need to reference to other buttons which will be added programmatically after the first button, and also I need to use the id's of those to add those layoutParams. Please consider looking at the Button xml code to know what I really want to do. – Rahat Zaman Oct 25 '17 at 22:27
  • @Rahat maybe i don't understand correctly, but you can get context from a parent view (ConstraintLayout) . LayoutInflater.from(rootView.getContext()) – Vahe Gharibyan Oct 25 '17 at 22:57
  • The word context doesn't mean the `Context` of the app, I was just saying that this may be out of the topic of my question. What I meant was that how do I create a `Constraint Chain` with the buttons I created in my code? – Rahat Zaman Oct 25 '17 at 23:02
  • 1
    Any time if you call inflate(), it's create a new view object. Pass your view objects in array, or in Entry with key and value , but it's bad practice, because it can be cause of memery leak. I suggest to use WeakReferences> for keeping your views. Than find by key. – Vahe Gharibyan Oct 25 '17 at 23:09
1

You can associate a number with a view by using android:tag:

android:tag

Supply a tag for this view containing a String, to be retrieved later with View.getTag() or searched for with View.findViewWithTag(). It is generally preferable to use IDs (through the android:id attribute) instead of tags because they are faster and allow for compile-time type checking.

May be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character;

With this, you can memorize all the button in your layout:

<Button 
    android:layout_width="wrap_content"
    android:tag="0"
    android:layout_height="wrap_content"
    android:text="A"/>

<Button 
    android:layout_width="wrap_content"
    android:tag="1"
    android:layout_height="wrap_content"
    android:text="B"/>

 ...

Then, you can get all the button with findViewWithTag() and a for loop:

Button[] buttons = new Button[26];

for(int i = 0; i < 26; i++) {
  buttons[i] =  findViewByTag(String.valueOf(i));
}

For the click listener, you can do something by checking for the tag with getTag():

buttonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      int tag = Integer.parseint(v.getTag());

      // do something based on the tag.
    }
};
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

One thing you could take a look at is Butter knife library for Android. You just have to annotate fields with @BindView and a view ID and Butter knife will automatically cast the corresponding view in your layout. In my opinion this is a good approach because you can still have a clear structure in your layout XML file.

Here is an example on how you could use it in your app

 @BindView(R.id.title) TextView title;
 @BindView(R.id.subtitle) TextView subtitle;
 @BindView(R.id.footer) TextView footer;

To include Butter knife in your project, you just have to add the dependencies to your app gradle file.

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

For more information and a detailed installation and usage guide you can check their website: http://jakewharton.github.io/butterknife/

aarnaut
  • 507
  • 6
  • 17
  • Thanks a lot for the quick response. I have used `ButterKnife` and found it very useful. But that still doesn't make my code feel robust ( I still need to write 26 lines of code just to make a single button array). I was expecting something like a loop which will loop 26 times and make the array for me. – Rahat Zaman Oct 25 '17 at 22:06
  • 1
    You could still create the buttons programmatically and add them to the view. Would this be an option for you? – aarnaut Oct 25 '17 at 22:07
  • @RahatZaman then you need to use Android Data Binding Library as I mentioned before. – Max Oct 25 '17 at 22:08
  • @AhmedArnaut Yes it is a choice. But It seems a bit hard to add the buttons programmatically in the layout and adjust the design, placement and other stuff (As I cannot visualize them when doing programmatically) – Rahat Zaman Oct 25 '17 at 22:10