github.com/itcuties/Android-SQLite-TODO-App-Example - Tried to add the new todo field and button to activity_main.xml« Back to Previous Page

Hi, From https://github.com/itcuties/Android-SQLite-TODO-App-Example I tried to add the new task field and button to the main screen by trying to merge activity_main.xml and activity_add_todo.xml together. I tried multiple different solutions and none seemed to work. I cannot recall all the combinations I tried anymore, but have listed two below. It would seem that I cannot have two separate linear layouts without them affecting each other? Or do I need to use a listview and then an edittext instead of textview? Is there a way to make it work without having to use listview? Sample code below. Thanks.
Posted by SergeyLagent (Questions:1, Answers:1)
Asked on July 18, 2013 7:48 pm
Category: Android
0

Hi, if you want to merge those two layouts together you need to recode all the Activity logic that stands behind those layouts. Here is how you can do it. First here you have the combined view, let’s call it activity_list.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/newTodoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/addNewTodoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<ListView
android:id="@+id/todosList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

Next your activity that displays this needs to set the content view (using setContentView(R.layout. activity_list)) to this layout. Here is a good example on how we have coded this behavior – https://github.com/itcuties/Android-RSS-Reader/blob/master/ITCutiesReaderApp/src/com/itcuties/android/reader/ITCutiesReaderAppActivity.java (in line 36 you can use original activity_main layout for the adapter). The next thing you need is the behavior removing the TODO when the list element is clicked, as in the RSS example, you can code it in the ListListener https://github.com/itcuties/Android-RSS-Reader/blob/master/ITCutiesReaderApp/src/com/itcuties/android/reader/listeners/ListListener.java.

Posted by itcuties (Questions:2, Answers:18)
Answered On July 19, 2013 4:46 am #
0

Forgot pre tags and couldn’t edit post


Sample 1:
<?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" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" >


<TextView
android:id="@+id/todoText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000"
android:text="TODO TEXT GOES_HERE">
</TextView>

</LinearLayout>

<LinearLayout
android:id="@+id/formLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >

<EditText
android:id="@+id/newTodoText"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/addNewTodoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_addnew" />
</LinearLayout>
</LinearLayout>

Sample 2: Same as above, but with RelativeLayout instead of LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" >


<TextView
android:id="@+id/todoText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000"
android:text="TODO TEXT GOES_HERE">
</TextView>

</LinearLayout>

<LinearLayout
android:id="@+id/formLayout"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" >

<EditText
android:id="@+id/newTodoText"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/addNewTodoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_addnew" />
</LinearLayout>
</RelativeLayout>
Posted by SergeyLagent (Questions:1, Answers:1)
Answered On July 18, 2013 7:51 pm #
No account?



It's FREE!

Like what you have found?


... and keep us going