Insert button dynamically in RelativeLayout

0

I have a ListView and I want to add a button on the side of each list item, 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>

lista.xml

<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 stay next to each item? I want it to appear just like this green button in the image:

I tried to make it invisible as they suggested and it did not 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);
                }
            }
        });
    
asked by anonymous 30.06.2016 / 14:46

1 answer

0
Answering your question about how to create a Button in RelativeLayout is simple, here is an example of how to do this 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 answering:

  

It did not work because you are inflating the layout at the time of the click, and   not using the View corresponding to your item. Remove both   the first few lines you use to inflate the layout, and replace the   other lines where v is view , since onItemClick already gives you 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);
}
    
30.06.2016 / 18:33