Akash Shah
Akash Shah

Reputation: 132

Sending data to other activity and display some additional TextViews

I have an Android application in which i send data from one activity to another. I can do that part easily but the problem is i want to display some additional TextViews as well. So when i display the sent data using setContentView i can see only the sent data. I cannot see the additional TextViews. I want both of them to be displayed simultaneously.

I searched and tried hard to find a solution without any luck.I am attaching the code for reference.Thanks!

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop_information);
    Intent intent = getIntent();
    String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
    TextView textViewci= new TextView(this);
    textViewci.setText(message);
    TextView textView1= new TextView(this);
    TextView textView2= new TextView(this);
    textView1.setText("Major Districts");
    textView2.setText("Suitable Soil");

When i use this code,i am unable to see anything and the activity remains blank.Actually i am using a TableLayout here so that i can display data in form of tables

Upvotes: 0

Views: 801

Answers (5)

Akash Shah
Akash Shah

Reputation: 132

I have found out the answer by myself.It's fairly easy and there is no need here to use views in the parent layout.I modified the textViews in my xml file and got their values using findViewById.I am attaching the updated code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crop_information);
    Intent intent = getIntent();
    String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
    textView=(TextView)findViewById(R.id.textViewci);
    textView1=(TextView)findViewById(R.id.textViewmd);
    textView2=(TextView)findViewById(R.id.textViewso);
    textView.setText(message);
    textView1.setText("Major Districts");
    textView2.setText("Suitable Soil");
    tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
    textView.setTypeface(tf);
    }

Upvotes: 1

Poonam Anthony
Poonam Anthony

Reputation: 1988

are you getting any error messages? if yes then please post them here. and the others are right.you need to add yours views to the parent layout.

Upvotes: 1

vyas
vyas

Reputation: 412

I have just make a test program for you. first class code

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent intent = new Intent(MainActivity.this, SecondClass.class);
        intent.putExtra("firstValue", "1");
        intent.putExtra("secondValue", "2");
        startActivity(intent);


        }
    });

Second class code :

setContentView(R.layout.activity_crop_information);
        Intent intent = getIntent();
        String message1 = intent.getStringExtra("firstValue");
        String message2 = intent.getStringExtra("secondValue");

        RelativeLayout layout = new RelativeLayout(this);
        TextView textView1= new TextView(this);
        TextView textView2= new TextView(this);

        TextView textViewci= new TextView(this);
        textViewci.setText(message1+""+message2);
        Log.e("intent value is", ""+message1+""+message2);


        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
        lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());


        textView1.setText("Major Districts");
        textView2.setText("Suitable Soil");

        layout.addView(textView1);
        layout.addView(textView2);
        addContentView(layout, lp);

Try and let me know. YOu have to make related layouts for both classes.
Hope this will help

Upvotes: 1

vyas
vyas

Reputation: 412

You have to add the text view in the parentview. For example, if you have used the text view in the linear layout then add the text view in the linear layout and then use the addview(linearlayout).

Below is the example

RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);

TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());

layout.addView(tv1);       
layout.addView(tv2, lp);

Upvotes: 2

duggu
duggu

Reputation: 38409

You have to add text view on your layout.

View linearLayout =  findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);


TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(valueTV);

Upvotes: 2

Related Questions