arjun narahari
arjun narahari

Reputation: 176

how to change the header dynamically in android by clicking on any view like textview or button?

i need to change my text in the header when i click on the textview , i made my best effort to do somethings, but i m not getting the result i will post the code and if anyone can help me out and tell me where i m going wrong , it will be of great help

this is my results.xml where my listview and the header with textview(the one which is to be set inside the java code) is there ,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White" >

<RelativeLayout
    android:id="@+id/rltHeader"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#27acc3" >

    <ImageView
        android:id="@+id/imgBackReuslt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:onClick="onClick"
        android:src="@drawable/arrow" />

    <TextView
        android:id="@+id/txtHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textColor="@color/White"
        android:textSize="20dp"
        android:textStyle="bold" />
 </RelativeLayout> 

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/rltHeader"
    android:layout_marginRight="5dp"
    android:divider="@color/LightGray"
    android:dividerHeight="4px" >
</ListView>

</RelativeLayout>

this is my another xml where i have displayed how the listviews every Row will appear

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >



    <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="100dp">

    <RelativeLayout
        android:id="@+id/rltRow"
        android:layout_width="17dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/r_list" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="83dp"
        android:layout_height="80dp"
        android:layout_alignBottom="@+id/rltRow"
        android:layout_alignParentRight="true" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="22dp"
        android:layout_toRightOf="@+id/rltRow"
        android:text="TextView"
        android:textColor="@color/Black"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/textView2"
        android:text="TextView"
        android:textColor="@color/Black" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/rltRow"
        android:layout_alignLeft="@+id/textView2"
        android:text="TextView"
        android:textColor="@color/Black" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignTop="@+id/imageView1"
        android:text="TextView"
        android:textColor="@color/Black"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:text="TextView"
        android:textColor="@color/Black"
        android:textSize="12dp" />
    </RelativeLayout>

    </RelativeLayout>

Now the Java part This is the List_Activity class where i have displayed all the textviews , there are 6 textviews which when clicked , an activity will appear Results.java , for all the textviews when clicked the same activity Results.java is opened.

package com.demo.Test;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class List_Activity extends Activity {

ImageView iv;
TextView t1, t2, t3, t4, t5, t6;
String header_result = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
  //TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_info);
    iv = (ImageView)findViewById(R.id.imgBackReuslt);
    t1 = (TextView) findViewById(R.id.text1);
    t2 = (TextView) findViewById(R.id.text2);
    t3 = (TextView) findViewById(R.id.text3);
    t4 = (TextView) findViewById(R.id.text4);
    t5 = (TextView) findViewById(R.id.text5);
    t6 = (TextView) findViewById(R.id.text6);

    t1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // String str = headervalue.getText().toString();

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Self", header_result);

            startActivity(intent);

        }
    });
    t2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Mary(Wife)", header_result);
            startActivity(intent);

        }
    });
    t3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // name = t3.getText().toString();
            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Alex(Child-1)", header_result);
            startActivity(intent);

        }
    });
    t4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Steven(Child-2)", header_result);
            startActivity(intent);

        }
    });
    t5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Robert(Father)", header_result);
            startActivity(intent);

        }
    });
    t6.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            intent.putExtra("Results for Diana(Mother)", header_result);
            startActivity(intent);

        }
    });

}

public void onClick(View v) {
    super.onBackPressed();
    finish();
}

}

Now the main class Results.java , the activity which opens when clicked on the textviews described the previous class

package com.demo.Test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Results extends Activity{
TextView header;
ImageView iv;
ListView lv;
String title;

int[] images = { R.drawable.photo_bg, R.drawable.photo_bg,
        R.drawable.photo_bg, R.drawable.photo_bg, R.drawable.photo_bg,
        R.drawable.b_list, R.drawable.g_list, R.drawable.r_list,
        R.drawable.v_list };

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);
    iv = (ImageView) findViewById(R.id.imgBackReuslt);
    header = (TextView) findViewById(R.id.txtHeader);

    lv = (ListView) findViewById(R.id.listView1);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        title = extras.getString("header_result");
    }
    ResultsAdapter adapter = new ResultsAdapter(getBaseContext(), doc,
            diag, dt, images, docname, diagname);
    lv.setAdapter(adapter);
}

so what changes i have to make so that the header text gets changed when i click on the textviews in the List_Activity accordingly any suggestions are welcomed

Upvotes: 0

Views: 1166

Answers (2)

Mina Fawzy
Mina Fawzy

Reputation: 21452

according to what you want

    t2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent intent = new Intent("com.demo.Test.RESULTS");
            // this header_result you want pass it textview (txtHeader)
            // prefer don't write long description in key of your extra 
            intent.putExtra("Results for Mary(Wife)", header_result);
            startActivity(intent);

        }
    });

put in your jave file that contain

 header = (TextView) findViewById(R.id.txtHeader);
header.setText("YourPassingValue"); // in your case getIntent().getStringExtra("Results for Mary(Wife)");

Refine your code

if you want move from activity yo anther

Intent intent = new Intent (YourFirstActivity.this , SecondActivity.class);
intent.putExtra("Result", PassingVariable); // prefer to make the key of your extra short to make east to call back , PassingVariable is what ever you want pass to the anther activity 
startActivity(intent);

in Anther activity get you extra data

in your on create method

String PassedData = getIntent().getStringExtra("Result"); // Result is Your used key

make any thing with passed data

in Your case you want set it on Textview

t2.setText(PassedData);

Upvotes: 0

Luis Pereira
Luis Pereira

Reputation: 1521

Do you mean changing the title of your action bar in each activity?

you can do it by calling this short code

getActionBar().setTitle("the title you want");

you can check this link http://developer.android.com/guide/topics/ui/actionbar.html

Hope it helps, and tell me if you need anything else ;)

Upvotes: 1

Related Questions