Skip to content

Instantly share code, notes, and snippets.

@omaraboumrad
Created November 18, 2016 14:49
Show Gist options
  • Save omaraboumrad/294dff5bb8e455f6066e55dc11b285f7 to your computer and use it in GitHub Desktop.
Save omaraboumrad/294dff5bb8e455f6066e55dc11b285f7 to your computer and use it in GitHub Desktop.
android declarative activity transition
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:transition="https://aboumrad.info/transition"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="info.aboumrad.transitiontest.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<info.aboumrad.transitiontest.TransitionButton
android:text="Load Foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
transition:to="FooActivity"/>
<info.aboumrad.transitiontest.TransitionButton
android:text="Load Bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
transition:to="BarActivity"/>
</LinearLayout>
package info.aboumrad.transitiontest;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class TransitionButton extends Button {
Context _context;
String _target;
public TransitionButton(Context context, AttributeSet attrs) {
super(context, attrs);
this._context = context;
this._target = attrs.getAttributeValue("https://aboumrad.info/transition", "to");
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
handle_click(v);
}
});
}
public void handle_click(View view){
try {
Class clazz = getActivityFromName(
_context.getApplicationContext().getPackageName() + "." + _target);
_context.startActivity(new Intent(_context, clazz));
}catch(ClassNotFoundException ex){
Toast.makeText(_context, "Unable to load " + _target, Toast.LENGTH_SHORT).show();
}
}
public Class getActivityFromName(String name) throws ClassNotFoundException {
return Class.forName(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment