Insert button dynamically in Relativelayout

Asked

Viewed 285 times

0

I have a Listview and I want to add a button next to each item in the list, but it has to be dynamically:

item_listview.xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relativelayout">

<ImageButton
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="@color/vermelho"
        android:src="@drawable/lixobotao"
        android:id="@+id/btnexcluir"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/btnfavorito"
        android:visibility="invisible"/>

    <ImageButton
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="@color/amarelo"
        android:src="@drawable/estrelabotao"
        android:id="@+id/btnfavorito"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:visibility="invisible"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/primary_text"
        android:id="@+id/texto1"
        android:textSize="20dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/secondary_text"
        android:id="@+id/texto2"
        android:textSize="15dp"
        android:layout_below="@+id/texto1"
        android:layout_alignParentStart="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/secondary_text"
        android:id="@+id/texto3"
        android:textSize="15dp"
        android:layout_below="@+id/texto2"
        android:layout_alignParentStart="true" />

</RelativeLayout>

xml list.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:weightSum="1"
    android:gravity="center">

    <ListView
        android:id="@+id/lisview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#FFECECEC"
        android:dividerHeight="2sp"
        android:layout_alignParentEnd="true"
        android:layout_above="@+id/btn">
    </ListView>

    <Button
        android:layout_width="130dp"
        android:layout_height="30dp"
        android:text="SALVAR"
        android:id="@+id/btn"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

How do I make the button next to each item? I want it to look like this green button in the image:

inserir a descrição da imagem aqui

I tried to make it invisible like they suggested and it didn’t work:

@Override
    protected void onCreate(Bundle savedInstanceStade){
        super.onCreate(savedInstanceStade);
        setContentView(R.layout.listas);

    ListView list = (ListView) findViewById(R.id.list);


        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                View v;
                v = inflater.inflate(R.layout.lista_listas,null);
                ImageButton favorito = (ImageButton) v.findViewById(R.id.btnfavorito);
                ImageButton excluir = (ImageButton) v.findViewById(R.id.btnexcluir);
                favorito.setBackgroundColor(getResources().getColor(R.color.primary));
                favorito.setVisibility(View.VISIBLE);
                excluir.setVisibility(View.VISIBLE);
                }
            }
        });
  • 2

    The ideal is you put it in the layout normally and set as invisible, and then via code you appear with it or not.

  • I tried, but I can’t seem to make the button visible

  • Is your click function working? to test have it display a Toast, and if it works add in the xml of the button visibility = GONE, so it won’t take up space, and then under Toast, try to set the visibility = VISIBLE. also check if you have the right id, do tests, because you may not be able to find the button.

  • I already did the tests and I debugged. Apparently it was supposed to be working

  • It didn’t work because you are inflating the layout at click time, and not using the View corresponding to your item. Remove the first two lines you use to inflate the layout, and replace the other lines where you are v for view, for the onItemClickit’s all right with you View corresponding to the item you clicked

1 answer

0

Answering your question of how to create a Button in the RelativeLayout, it’s simple, here’s an example of how to do it dynamically:

Button btTeste = new Button(this);
btTeste.setText("Teste");

RelativeLayout rl = (RelativeLayout ) findViewById(R.id.seu_relative_layout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
rl.addView(btTeste, lp);

As for your other question regarding button visibility, I left a comment responding:

It didn’t work because you’re inflating the layout at click time, and not using the View corresponding to your item. Remove both first lines you use to inflate the layout, and replace the other lines where is v for view, for the onItemClick It’s all right View corresponding to the item you clicked

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    ImageButton favorito = (ImageButton) view.findViewById(R.id.btnfavorito);
    ImageButton excluir = (ImageButton) view.findViewById(R.id.btnexcluir);
    favorito.setBackgroundColor(getResources().getColor(R.color.primary));
    favorito.setVisibility(View.VISIBLE);
    excluir.setVisibility(View.VISIBLE);
}
  • What if I didn’t have the view? Because I will have to do this within a Swipelistview and in this event the View is not passed as a parameter. If I inflated before the event would work?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.