0

I want to make a newsletter app with the feature to make posts yourself. I'm trying to makr a code for a button and I am stuck at the one moment. I don't know how to set a position for a new textView in the new cardView.

Here is a piece of code from MainActivity.java

package com.example.rame956.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ScrollView scrollView = new ScrollView(this);
    }

//<...>

public void CreateNewPost(View view){
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
        CardView card = new CardView(this);
        TextView text = new TextView(this);
        ImageView image = new ImageView(this);


    }

    }

Sorry if it's a dumb qustion. I'm new in android developing.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rame
  • 51
  • 8
  • This can guide you.. https://stackoverflow.com/questions/4203506/how-to-add-a-textview-to-a-linearlayout-dynamically-in-android – Jay Dec 27 '18 at 17:23

1 Answers1

0

For starters, I'd assume you would at least have a button inside your MainActivity which will trigger the process of creating a cardview. Therefore, to answer your question, I'll create a brand new card view programmatically from scratch. Take the elements which you need out of it i.e. textview, buttons and so on.

// Wiring in my 2 main aspects relativeLayout + Button
relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
button = (Button) findViewById(R.id.btn);

//my trigger but in your case, it can be anything
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CardView card = new CardView(mContext);

            // Set the CardView layoutParams
            LayoutParams layoutParams = new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT
            );
            card.setLayoutParams(layoutParams );

            // Setting different attributes
            card.setRadius(9);
            card.setContentPadding(15, 15, 15, 15);
            card.setCardBackgroundColor(Color.parseColor("#FFC6D6C3"));
            card.setMaxCardElevation(15);
            card.setCardElevation(9);

            // Initialize a new TextView to put in CardView
            TextView tv = new TextView(mContext);
            tv.setLayoutParams(layoutParams );
            tv.setText("My CardView");
            tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
            tv.setTextColor(Color.RED);

            // Put the TextView inside CardView
            card.addView(tv);

            // Finally, add the CardView in root layout
            relativeLayout.addView(card);
        }
    });

One thing to note here is the relativeLayout at the end of the function. You will need to have a parent layout in which you'll be placing your cardview. And of course, the attributes can be modified to your needs i.e. settext, backgroundcolour and so on.

If you simply want to insert a TextView to a pre-existing CardView in your xml. It'll be the same concept.

card = (CardView)findViewById(R.id.cardView);
//generate your textview as above....
card.addView(textView);
Nero
  • 1,058
  • 1
  • 9
  • 25