21.kaw
21.kaw

Reputation: 593

How to make a button redirect to another XML layout

I'm making a button in xml(res / layout / activity_home.xml), like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity" >
    <ImageView
        android:id="@+id/imageView1"
        android:src="@drawable/schkopwide" 
        android:contentDescription="@string/HTI"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="78dp"
        android:onclick="Intent i = new Intent(activity_store.xml);
        startActivity(i);"
        android:text="@string/HTI" />
</RelativeLayout>

so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?

Thank you

Upvotes: 1

Views: 22700

Answers (6)

Bhoomika Patel
Bhoomika Patel

Reputation: 1925

If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.

You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.

<ViewSwitcher

android:id="@+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<!--   Add Two View’s Here -- >

</ViewSwitcher>

You can take reference from this link: http://abhiandroid.com/ui/viewswitcher

Upvotes: 2

Yashaswi Bhardwaj
Yashaswi Bhardwaj

Reputation: 85

A simple way would be to create an Activity with another xml attached to it and then use intent.

Upvotes: -1

Corbella
Corbella

Reputation: 1812

You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.

In your code:

    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(this, ActivityStore.class);
            startActivity(i);
        }
    });

And in the OnCreate of the ActivityStore class, put this

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);
    }

NOTE: I supposed that yous activity_store class is called ActivityStore

Upvotes: 1

MuraliGanesan
MuraliGanesan

Reputation: 3261

Try this,

Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml

  <include layout="@layout/activity_home"/>

Sure you will get solution.

Upvotes: 0

EvZ
EvZ

Reputation: 12169

You need to check on Android documentation :

  • Activity
  • OnClickListener
  • Intent

http://developer.android.com/training/index.html

Good luck.

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Try out as below:

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="78dp"
    android:onclick="start"
    android:text="@string/HTI" />

In your main activity :

  Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(this, ActivityStore.class);
        startActivity(i);
    }
});

Here is your ActivityStore Class code:

 public class ActivityStore extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
         }
       }

Also add the activity into your mainfest file.

<activity
        android:name=".ActivityStore"
        android:label="@string/app_name"/ >

Upvotes: 1

Related Questions