user709589
user709589

Reputation:

Switching the layouts through Intents

I have two layouts(xml files) and I want to flip from one page to another, the two xml files are main.xml and register.xml, if I click signin button in main.xml the page should turn and show register.xml and also in register.xml if I click the submit button it should turn to main.xml I tried a lot with the activity and i coldnt do it as I am new to android

please someone share the code for me,

here is my two xml codes

main.xml

<LinearLayout
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:background="@drawable/loginapp">

<RelativeLayout android:id="@+id/relativeLayout1" android:gravity="center" android:layout_marginTop="25dip" android:layout_height="177dip" android:background="@drawable/login_form_bg_green" android:layout_width="296dip">
 <EditText android:layout_marginRight="0dip"  android:id="@+id/userNameBox" android:layout_width="200px" android:background="@android:drawable/editbox_background" android:maxLines="1" android:layout_marginLeft="85dip" android:inputType="text" android:layout_height="wrap_content"></EditText>
 <EditText android:layout_marginRight="0dip"  android:id="@+id/passwordBox" android:layout_width="200px" android:background="@android:drawable/editbox_background" android:maxLines="1" android:layout_marginTop="45dip" android:layout_marginLeft="85dip" android:inputType="text|textVisiblePassword" android:layout_height="wrap_content"></EditText>
</RelativeLayout>

<LinearLayout android:id="@+id/ll_three" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/ll_two" android:gravity="center">
  <Button 
    android:text="Sign In"
    android:id="@+id/Button01"
    android:layout_width="wrap_content" 
    android:layout_marginTop="5dip"
    android:layout_marginRight="15dip"
    android:layout_height="wrap_content"/>
<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_marginTop="5dip"
    android:id="@+id/Button02"
    android:text="New user"/>

register.xml is

<TableRow>
 <TextView android:id="@+id/TextView"   
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" REGISTER:"/>    
</TableRow>

<TableRow>
    <TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name:"/>
<EditText android:layout_marginRight="0dip"
  android:id="@+id/userNameBox" android:background="@android:drawable/editbox_background"
    android:maxLines="1" android:layout_marginLeft="15dip"   android:layout_weight="1"
    android:inputType="text" android:layout_height="35px" android:layout_width="0dip"></EditText>


</TableRow>

<TableRow>
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name:"/>
<EditText android:layout_marginRight="0dip"
  android:id="@+id/userNameBox" android:layout_width="200px"  android:layout_weight="1" 
  android:background="@android:drawable/editbox_background" android:maxLines="1"
   android:layout_marginLeft="15dip" android:inputType="text" 
   android:layout_height="35px"></EditText>
</TableRow>


<TableRow>
    <TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"/>
<EditText 
 android:id="@+id/userNameBox" android:layout_width="200px"
  android:background="@android:drawable/editbox_background"  android:layout_weight="1"
   android:maxLines="1" android:layout_marginLeft="15dip" android:layout_marginRight="0dip"  
   android:inputType="text" android:layout_height="35px">
   </EditText>
</TableRow>

<TableRow>
 <TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No:"/>
<EditText android:layout_marginRight="0dip" 
 android:id="@+id/userNameBox" android:layout_width="200px"   android:layout_weight="1"
  android:background="@android:drawable/editbox_background"
   android:maxLines="1" android:layout_marginLeft="15dip"
    android:inputType="text" android:layout_height="35px">
    </EditText>

</TableRow>   

HERE ARE MY JAVA FILES

login.java

package com.android;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Login extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button newuser = (Button) findViewById(R.id.Button02);
        newuser.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Register.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }
}




Register.java


package com.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Register extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        Button register = (Button) findViewById(R.id.Button03);
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }}

while debugging the project these are the DDMS errors I found

Android [Android Application]
DalvikVM[localhost:8759]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626 NativeStart.main(String[]) line: not available [native method]
Thread [<6> Binder Thread #2] (Running) Thread [<5> Binder Thread #1] (Running

LOGCAT showing this

04-24 16:24:18.965: ERROR/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3 04-24 16:24:20.415: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{44f2b188 com.android.launcher/com.android.launcher2.Launcher} 04-24 16:24:22.965: WARN/WindowManager(58): App freeze timeout expired. 04-24 16:24:22.965: WARN/WindowManager(58): Force clearing freeze: AppWindowToken{450698d0 token=HistoryRecord{44f2b188 com.android.launcher/com.android.launcher2.Launcher}} 04-24 16:24:23.645: WARN/GoogleLoginService(180): Device has no accounts: sending Intent { act=com.google.android.gsf.LOGIN_ACCOUNTS_MISSING } 04-24 16:24:31.296: WARN/PackageManager(58): Code path for pkg : com.android.cardioworld changing from /data/app/com.android.cardioworld-1.apk to /data/app/com.android.cardioworld-2.apk 04-24 16:24:31.296: WARN/PackageManager(58): Resource path for pkg : com.android.cardioworld changing from /data/app/com.android.cardioworld-1.apk to /data/app/com.android.cardioworld-2.apk 04-24 16:24:34.216: WARN/RecognitionManagerService(58): no available voice recognition services found 04-24 16:24:36.214: WARN/SystemClock(123): Unable to set rtc to 1303642476: Invalid argument 04-24 16:24:41.174: WARN/ActivityThread(318): Application com.android.cardioworld is waiting for the debugger on port 8100... 04-24 16:24:50.686: WARN/ActivityManager(58): Launch timeout has expired, giving up wake lock! 04-24 16:24:51.170: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{450caa98 com.android.cardioworld/.login}

Upvotes: 0

Views: 666

Answers (2)

misu
misu

Reputation: 31

If u want to change the layouts u must create new Activity classes in your source folder and in on create u set the content view to the layout u want.Also don't forget to add the activity in the Manifest file inside application, for example: There are many ways to do this but I like to do it like this:

define a button :Button btnSignIn; then in on Create set the layout and initialize the button: btnSignIn =(Button)findViewByid(R.id.btnSingIn); btnSignIn.setOnClickListener(this);

u will get an error ,but put yout mouse pointer over the set onClick...and select let 'your main ActivityClass'impelment onClickListener.And then the same on the main class and select add unimplemented methods:This will ad a set onClick(View v)method,and here u can do something like this:

  switch(v.getID()){

case R.id.btnSingIn: startActivity(new Intent(getBaseContext(), SignIn.class)); break; case R. ...//and u can continue.

However I don't know if u can change layouts without starting a new activity.If u find out reply pls:)).

Hope it helped!

}

Upvotes: 1

Vicente Plata
Vicente Plata

Reputation: 3380

On Register.java, I can see:

Button next = (Button) findViewById(R.id.Button01);

But before that, there is:

setContentView(R.layout.register);

However Button01 is not on "register", but in main. There's no button on register at all, is it?

Upvotes: 0

Related Questions