Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching between 2 layouts in android activity

I have 2 different layout files that I want to use for modifying the same data, I was to switch to my layout for the "edit view" that allows the user to modify graph data and then allow them to switch back to a "detailed view" that displays a detailed graph(using androidplot library).

My issue is, when switching back to my "edit view" my graphed lines are gone and only the axises draw(so the layout switches and the onDraw() is being called for my graph View). Everything is stored within the same Activity so I don't understand why this isn't working?

The lines are stored within the Graph View object itself which should be persistent since it is a stored variable in my activity.

I use these two methods for switching layout files on a button click.

public class GraphLibActivity extends Activity {

    private Graph graph;

    private boolean editView;

    private static TextView coordTextView;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        editView = true;

        setContentView(R.layout.graphlib);



        graph = (Graph) findViewById(R.id.graph);

        coordTextView = (TextView)findViewById(R.id.currentCoords);


        (lots of calculations)
        graph.addLine(gelHistogramPoints, linePaint);



        graph.invalidate();      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        if(editView == true){
            inflater.inflate(R.menu.activity_menu, menu);
        }else{
            inflater.inflate(R.menu.detailed_view_menu, menu);
        }
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu){
        menu.clear();
        MenuInflater inflater = getMenuInflater();
        if(editView == true){
            inflater.inflate(R.menu.activity_menu, menu);
        }else{
            inflater.inflate(R.menu.detailed_view_menu, menu);
        }
        graph.invalidate();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {


        case R.id.detailed_view:
            editView = false;
            setContentView(R.layout.imagegraph);
            return true;


        case R.id.edit_view:
            editView = true;
            setContentView(R.layout.editgraph);             
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }

    }




}
like image 400
John Lotacs Avatar asked Aug 10 '11 20:08

John Lotacs


People also ask

How can I use two layout in one activity?

You can use something like: if (Case_A) setContentView(R. layout. layout1); else if (Case_B) setContentView(R.

How do I switch from one Android activity to another?

To create the second activity, follow these steps: In the Project window, right-click the app folder and select New > Activity > Empty Activity. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name. Leave all other properties set to their defaults and click Finish.


1 Answers

Each time you call setContentView you are inflating the layout so data must be set again. Are you doing that?

Anyway, I would recommend merging the two layouts into one file. Then use ViewFlipper to change from one layout to another. That would look something similar to:

graph.xml:

<ViewFlipper android:id="@+id/viewFlipper">
    <LinearLayout>
        // Here would go the content of R.layout.imagegraph
    </LinearLayout>
    <LinearLayout>
        // Here would go the content of R.layout.editgraph
    </LinearLayout>
</ViewFlipper>

Then you just have to call showNext() to switch layouts in your activity:

ViewFlipper vf = (ViewFlipper) findViewById( R.id.viewFlipper );
vf.showNext();

Hope it helps.

like image 182
Xavi Gil Avatar answered Oct 07 '22 07:10

Xavi Gil