Question

I would like to create a imageView and a textView in android that appears only when the user has clicked in a button from another activity and have another button that appears when the user has clicked in another button from the same activity .

I implemented it programmatically using a relative layout :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* Adding the icon and text */
        // Creating a new RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters.
        // In this case I want to fill its parent
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);


        // Creating the icon
        ImageView icon = new ImageView(this);
        icon.setImageResource(R.drawable.add_16);

        // Defining the layout parameters of the ImageView
        RelativeLayout.LayoutParams iconlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        iconlp.addRule(RelativeLayout.CENTER_IN_PARENT);
        icon.setClickable(true);

        // Setting the parameters on the ImageView
        icon.setLayoutParams(iconlp);

        // Adding the ImageView to the RelativeLayout as a child
        relativeLayout.addView(icon);


        // Creating the textView
        TextView add = new TextView(this);
        add.setText("Add");

        // Defining the layout parameters of the TextView
        RelativeLayout.LayoutParams addlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        addlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        addlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
         add.setClickable(true);

        // Setting the parameters on the TextView
        addCard.setLayoutParams(addlp);

        // Adding the TextView to the RelativeLayout as a child
        relativeLayout.addView(add);

The problem is that the icon appears in the middle of the screen and the text "Add" on its bottom, while I would like to have it just below the icon... How can I do that ?

Was it helpful?

Solution

What about something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Adding the icon and text */
    // Creating a new RelativeLayout
    final RelativeLayout relativeLayout = new RelativeLayout(this);

    // Defining the RelativeLayout layout parameters.
    // In this case I want to fill its parent
    final RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    rlp.addRule(RelativeLayout.CENTER_IN_PARENT);

    final TextView view = new TextView(this);
    view.setText("Add");
    view.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.add_16, 0, 0);
    view.setGravity(Gravity.CENTER);

    relativeLayout.addView(view, rlp);
    setContentView(relativeLayout);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top