Dalvinder Singh
Dalvinder Singh

Reputation: 2149

Expand Android View as a full screen

I have a linear layout which has five TextViews. Suppose a user clicks on the third TextView; I want to expand that TextView to the entire screen. In other words, I want to show the third TextView as full screen in the same activity above the other text views. How do I do this?

Upvotes: 8

Views: 17367

Answers (2)

Gary Wright
Gary Wright

Reputation: 2469

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);

Upvotes: 1

Gary Wright
Gary Wright

Reputation: 2469

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.

Upvotes: 3

Related Questions