renupok92
renupok92

Reputation: 98

Linking two XML layouts in one java file

I am creating a flashlight app...I want to link two layouts in one java file...one layout is when the torch is unlit...and the other layout shows an illuminating torch...I have worked on it and now when i press the button it shifts to the other layout..(the illuminating one)...but does not come back to the first one when i click the button...What to do? Here is the code..

My XML1:

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/fl"
tools:context=".Flash" >

<Button
    android:id="@+id/ib2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/bu" />

My XML2:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fl1"
android:orientation="vertical" >

<Button
    android:id="@id/ib2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/bu" />

My Flashlight.java file (excluding imports):

    package com.potapptoes.flashlight;
    public class Flash extends Activity implements OnClickListener {
Camera cam = null;
Button ib1;
Parameters para;
PowerManager pm;
WakeLock wl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    wl.acquire();
    initialize();
    ib1.setOnClickListener(this);
}

private void initialize() {
    // TODO Auto-generated method stub
    ib1 = (Button) findViewById(R.id.ib2);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (cam == null) {
        setContentView(R.layout.main2);
        cam = Camera.open();
        para = cam.getParameters();
        para.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(para);
    } else {
                    setContentView(R.layout.main);
        para.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.setParameters(para);
        cam.release();
        cam = null;
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    para.setFlashMode(Parameters.FLASH_MODE_OFF);
    cam.setParameters(para);
    cam.release();
    cam = null;
    wl.release();
    finish();
}
    }

Upvotes: 0

Views: 4537

Answers (3)

Фред Генкин
Фред Генкин

Reputation: 163

Why do not you just change background image on button Click?

  1. Create onClickListener for your button.
  2. Create boolean variable which would trigger the flashlight on and off on Click of a button.
  3. Change background after you check is the flashlight already "triggered" or not.

Hope I understood your question correct, and you will find this helpful.

Upvotes: 0

d4n13l
d4n13l

Reputation: 126

Check out ViewSwitcher.

Here is an example.

Upvotes: 2

Carnal
Carnal

Reputation: 22064

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (cam == null) {
        setContentView(R.layout.main2);
        cam = Camera.open();
        para = cam.getParameters();
        para.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(para);
    } else {
        setContentView(R.layout.main); // you forgot this line
        para.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.setParameters(para);
        cam.release();
        cam = null;
    }
}

Upvotes: 0

Related Questions