Skip to content

Examples

Dan edited this page Jul 9, 2014 · 5 revisions

Examples

  • Create a New Application In Twitter https://apps.twitter.com/app/new
    • Make sure you add a Callback URL.
    • Write your API key and API secret somewhere down (you will need them later).
  • Create a new Android Application Project in Eclipse
    • Use API 8 as the minimum SDK
  • Edit the AndroidManifest.xml and add these tags:
<manifest>

    ...

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    ...

    <application>

        ...

        <meta-data
            android:name="com.twitt4droid.auth.CONSUMER_KEY"
            android:value="YOUR_API_KEY" />
        <meta-data
            android:name="com.twitt4droid.auth.CONSUMER_SECRET"
            android:value="YOUR_API_SECRET" />

        <activity
            android:name="com.twitt4droid.activity.WebLoginActivity"
            android:theme="@android:style/Theme.Black.NoTitleBar" />
        ...

    </application>

    ...

</manifest>
  • Create an Activity called SignInActivity:
public class SignInActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Twitt4droid.isUserLoggedIn(this)) {
            startActivity(MainActivity.makeIntent(this));
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case WebLoginActivity.REQUEST_CODE:
                switch (resultCode) {
                    case RESULT_OK:
                        startActivity(MainActivity.makeIntent(this));
                        finish();
                        break;
                    case RESULT_CANCELED: break;
                }
                break;
           default: super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.twitt4droid.widget.LogInOutButton
        android:id="@+id/log_in_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
  • Create an Activity called HomeActivity:
public class HomeActivity extends FragmentActivity {

    private LogInOutButton logoutButton;
    
    public static Intent makeIntent(Context context) {
        return new Intent(context, HomeActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        logoutButton = (LogInOutButton) findViewById(R.id.log_out_button);
        logoutButton.setOnLogoutListener(new LogInOutButton.OnLogoutListener() {
            
            @Override
            public void OnLogout(LogInOutButton button) {
                finish();
            }
        });

        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, HomeTimelineFragment.newInstance())
            .commit();
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.twitt4droid.widget.LogInOutButton
        android:id="@+id/log_out_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="8dp" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/log_out_button" />

</RelativeLayout>
  • With this you have simple login/logout app with the home timeline.
  • Download the source code (Import it in Eclipse as Existing Android Code Into Workspace)

Check out the code in the app/ folder.

Clone this wiki locally