Have you ever tried of combining all 4 or more activities(layouts) into single activity?
If not here is a sample which uses Image Gallery to load activities.
I have used total 5 layouts (1 main & other 4 for 4 different activity)
layout_1.java
Has a simple toast notifcation on click of a button
layout_2.java
Has a alert dialog to be opened on click of a button
layout_3.java
Has a image to be changed on click of a button
layout_4.java
Has simple toast
Java class code as follows
SlidingActivity.java
If not here is a sample which uses Image Gallery to load activities.
I have used total 5 layouts (1 main & other 4 for 4 different activity)
- sliding_activity.xml
- layout_1.xml
- layout_2.xml
- layout_3.xml
- layout_4.xml
- SlidingActivity.java
- layout_1.java
- layout_2.java
- layout_3.java
- layout_4.java
layout_1.java
Has a simple toast notifcation on click of a button
layout_2.java
Has a alert dialog to be opened on click of a button
layout_3.java
Has a image to be changed on click of a button
layout_4.java
Has simple toast
Java class code as follows
SlidingActivity.java
package com.rnd.slidingactivity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout;
public class SlidingActivity extends Activity {
/** Called when the activity is first created. */
Gallery my_gallery;
Integer[] integer_layouts=
{
R.layout.layout_1,
R.layout.layout_2,
R.layout.layout_3,
R.layout.layout_4
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sliding_activity);
my_gallery = (Gallery)findViewById(R.id.my_gallery);
my_gallery.setAdapter(new LayoutAdapter());
}
public class LayoutAdapter extends BaseAdapter {
public int getCount() {
return integer_layouts.length;
}
public View getView(int arg0, View convertView, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutViews = (LinearLayout)inflater.inflate(integer_layouts[arg0], null, false);
layoutViews.setLayoutParams(new Gallery.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)) ;
if(arg0==0)
{
LayoutActivity1 la = new LayoutActivity1(SlidingActivity.this, layoutViews);
}
else if(arg0==1)
{
LayoutActivity2 la = new LayoutActivity2(SlidingActivity.this, layoutViews);
}
else if(arg0==2)
{
LayoutActivity3 la = new LayoutActivity3(SlidingActivity.this, layoutViews);
}
else if(arg0==3)
{
LayoutActivity4 la = new LayoutActivity4(SlidingActivity.this, layoutViews);
}
return layoutViews;
}
public Object getItem(int position) {
// Do nothing
return null;
}
public long getItemId(int position) {
// Do nothing
return position;
}
}
}
LayoutActivity1.java
Layouts
sliding_activity.xml
LayoutActivity1.java
package com.rnd.slidingactivity;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class LayoutActivity1 {
Context context;
View vw;
public LayoutActivity1(Activity act,View view)
{
context = act;
vw=view;
operations();
}
public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "You have clicked the second activity Control", Toast.LENGTH_LONG).show();
}
});
}
}
LayoutActivity2.java
LayoutActivity3.java
LayoutActivity4.java
LayoutActivity2.java
package com.rnd.slidingactivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class LayoutActivity2 {
Context context;
View vw;
public LayoutActivity2(Activity act,View view)
{
context = act;
vw=view;
operations();
}
public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_2);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getAlert();
}
});
}
public void getAlert()
{
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Layout 2");
alert.setMessage("Alert for 2nd layout");
alert.setPositiveButton("Ok",new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alert.show();
}
}
LayoutActivity3.java
package com.rnd.slidingactivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class LayoutActivity3 {
Context context;
View vw;
public LayoutActivity3(Activity act,View view)
{
context = act;
vw=view;
operations();
}
public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_3);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
changeImage();
}
});
}
public void changeImage()
{
ImageView img = (ImageView)vw.findViewById(R.id.img_ll_3);
img.setImageResource(R.drawable.droid);
}
}
package com.rnd.slidingactivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class LayoutActivity4 {
Context context;
View vw;
public LayoutActivity4(Activity act,View view)
{
context = act;
vw=view;
operations();
}
public void operations()
{
Button btn = (Button)vw.findViewById(R.id.btn_ll_4);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "You are into Activity 4", Toast.LENGTH_LONG).show();
}
});
}
}
Layouts
sliding_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="@+id/my_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
layout_1.xml
layout_2.xml
layout_3.xml
layout_4.xml
layout_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#222288" android:gravity="center">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="50dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1st Layout"
android:textSize="50dp"
android:gravity="center"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<Button
android:id="@+id/btn_ll_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast Notification" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#228822" android:gravity="center">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="50dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2nd Layout"
android:textSize="50dp"
android:textColor="#ffffff"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<Button
android:id="@+id/btn_ll_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Alert Dialog" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#881122" android:gravity="center">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="50dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3rd Layout"
android:textSize="50dp"
android:textColor="#ffffff"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/img_ll_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<Button
android:id="@+id/btn_ll_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Image" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#311100" android:gravity="center">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="50dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4th Layout"
android:textSize="50dp"
android:textColor="#ffffff"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<Button
android:id="@+id/btn_ll_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast Notification" />
</LinearLayout>
Manifest file
Now enjoy your sliding activity like gallery imageview
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rnd.slidingactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SlidingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Now enjoy your sliding activity like gallery imageview
To do that right you should use ViewPager and Fragments.
ReplyDeleteJust like in this doc:
http://developer.android.com/training/animation/screen-slide.html#viewpager