Add ExpandableListView to a tab with other elements - android

I am developing an app with tabs. On one of the tabs,
I have two EditText fields and a couple of buttons. I want to add an ExpandableListView
to the tab, below the rest of the elements.
My problem is that the current class extends Activity. All the example I've found extend ExpandableListActivity, but I can't extend both for the same class.
How can I add this list to the tab? I've added an ExpandableListView element in the XML file, but whenever I run it, it doesn't show up. I understand that I need to bind the view to some data, but how do I go about doing this?
Thank you.
Here is my class code:
public class DirectionsTab extends Activity
{
private Button clear_btn;
private Button go_btn;
private EditText origin_txt;
private EditText dest_txt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.directions_tab);
// initialize views and onClick event handler
origin_txt = (EditText)findViewById(R.id.origin_txt);
dest_txt = (EditText)findViewById(R.id.dest_txt);
clear_btn = (Button)findViewById(R.id.clear_btn);
// clear all text fields and return focus to dest_txt
clear_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
origin_txt.setText("");
dest_txt.setText("");
origin_txt.requestFocus();
}
});
} // end public void onCreate(Bundle savedInstanceState)
} // end public class DirectionsTab extends Activity
and XML:
<?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">
<TextView
android:id="#+id/origin_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Origin"/>
<EditText
android:id="#+id/origin_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/origin_lbl"/>
<TextView
android:id="#+id/dest_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/origin_txt"
android:text="Destination" />
<EditText
android:id="#+id/dest_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/dest_lbl" />
<ExpandableListView
android:id="#+id/listView"
android:layout_below="#+id/dest_txt"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /><!--android:id="#android:id/list"-->
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dest_txt"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="Clear" />
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/clear_btn"
android:layout_alignTop="#id/clear_btn"
android:text="Go!" />
</RelativeLayout>

I got this!, If anyone is having problems with this, make sure you follow these steps:
Copy and paste ExpandableList3 example (located in ApiDemos project from Samples folder in your Android SDK). This is just a good example of how to set up a list.
Make your class inherit from ExpandableListActivity
Create the following in your XML layout file:
<ExpandableListView
android:id="#android:id/list"
android:layout_below="#+id/go_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
It is VERY important that the ID property matches what you see above, otherwise, nothing will work. This little detail wasted plenty of hours for me and was discovered seating neatly as a small detail in the android docs.

Related

Leanback focus problems

Hello guys I'm working on application and my layout structure is as following :
RelativeLayout:
CompoundView:
CompoundView:
RelativeLayout:
Button
Button
RecyclerView
BrowseFragment:
Only rows
My problem is when I get to first row of browse fragment and first item in it and I want to go up (D-PAD-UP) to focus button it does nothing it works only when I push left ( D-PAD-LEFT). Anyone has solution for this ?
So the problem was in BrowseFrameLayout for some reason and to solve this issue I had to override onFocusSearchListener and manage focus myself.
In BrowseFragment which I extended I have this method :
public void workaroundFocus(){
if(getView() != null) {
View viewToFocus = getActivity().findViewById(R.id.view_to_focus);
BrowseFrameLayout browseFrameLayout = getView().findViewById(android.support.v17.leanback.R.id.browse_frame);
browseFrameLayout.setOnFocusSearchListener((focused, direction) -> {
if (direction == View.FOCUS_UP) {
return viewToFocus;
}
else {
return null;
}
});
}
}
And then:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
workaroundFocus();
/*
Rest of the code
*/
}
And voila it works.
Since you are using the RelativeLayout you should layout the components in the order that you would like to be navigated.
Using the XML attributes presented in the RelativeLayout reference document, you will be able to establish an navigation order too:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/reminder" />
<Spinner
android:id="#+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/times" />
<Spinner
android:id="#id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="#id/times"
android:layout_alignParentRight="true"
android:text="#string/done" />
</RelativeLayout>
Here the dates Spinner is below the name EditText and at left of times Spinner, the times component is below the name one and the done Button is below times. See the image below:
See the Build Basic TV Layouts guide for more tv related details.

Sidebar in each activity

For an application I am designing we have to deal with some "special" needs of our users. One of these is to create a navigation sidebar that has to appear in almost all the activities.
This navigation bar has to contain always the same three buttons that links to three activities: HOME, INFO, CONFIGURATION.
Each of these three activities can also load others activities that can (or cannot) contain this navigation bar.
Each of these buttons has to reset the current activity-stack status bringing to the top the corresponding activity selected by the user.
The navigation bar has to be customisable (made visible/invisible) and I would like also to deactivate some of the buttons.
EDIT: It has to be similar to a Drawer, but the buttons have to be highly customisable (in size and appearance) and it has to be always on (no sliding functions).
What is the best way to achieve that without manually including those buttons in each of my layouts?
You can create this layout manually but only once - in your ActivityBase class. Other activities can extend this base class.
EDIT:
As I said, I'm improving my answer.
So my idea is to create an Activity with menu. And if other Activity needs to have the same menu it can extend this ActivityBase and add it's own layout.
Let's look on a simple example.
ActivityBase layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/black" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_alignParentRight="true"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/menu" >
</RelativeLayout>
</RelativeLayout>
As you can see I created a simple layout that contains menu bar and a container for layout from Activities that will be extending AcivityBase.
Now ActivityBase:
public class ActivityBase extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
}
}
It's a simple Activity, but if you want, you can also place here menu events handling if they are the same for all Activities that will be extending this.
And now let's see at SecondActivity's layout:
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
It's a normal layout, nothing special. I put some controls in there just for purpose of this example.
And SecondActivity class:
public class SecondActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = getLayoutInflater();
inflater.inflate(R.layout.second_activity, (ViewGroup) findViewById(R.id.container));
}
}
It extends ActivityBase and what is important - it does not call setContentView. Instead we are creating LayoutInflater and we are inflating second_activity layout in the container that we created in activity_base layout.

Why aren't both of my views being displayed?

So I've been trying to figure out layouts and using the layout inflater, but I'm running into some issues. I have two relative xml layout files, the one xml file has two textViews, an editText field, and a spinner object:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/goalNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:text="#string/goalName" />
<EditText
android:id="#+id/goal_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/goalNameView"
android:hint="#string/editText"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/goalTasksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/goal_tasks"
android:layout_alignBottom="#+id/goal_tasks"
android:layout_alignLeft="#+id/goalNameView"
android:text="#string/goalTasks" />
<Spinner
android:id="#+id/goal_tasks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/goalTasksView"
android:layout_marginTop="51dp" />
</RelativeLayout>
the other xml file has one textView and one editText field:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tView"
>
<EditText
android:id="#+id/taskNameField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/taskNameView"
android:ems="10"
android:hint="#string/editText" />
<TextView
android:id="#+id/taskNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/taskNameField"
android:layout_alignBottom="#+id/taskNameField"
android:layout_alignParentLeft="true"
android:text="#string/taskName"
/>
</RelativeLayout>
I'm trying to patch the two layouts together with a third xml file (main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/theMain"
>
<include android:id="#id/mainLayout" layout="#layout/activity_goal_keeper" />
</LinearLayout>
The way I'm looking to have the layout work is to display the first layout (#id/firstLayout) and then the second layout (#id/tView) directly beneath it. I've read that I need to implement the layouts on a LinearLayout to achieve this which is why I have the third layout (#id/theMain).
As you can see I have an include statement for #id/firstLayout, but I don't have an include statement for #id/tView because I'm trying to inflate multiple versions of #id/tView via my main activity:
public class GoalKeeperActivity extends Activity implements AdapterView.OnItemSelectedListener {
public Integer[] items= {1,2,3,4,5,6,7,8};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.activity_goal_keeper);
setContentView(R.layout.main);
Spinner numOfTasks=(Spinner) findViewById(R.id.goal_tasks);
numOfTasks.setOnItemSelectedListener(this);
ArrayAdapter<Integer> aa = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numOfTasks.setAdapter(aa);
ViewGroup item = (ViewGroup) findViewById(R.id.theMain);
for(int i=0; i<3;i++)
{
View child = getLayoutInflater().inflate(R.layout.tasks_view, item, false);
child.setId(i);
item.addView(child);
}
My problem is that #firstLayout displays properly, but #tView doesn't show up at all. Can someone please explain what I'm missing?
Thanks in advance.
I figured it out, it was simply an issue of setting the "android:layout_height" attribute equal to wrap_content on the #id/tView xml file. Silly little mistakes...

Android: i am trying to add a view into a list

i have made this xml layout file :
<?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="wrap_content"
android:background="#color/gray"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_margin="10dp"
android:background="#color/black"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/s_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/gray"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/e_hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/gray"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
and i have this line of codes:
class WorkingView extends LinearLayout
{
public WorkingView(Context context,String str) {
super(context);
// TODO Auto-generated constructor stub
setContentView(R.layout.work_data);
Text s_hour = (Text) findViewById(R.id.s_hour);
s_hour.setData(str);
}
}
and this line from another main class which extends activity:
private void readData() {
WorkingView wv = new WorkingView(this,"hello");
list.addView(wv);
}
it all works fine, until the code calls this readData() method.
i don't understand what is wrong?
For what you are doing you shouldn't use a list.
If you simply want add one view under another:
Have a linearLayout as the "container", and then add them to that.
Lists are used if you have a bunch of data which all needs to be lined up under eachother with one or more properties. And for that you need to look into adapters, arrayadapter is the more simple one, and cursoradapter a bit more advanced.
Also, your method will probably crash since you don't inflate your xml. For inflation of views look here
For scrolling capabilities look into ScrollView and here is a guide to use it

Fragment does not show after inflating [Fixed, but...]

I'm trying to create a music player that will be visible in all my activities. To do this, I'm going to use a Fragment as some of you advised me earlier.
Since I have no experience with Fragments whatsoever, I decided to implement a fragment in a "filler" activity first.
I created a simple fragment containing only a button and some text, and try to inflate that in my filler activity.
However, after inflating this fragment does not show up. A message does get printed to LogCat telling me that the fragment has been inflating.
I'm pretty sure I'm missing something, but since I have no experience with this and I couldn't find a tutorial that quite explained how to do this, I don't know what it is that I'm missing.
Music player Fragment
public class AppMusicPlayer extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
System.out.println("fragment added");
return inflater.inflate(R.layout.musicplayer_main, container, false);
}
}
Music Player layout
<?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="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the fragment you're looking for" />
</LinearLayout>
Filler Activity
public class Filler extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filler);
Button b = (Button) findViewById(R.id.terug);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Filler layout
<?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" >
<!-- Title bar -->
<RelativeLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/title_bar" >
<TextView
android:id="#+id/fillertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Work In Progress"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" >
</TextView>
<Button
android:id="#+id/terug"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="#drawable/button_back"
android:text="Back"
android:textColor="#FFFFFFFF"
android:textStyle="bold" >
</Button>
</RelativeLayout>
<!-- End of title bar -->
<TextView
android:id="#+id/wip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This section of the app has not been made yet. Work in progress." >
</TextView>
<fragment
android:id="#+id/musicPlayerFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:name="com.mobowski.app.player.AppMusicPlayer" />
</LinearLayout>
Obviously I'm doing something wrong, but what?
Note: I'm using Android 2.3 with the Compatibility library.
Any help is appreciated
The problem has been fixed with the help of Yashwanth Kumar and blessenm. One gave an XML solution, the other a programmatic solution. Now I'm just wondering which solution is the most desirable one. Are there any concessions to be made with either of the solutions, or does it all boil down to the programmer's preferred solution?
I haven't tried adding fragments directly to xml. But what I normally do is add a framelayout or linearlayout to the filler layout.
Suppose its id is 'fragment_holder'. I use the following code in the fragment activity right after setContentView. Try this
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new AppMusicPlayer(),"musicplayer");
ft.commit();
you should mention layout_height as 0dip, not the width in vertical linear layout.
change that it should work.
<fragment
android:id="#+id/musicPlayerFragment"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:name="com.mobowski.app.player.AppMusicPlayer" />

Categories

Resources