I created a sample application to test how sliding menu works. Shown below in the screenshot is what i get as of now. But when I click on the categories button (shown in image below) I should get a secondary menu as shown in the zomato app's screenshot below. How can I do this ? Am I proceeding in the correct way or not?
my SlidingFragmentActivity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
con = this;
setSlidingActionBarEnabled(false);
setContentView(R.layout.main);
sm = getSlidingMenu();
sm.setMode(SlidingMenu.RIGHT);
sm.setShadowDrawable(R.drawable.shadowright);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
sm.setBehindScrollScale(1.0f);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.5f);
//sm.setSecondaryMenu(R.layout.properties);
//sm.setSecondaryShadowDrawable(R.drawable.shadow);
setTitle("Sliding Bar");
// set the Behind View
setBehindContentView(R.layout.menu_frame);
FragmentTransaction t = this.getSupportFragmentManager()
.beginTransaction();
mFrag = new SampleListFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
}
my SampleListFragment :
public class SampleListFragment extends SherlockFragment {
private static final String[] Radio_buttons = new String[] { "Distance",
"Rating" };
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container, false);
ListView radio_list = (ListView) view.findViewById(R.id.RadioList);
Button categories = (Button) view.findViewById(R.id.sampleButton);
radio_list
.setAdapter(new ArrayAdapter<String>(MainActivity.con,
android.R.layout.simple_list_item_single_choice,
Radio_buttons));
radio_list.setItemsCanFocus(true);
radio_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
categories.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.sm.showSecondaryMenu();
}
});
return view;
}
}
main.xml
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Sliding menu demo...!!!" />
</RelativeLayout>
menu_frame.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SEARCH"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:id="#+id/searchTextLayout"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginBottom="20dip"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip" >
<ImageButton
android:id="#+id/searchTextButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#685E5C"
android:scaleType="fitCenter"
android:src="#drawable/abs__ic_search" />
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/searchTextButton"
android:background="#drawable/background_black_border_full"
android:padding="8dp"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingLeft="10dp"
android:text="SORT BY"
android:textColor="#FF3300"
android:textSize="20dp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="134dp" >
<ListView
android:id="#+id/RadioList"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
<Button
android:id="#+id/sampleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Categories" />
</LinearLayout>
</ScrollView>
SlidingMenu does NOT do this, zomato uses a custom implementation.
SlidingMenu will let you have a menu on the left and the right, but not two on either side.
I would look at using a view pager or a custom implementation. EitherWay I don't know of anything out of the box to do this. I might be worth looking at Android Views for inspiration.
It's a bit late, but still let me post my answer in case it will help someone in future. If you want to show another menu you can use
setMode(SlidingMenu.LEFT_RIGHT);
setSecondaryMenu(R.layout.yourSecondMenu);
and in your button click
showSecondaryMenu(true);
and perform your actions in that class.
Related
This is inside the LinearLayout of my activtiy_main.xml. It contains an emply frame layout and a button outside the frame.
<FrameLayout
android:layout_weight="0.8"
android:background="#color/colorAccent"
**android:id="#+id/placeholder"**
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="match_parent">
</FrameLayout>
<Button
android:id="#+id/button"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is my fragment1.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Hello Sir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
I have successfully created a java file named hello for this fragment.
And lastly this is my MainActivty.java
public class MainActivity extends AppCompatActivity {
Button change , change2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
change = (Button) findViewById(R.id.button);
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new hello();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
R.animator.fragment_slide_left_exit,
R.animator.fragment_slide_right_enter,
R.animator.fragment_slide_right_exit);
ft.replace(R.id.placeholder, fragment);
ft.commit();
}
});
}
}
This executes fine but the problem is the FrameLayout(from the activtiy_main.xml) stays even after the fragment appears on top of it.
Screen Shot after the button is clicked
I want to make the frameLayout(id - placeholder) to disappear or hide after the fragment has come on top of it.
Please help me.
P.S: This just a test application. Can't share the original one.
Thanks in advance.
Just keep mainActivity as place holder
<FrameLayout
android:layout_weight="0.8"
android:background="#color/colorAccent"
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:textAlignment="center"
android:layout_height="match_parent">
</FrameLayout>
Add one fragment_one with button in activity and replace fragment_one with fragment_two it will work fine.
fragment_one
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
fragment_two
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Hello Sir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
I'm trying to launch a fragment from an activity. However, when I run the app and click on the button that has to launch the fragment I get the error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0074 (com.example.hudhud.islam:id/kontaktfragment) for fragment Kontakt{aaaed67 #1 id=0x7f0e0074}
I can't see where I've made something wrong. It should be correct. This is the class where I implement the fragment. I'm only gonna upload the View onCreateView for this class as no more is needed:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_kontakt, container, false);
sendmail = (Button) view.findViewById(R.id.sendknap);
sendmail.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
msg = (EditText) view.findViewById(R.id.besked);
String message = msg.getText().toString();
sendemail(message);
}
});
return view;
}
I launch the fragment from here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.Kontakt) {
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.add(R.id.kontaktfragment, fragment)
.commit();
}
return super.onOptionsItemSelected(item);
}
What have I missed?
Any help is appreciated!
EDIT:
This is the XML for the activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32c6a6"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/velkomst"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<FrameLayout
android:id="#+id/Buttons"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/velkomst" >
</FrameLayout>
</RelativeLayout>
And this is the kontakfrag XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/kontaktfragment"></FrameLayout>
</RelativeLayout>
And this is the fragment_kontakt XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#32c6a6"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/besked"
android:layout_weight="0.70"
android:textSize="20dp"
android:layout_marginTop="30dp"
android:textIsSelectable="true"
android:textColor="#000000"
android:background="#ffffff"
android:gravity="top"
android:paddingTop="30dp"
android:hint="Hvis du har feedback eller nogle spørgsmål, så er du velkommen til at skrive. Vi besvarer mailen hurtigst muligt"
android:scrollIndicators="right"/>
<Button
android:layout_width="144dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendknap"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05"
android:textSize="20dp"/>
</LinearLayout>
My mistake was that I created the fragment as a separate layout and not as a framelayout on the frontpage. So the solution was to create a framelayout on the frontpage. That makes sense :)
First to the layout of activity, add this
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then in that activity java code add
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
I got an activity with many fragments. When i change fragments i need to change also and my toolbar declared in MainActivity.class. My problem is that i include a layout and didnt find a method to change this layout. How to change toolbars layout in this case?
MainActivity.class
#ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
#InjectView(R.id.tool_bar)
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
String currentFragmentClass = fragment.getClass().getSimpleName();
if (currentFragmentClass.equals(getString(R.string.info_fragment))) {
//here i need to change and set onclicks
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
<View
android:id="#+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolBar"
>
<RelativeLayout
android:id="#+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="#mipmap/icon_sliding_menu" />
<ImageView
android:id="#+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/jammboree" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/app_logo"
android:padding="5dp">
<ImageView
android:id="#+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="#mipmap/icon_my_cart" />
<TextView
android:id="#+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/hotlist_bell"
android:layout_alignTop="#id/hotlist_bell"
android:background="#drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
If I understood right, you want to change the toolbar whenever you change the Fragment the user is currently seeing, isn´t that right?
There is a very simple way to achieve this trough your XML. Let's say you want three different styled toolbars.
1) First, you create XML files for every toolbar you need.
2) Then you include every toolbar in to your activity_main.xml, like this:
<?xml version="1.0" encoding="utf-8"?>
<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=".activities.MainActivity">
<include
android:id="#+id/toolbar1"
layout="#layout/tool_bar1"
android:visibility="visible" />
<include
android:id="#+id/toolbar2"
layout="#layout/tool_bar2"
android:visibility="gone" />
<include
android:id="#+id/toolbar3"
layout="#layout/tool_bar3"
android:visibility="gone" />
// Here is the rest of your XML code
</RelativeLayout>
See how all three toolbar includes have the visibility property?
Well, now you can toy with this property, and show/hide the desired toolbar whenever you want/need to.
For example:
RelativeLayout mLayout = (RelativeLayout) getActivity().findViewById(R.id.my_main_layout);
Toolbar mToolbar1 = (Toolbar) mLayout.findViewById(R.id.toolbar1);
mToolbar1.setVisibility(View.GONE);
Toolbar mToolbar2 = (Toolbar) mLayout.findViewById(R.id.toolbar2);
mToolbar2.setVisibility(View.GONE);
Toolbar mToolbar3 = (Toolbar) mLayout.findViewById(R.id.toolbar3);
mToolbar3.setVisibility(View.VISIBLE);
And just change which one are you making visible, according to your needs.
Hope this helps.
I don`t think the method of #herrmartell is nice;
In my way,I have three Fragment in a Activity,I defined some different layout resources for a common toolbar xml,and load them in toolbar when I click different button; my code :
common_toolbar xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- your custom layout -->
</android.support.v7.widget.Toolbar>
activity_main_toolbar_common_view.xml
<TextView
android:id="#+id/action_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="迪士尼...ˇ"
android:textColor="#color/white"
android:textSize="#dimen/larget_text_size"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
activity_main_toolbar_mine_view.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mine"
android:textSize="#dimen/big_text_size"
android:textColor="#color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<ImageView
android:id="#+id/setting"
android:layout_width="#dimen/toolbar_icon_size"
android:layout_height="#dimen/toolbar_icon_size"
android:src="#drawable/toolbar_setting"
android:layout_alignParentRight="true"
/>
switchToolbar() method in Activity:
public void switchToolbar(int layout) {
if(currentToolbarLayout == layout){
return;
}
currentToolbarLayout = layout;
View v = getLayoutInflater().inflate(layout,null);
toolbar.removeAllViews();
toolbar.addView(v);
switch (layout){
case R.layout.activity_main_toolbar_common_view:
v.findViewById(R.id.action_location).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到位置", Toast.LENGTH_SHORT).show();
}
});
break;
case R.layout.activity_main_toolbar_mine_view:
v.findViewById(R.id.setting).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "跳到设置", Toast.LENGTH_SHORT).show();
}
});
break;
}
}
I render my toolbar view in the onCreate() method of Activity:
ButterKnife.bind(this);
setSupportActionBar(toolbar);
switchToolbar(R.layout.activity_main_toolbar_common_view);
when I switch toolbar, I call:
switchToolbar(R.layout.activity_main_toolbar_mine_view);
I have some code in an Android app which was working fine but is now throwing an NPE when trying to locate a LinearLayout. The first line of the ReadRecords method below is returning a null for the linearLayoutRecords object, which is causing an NPE on the 2nd line:
public class SavedMealsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_meals);
// enable the home button
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
// Read saved meal records from the database and display them
readRecords();
}
public void readRecords() {
LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
linearLayoutRecords.removeAllViews();
The layout XML is here:
<LinearLayout 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:background="#drawable/background"
android:orientation="vertical"
tools:context="com.ian.mealtimer.SavedMealsActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/activity_saved_meals_textview1"
android:textSize="16sp"
android:textStyle="bold" />
<ScrollView
android:id="#+id/scrollViewRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewRecordCount"
android:layout_margin="5dip"
android:padding="5dip" >
<LinearLayout
android:id="#+id/linearLayoutRecords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
Any ideas??
I am having a silly problem with a OnItemCliskListener for my ListView. It works well when the XML is very simple but after I improve its complexity, the OnItemCliskListener stop working
In other words, I have a ListView with just a TextView. But if I turn my ListView into a more complex list with other TextView, an Icon a CheckBox, the ClickListener stop working.
If I run exactly the same code changing just the XML, the OnItemCliskListener stops working. I would be rather grateful if you could help me
WORKING LIST VIEW:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<TextView android:id="#+id/title"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/appCheck"
android:layout_toRightOf="#+id/appIcon"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</RelativeLayout>
NOT WORKING LISTVIEW:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/icon" android:id="#+id/appIcon"
android:layout_width="30px" android:layout_height="40px"></ImageView>
<CheckBox android:id="#+id/appCheck"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true"></CheckBox>
<TextView android:id="#+id/title"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/appCheck"
android:layout_toRightOf="#+id/appIcon"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TextView android:id="#+id/subtitle"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_toLeftOf="#+id/appCheck"
android:layout_toRightOf="#+id/appIcon" android:layout_below="#+id/title"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
</RelativeLayout>
MY JAVA ACTIVITY:
public class MyTabsActivity extends TabActivity implements OnTabChangeListener {
private static final String LIST3_TAB_TAG = "List3";
private RelativeLayout layoutTab3;
private MyArrayAdapter adapterListViewTab3;
private TabHost tabHost;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
...
...
// ----------------- BUILD TAB3
// That is my Tab of Running App
layoutTab3 = (RelativeLayout) findViewById(R.id.running_app_relative_layout);
// Fill my ListView of Running App
ListView runningAppList = (ListView) findViewById(R.id.running_app_listview);
List<String> list3Strings = new ArrayList<String>();
list3Strings.add("Item 1");
list3Strings.add("Item 2");
list3Strings.add("Item 3");
list3Strings.add("Item
adapterListViewTab3 = new MyArrayAdapter(this, android.R.layout.simple_list_item_1, list3Strings);
runningAppList.setAdapter(adapterListViewTab3);
// ????????????????????????????????????????????????????????????????????????????????????????????
// >>>>>>>>>>>> THIS OnItemClickAdapter DOESN'T WORK !!!!! WHY ????????
runningAppList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MyTabsActivity.this, "EUREKA", Toast.LENGTH_SHORT).show();
}
});
...
// add Tab3
TabSpec tab3 = tabHost.newTabSpec(LIST3_TAB_TAG).setIndicator(LIST3_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return layoutTab3;
}
});
tabHost.addTab(tab3);
layoutTab3.setVisibility(View.INVISIBLE);
...
}
}
MY R.layout.main:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="#+id/list1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
<ListView android:id="#+id/list2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
<RelativeLayout android:id="#+id/running_app_relative_layout"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:id="#+id/coco" android:layout_alignParentTop="true"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="#+id/running_app_listview"
android:scrollbars="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="fill_vertical"
android:layout_alignParentTop="true"></ListView>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
I've figured out that when a custom ListView contains focusable elements, onListItemClick won't work (I think it's the expected behaviour). So I've just removed the focus from the custom view and it did the trick. In other words, I added the following commands to my view:
android:focusable="false"
android:focusableInTouchMode="false"
The full explanation is avaiable here: How to fire onListItemClick in Listactivity with buttons in list?