Android tutorial for beginner part 11, Intent

  • 23 April 20
  • Posted By : Admin
  • 108 Hits
  • 10 Comment

how to create intent

}

Java code

Intent

Paste code on MainActivity.java

package com.salam.intentent; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { public Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btn); btn.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn: runIntent(); break; } } private void runIntent() { Intent intent = new Intent(MainActivity.this, PostActivity.class); startActivity(intent); } }

Create an emty activity

Name will PostActivity.java paste the below code

package com.salam.intentent; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class PostActivity extends AppCompatActivity { public Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post); btn = findViewById(R.id.mainActivity); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(PostActivity.this, MainActivity.class); startActivity(intent); } }); } }

Create xml file

Paste belwo code on activity_main.xml file

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Intent" android:textSize="30sp" /> <Button android:id="@+id/btn" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Post activity"/> </LinearLayout>

Create xml file

Paste below file in activity_post.xml file

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".PostActivity" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is post activity"/> <Button android:id="@+id/mainActivity" android:layout_marginTop="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back to main activity"/> </LinearLayout>