tags:

views:

74

answers:

2

I have a linear layout which has 5 text views. Suppose user click on 3rd text view. I want to expand this 3rd text view on the entire screen i.e. i want to show this 3rd text view as a full screen in the same activity above the other text views. Is it possible in Android ?

+1  A: 

If you initially set the height of each text view to wrap_content as below:

<TextView  
   android:id="@+id/textview1"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="text1"
/>

Then attach a click handler that toggles the layout_height between wrap_content and fill_parent as below, you should achieve what you want.

final TextView tv1 = (TextView)findViewById(R.id.textview1);

    tv1.setOnClickListener(new OnClickListener(){           
        public void onClick(View arg0) {                
            if(tv1.getLayoutParams().height == LayoutParams.FILL_PARENT )
                tv1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            else
                tv1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));                  
        }           
    });

You could also play around with layout_weight if you want to space out your text views initially.

Gary Wright
Thanks for the reply, but this code does not do anything. In my program i have designed 5 text views which are under the linear layout in vertical orientation. I have set the above mentioned code with 3rd text view's onClickListner. But when 3rd text view is clicked it remains same, its not getting expanded and overlapping other text views.
Dalvin
Yes it worked, the problem was that my linear layout container had layout width and height as Wrap_Content. After changing it to Fill_Parent it worked. But now the problem is when i click on Text View it gets expanded only downwards i.e. it covers the space which is below Text View. But there are views which are above this text view. I want this view to be expanded in both upwards and downwards direction hence covering the entire screen.
Dalvin
A: 

Yeah, I see the issue you mean. See example below, I have tested it to make sure it works. It's probably not the most elegant solution, but here goes.

This time I have set the height to 0 and the weight to 1 for each text view. This makes each View take an equal slice of the height.

<TextView  
  android:id="@+id/textview1"
  android:layout_width="fill_parent" 
  android:layout_height="0px"
  android:layout_weight="1" 
  android:text="text1"
/>

In the activity code, I have the following instance declarations:

private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private TextView tv5;   

private boolean singleViewExpanded = false;

private OnClickListener myOnClickListener = new OnClickListener(){          
    public void onClick(View arg0) {        

        if(singleViewExpanded)                  
        {
            showAllViews();     
            singleViewExpanded = false;
        }
        else
        {
            // Hide all views apart from the clicked one
            hideAllViews();
            arg0.setVisibility(View.VISIBLE);
            singleViewExpanded = true;
        }
    }           
};  

singleViewExpanded lets you track whether you are showing all views or just one. The onClickListener is defined here so that we can set it for each of the TextViews.

These are the show and hide methods:

 private void showAllViews()
{
    tv1.setVisibility(View.VISIBLE);
    tv2.setVisibility(View.VISIBLE);
    tv3.setVisibility(View.VISIBLE);
    tv4.setVisibility(View.VISIBLE);
    tv5.setVisibility(View.VISIBLE);

}

private void hideAllViews()
{
    tv1.setVisibility(View.GONE);
    tv2.setVisibility(View.GONE);
    tv3.setVisibility(View.GONE);
    tv4.setVisibility(View.GONE);
    tv5.setVisibility(View.GONE);
}

And finally, wire up the onClick handlers inside onCreate method:

tv1 = (TextView)findViewById(R.id.textview1);
    tv2 = (TextView)findViewById(R.id.textview2);
    tv3 = (TextView)findViewById(R.id.textview3);
    tv4 = (TextView)findViewById(R.id.textview4);
    tv5 = (TextView)findViewById(R.id.textview5);

    tv1.setOnClickListener(myOnClickListener);
    tv2.setOnClickListener(myOnClickListener);
    tv3.setOnClickListener(myOnClickListener);
    tv4.setOnClickListener(myOnClickListener);
    tv5.setOnClickListener(myOnClickListener);
Gary Wright