vnu / Androtweet

An Android Application for Twitter Client
0 stars 0 forks source link

[Android BootCamp] Review AndroTweet #1

Open vnu opened 11 years ago

vnu commented 11 years ago

Hello @nesquena @timothy1ee I have completed my app for submission Just a quick overview of what I've done and what I haven't implemented

Three Activities

I have few question, but preferably ask them after class

I didn't have time to play with Active Android for this version.

But I am planning to finish this app and will play with it in that.

Thanks.

nesquena commented 11 years ago

:+1: Looks good Vinu, a few points of feedback:

Let us know if you have any other thoughts or questions about this assignment. Hopefully by now you feel pretty comfortable with all the major pieces to basic Android apps (Views, Controllers, Models, Authentication, API Communication, Preferences, ActionBar, et al) and see how they all fit together.

vnu commented 11 years ago

Thanks @nesquena . I'll play with it and probably shoot you an email if I have any questions over discussion or in lab. :)

nesquena commented 11 years ago

@vnu Hi Vinu, If I recall you asked me two questions on Wednesday which I told you I would answer online.

  1. How to fix your button which doesn't have any pressed states in the ActionBar
  2. How to fix the back button so you don't see the browser after login

For #1, the issue has to do with https://github.com/vnu/Androtweet/blob/master/res/layout/tweet_btn.xml#L6 where you set the background of the button. This might seem like it should only effect the color but in fact you've stumbled upon a fairly complex aspect of Android styling. If you try to change the background of a button, all the "pressed" effects disappear completely and you lose all additional styling on the button.

This is because the background is actually a complex object called a "drawable" which is an xml representation of the styling that makes the button look a certain way by default, while pressed, etc. Instead of specifying a color, you need to create your own "drawable" which describes the different states of a button.

Don't think of the "background" as a color so much as a complex description of everything about the button including color, border, pressed states, et al. So you usually set background to a "drawable" description, rather than a color.

For example, create an XML drawable like this in res/drawable/tweet_button_states.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/white" /> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/white"/> <!-- focused -->
    <item android:drawable="@color/bar_grey"/> <!-- default -->
</selector>

and then change tweet_btn.xml to:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btnTweet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/tweet_button_states"
    android:gravity="center"
    android:padding="2dp"
    android:singleLine="true"
    android:text="@string/tweet"
    android:textColor="@android:color/white"
    android:textSize="16sp" >
</Button>

Notice android:background="@drawable/tweet_button_states" which is the key line. Now the background applies the drawable with the different states (i.e gray by default and white when pressed) and you'll at least see the color change on press. You could similar set android:textColor to a drawable to change the color of the text on different states. You can also use more complex drawable to style a nicer looking button with borders by using a more detailed drawable such as:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <gradient android:startColor="#FFFFFF" 
    android:endColor="#00FF00"
    android:angle="270" />
  <corners android:radius="3dp" />
  <stroke android:width="5px" android:color="#000000" />
</shape>

which would make the button have a green gradient and a border. You could then use a drawable for each state:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/blue_gradient" /> <!-- pressed -->
    <item android:drawable="@drawable/green_gradient"/> <!-- default -->
</selector>

It's OK if this doesn't all make sense now, drawables are a huge topic. Let me know if you have any more questions.

For #2, your issue is when you login to twitter and then you hit back you don't want to see the browser, you want it to take you back home. The simplest way is to first change AndroidManifest.xml to avoid putting LoginActivity in the history:

<activity
            android:name="com.example.app.LoginActivity"
            android:noHistory="true"
            android:label="@string/app_name" >

Once you add noHistory to LoginActivity, it disappears from back. Next, rewire onBackPressed on the timeline activity:

@Override
public void onBackPressed() {
     moveTaskToBack(true);
}

Now when you log in and authenticate and then go to timeline and hit back, you should be immediately back at the home screen.

Let me know if I missed any questions or if you need more detail on the answers.