Clear the previous view and show the new view of the click button in the same activity


I have created an activity with two buttons at the top. One button to show "SMS Logs" and second to show "Call Logs".

On clicking "SMS Logs" button, i am dynamically creating textviews and linear layout to show sms logs.

On Clicking "Call Logs", i am dynamically creating another textviews and linear layout to show call logs.

But the problem is that, once if we click "sms log" button and then we click "call log" button, the previously created linear layouts are not removed and the both(previous layouts and the current layouts) are shown simultaneously.

But i want that the previous layouts should be removed on clicking the second button.

Which function, should i use to remove the previous viewgroups or the layouts. Tell me if you need to read my class file.


This is my Activity's code,

public class General extends Activity
    String phone, message;
    TextView Logs;
    View layout, callLayout;
    TextView data, callData, line, callLine;
    Button smsLog, callLog;
    LinearLayout ll, callll;
    protected void onCreate(Bundle savedInstanceState)
        Logs = (TextView)findViewById(;
        layout = findViewById(;
        callLayout = findViewById(;
        smsLog = (Button)findViewById(;
        callLog = (Button)findViewById(;
        smsLog.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v)
                       ll = new LinearLayout(getApplicationContext());
                       data = new TextView(getApplicationContext());
                        data.setText("First Line");
                        line = new TextView(getApplicationContext());
                        line.setText("Second Line");
                        ((ViewGroup) ll).addView(data);
                        ((ViewGroup) layout).addView(line);
                        ((ViewGroup) layout).addView(ll);

        callLog.setOnClickListener(new View.OnClickListener()
            public void onClick(View v)
                        callll = new LinearLayout(getApplicationContext());
                         callData = new TextView(getApplicationContext());
                            callLine = new TextView(getApplicationContext());
                        callData.setText("Third Line");
                        callLine.setText("Fourth Line");
                        ((ViewGroup) callll).addView(callData);
                        ((ViewGroup) callLayout).addView(callLine);
                        ((ViewGroup) callLayout).addView(callll);

I have removed the extra code and made it simple to understand.

You could implement a TabView.

But having your current setup just change the visibility of one view group to GONE and the other to VISIBLE.

GONE will make the view invisible and it won't take up any space anymore.

EDIT based on the code added to the question

Both your layout and callLayout are using the same XML view. Implement 2 identical views in your xml and keep one visible and one gone. This way when you set layout or callLayout visibility to GONE they are 2 different ones not the same. So your onClick() will have something like this:

for smsLog:


for callLog: