Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

Clear previous View and show new View on button click 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.

Edit:

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;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.general_main);
        Logs = (TextView)findViewById(R.id.Logs);
        layout = findViewById(R.id.layout);
        callLayout = findViewById(R.id.layout);
        smsLog = (Button)findViewById(R.id.smsLogs);
        callLog = (Button)findViewById(R.id.callLogs);
        smsLog.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) 
            {
                callLayout.setVisibility(View.GONE);
                       ll = new LinearLayout(getApplicationContext());
                       ll.setOrientation(LinearLayout.VERTICAL);
                       data = new TextView(getApplicationContext());
                        data.setText("First Line");
                        data.setTextColor(Color.YELLOW);
                        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) 
            {
                layout.setVisibility(View.GONE);
                        callll = new LinearLayout(getApplicationContext());
                        callll.setOrientation(LinearLayout.VERTICAL);
                         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.

Upvotes: 0

Views: 1273

Answers (2)

slybloty
slybloty

Reputation: 6506

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:

layout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);

for callLog:

callLayout.setVisibility(View.GONE);
callLayout.setVisibility(View.VISIBLE);

Upvotes: 0

Pratik
Pratik

Reputation: 831

You can use FrameLayout to solve your problem.
But I recommend you to use tabview.
Here is the link that demonstrates how to develop tabbed applications.
Good Luck

Upvotes: 1

Related Questions