Not possible to click a Button - android

I'm having a weird issue with a button that is being shown on screen, but that I can't click. It is not that the onClick event is not firing, it seems that physically it does not react to clicks, as if it had a transparent layer on top of it.
The main Activity where it is built on:
public class MusicList extends Activity implements OnClickListener {
...
#Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.mymusic);
this.myTabHost = (TabHost)this.findViewById(R.id.th_set_menu_tabhost);
this.myTabHost.setup();
TabSpec ts = myTabHost.newTabSpec("TAB_TAG_1");
ts.setIndicator("Media Item",getResources().getDrawable(R.drawable.music));
ts.setContent(R.id.media_item_tab);
try {
relativeLayout = (RelativeLayout) myTabHost.findViewById(android.R.id.tabcontent).findViewById(R.id.media_item_tab);
} catch (NullPointerException e) {
Log.e(this.getClass().getSimpleName(),"Layout is not correct",e);
}
Button button = (Button)relativeLayout.findViewById(R.id.play_button);
// button.setClickable(true);
button.setOnClickListener(this);
myTabHost.addTab(ts);
}
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(itemData.get("ITEM_URL"))));
}
}
The layout, which is shown perfect on screen is like this:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/th_set_menu_tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65px">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/media_item_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/media_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#40A5D9"
android:gravity="center"
android:layout_alignParentTop="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Title"
>
</TextView>
<ImageView
android:id="#+id/media_cover"
android:layout_width="fill_parent"
android:layout_height="180px"
android:gravity="center"
android:layout_below="#+id/media_title"
android:src="#drawable/geniocover"
>
</ImageView>
<TextView
android:id="#+id/media_author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#86CCF3"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_below="#+id/media_cover"
android:text="Author"
>
</TextView>
<TextView
android:id="#+id/media_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#DAFFFF"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:layout_below="#+id/media_author"
android:text="Album (Year)"
>
</TextView>
<Button
android:id="#+id/play_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:textColor="#003983"
android:textStyle="bold"
android:textSize="20sp"
android:gravity="center"
android:layout_below="#+id/media_album"
android:clickable="true"
>
</Button>
</RelativeLayout>
<ListView
android:id = "#+id/tablist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</TabHost>
Any suggestions?
Thanks!

You have a FrameLayout with two children when it's only intended to host one. The ListView was last added, will end up on top and is probably receiving all of your input.

I think the problem is caused by the complex structure of your tablayout.
Usually, I use a separate Activity holding the TabLayout and just put different activities as content into the tabhost (tabSpec.setContent(Intent intent)).
Actually this should solve your problem.
By the way, avoid using RelativeLayout where you could use a LinearLayout.

Related

How to call a layout inside the linear layout after clicking the buttons?

I'm learning android and And I'm facing some troubles. I kindly request you to solve these problems.
I have 2 linear layouts inside a linear layout, both are placed horizontally. The left side section contains some buttons and if we click the button the respective layout must come on the right-side section. As of now, if I click the button the specific layout is appearing but not inside the second linear layout. And also, I want to set a default layout to appear on the right side layout. For ex, here I've added the "breakfastdishes.xml", which I want as a default right-side layout, and when I click on the buttons from the left-side layout, according to id the right-side layout must change. Can you please help me to achieve it?
Here is my code:
MenuSection.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuSection"
android:background="#drawable/gradient"
android:padding="10dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/breakfast"
android:layout_width="220dp"
android:layout_height="65dp"
android:text="#string/breakfast"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#AC8E0D"/>
<Button
android:id="#+id/lunch"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/lunch"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#0A5FAA"/>
<Button
android:id="#+id/eveningSnacks"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:text="#string/snacks"
android:textSize="20dp"
android:textStyle="bold"
app:backgroundTint="#DF5124" />
<Button
android:id="#+id/dinner"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/dinner"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#14A61A"/>
</LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:id="#+id/itemsDisplay">
<LinearLayout
android:id="#+id/menuDispArea"
android:layout_width="508dp"
android:layout_height="412dp">
<!-- <include-->
<!-- layout="#layout/breakfastdishes"-->
<!-- android:layout_width="508dp"-->
<!-- android:layout_height="412dp" />-->
</LinearLayout>
</ScrollView>
</LinearLayout>
BreakfastDishes.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:paddingStart="12dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingEnd="12dp"
android:paddingRight="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical">
<ImageView
android:id="#+id/burger"
android:layout_width="155dp"
android:layout_height="102dp"
android:layout_x="10dp"
android:layout_y="10dp"
app:srcCompat="#mipmap/burger_breakfast" />
<TextView
android:id="#+id/burgerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/burger"
android:layout_alignTop="#+id/burger"
android:layout_alignRight="#+id/burger"
android:layout_alignBottom="#+id/burger"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/burger"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical">
<ImageView
android:id="#+id/eggOmlet"
android:layout_width="160dp"
android:layout_height="104dp"
android:layout_x="233dp"
android:layout_y="7dp"
app:srcCompat="#mipmap/egg_omlet" />
<TextView
android:id="#+id/eggOmletTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/eggOmlet"
android:layout_alignTop="#+id/eggOmlet"
android:layout_alignRight="#+id/eggOmlet"
android:layout_alignBottom="#+id/eggOmlet"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/eggOmlet"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
MenuSection.java
package com.example.restaurant;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MenuSection extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_section);
Button breakfast = (Button) findViewById(R.id.breakfast);
Button lunch = (Button) findViewById(R.id.lunch);
breakfast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, BreakfastDishes.class);
startActivity(intent);
}
});
lunch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, LunchDishes.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed(){
//super.onBackPressed();
}
}
Akshay, here are a few things you have to keep in mind ;
In the button click listener you are launching new activity instead of updating
the existing layout. When you use Intent it will launch new activity and
close the current activity. So in your case do not use startActivity
Use findViewById for the menuDispArea layout and add update
the content when user clicks on the button.
never use dp for text size, use sp
you can divide the layout using LinearLayout weights to split your layouts on
all the devices instead of hardcoding the height and width
https://developer.android.com/guide/topics/ui/layout/linear#Weight
edit: Added sample
LayoutInflater inflater = LayoutInflater.from(MenuSection.this);
LinearLayout parentLayout = findViewById(R.id.menuDispArea);
View menuLayout= inflater.inflate(R.layout.BreakfastDishes, parentLayout, false);
parentLayout.addView(menuLayout);
You can use this to inflate any XML in your layout dynamically. Make sure you are doing this inside button onClick for your use case.

Android Changing Text of TextView on Button click not working

As the title shows, I'm trying to make a button in my Android app to change a TextView but it ain't working properly... Here's the code, I hope you guys see what's going wrong. FYI, I'm using NetBeans with the Android 2.3.3 emulator connected as I will be running on my phone (2.3.6) later.
Main java:
public class Rooster extends Activity
{
private Button buttonSearch;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
this.buttonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textRooster = (TextView)findViewById(R.id.textRooster);
textRooster.setText("some text");
}
});
}
Well here is my main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="42px"
android:layout_weight="1"
android:gravity="center_vertical" >
<EditText
android:id="#+id/editSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="2"
android:gravity="center"
android:hint="Zoekopdracht" />
<Button
android:id="#+id/buttonSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="1"
android:gravity="center"
android:text="Zoek" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="39px"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/buttonChangeWijzigingen"
android:layout_width="fill_parent"
android:layout_height="37px"
android:gravity="center"
android:text="Zet wijzigingen aan" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="12" >
<TextView
android:id="#+id/textRooster"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Geen rooster opgezocht" />
</LinearLayout>
</LinearLayout>
I think the button that you think your clicking is actually getting drawn over. try changing
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
to
this.buttonSearch = (Button)this.findViewById(R.id.buttonChangeWijzigingen);
It doesnt actually fix the problem but just makes more clear what the issue is.

registerForContextMenu not get calling properly - what am I doing wrong?

The answer to this question might be really obvious but its giving me a headache. I have a simple LinearLayout with one single ListView in it. I do this: onCreate
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.friends);
ListView listView = (ListView) findViewById(R.id.friend_list);
listAdapter = new CheckinListAdapter(checkins, listView, R.layout.checkin_list_item);
listView.setAdapter(listAdapter);
if (getLastNonConfigurationInstance() != null) {
FriendsActivity last = (FriendsActivity) getLastNonConfigurationInstance();
this.checkins.addAll(last.checkins);
this.sort = last.sort;
} else {
refresh();
}
registerForContextMenu(listView);
}
But for some reason onCreateContextMenu never gets called! So I did some research and since I am loading the list after the register, perhaps it doesn't register it correctly. If I go in my ListAdapter and do registerForContextMenu it does show up. But it doesn't behave correctly with the keyboard. So I am now confused on what can be the error because it seems a little non intuitive for me to have to register each child item. All the examples I find online are using ArrayAdapter. :(
Any suggestions?
Edit
Here is more detail, in case it something I don't see:
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="#string/check_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheckInClicked"/>
<ListView android:id="#+id/friend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
List item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView android:id="#+id/user_photo"
android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="centerCrop"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dip">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/user" style="#style/TextButton"/>
<TextView android:text="#string/at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="#+id/venue"
android:singleLine="true"
android:ellipsize="end"
style="#style/TextButton"/>
</LinearLayout>
<TextView android:id="#+id/venue_address" style="#style/GreyLarge"/>
<LinearLayout android:id="#+id/checkin_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip">
<ImageView android:id="#+id/checkin_image"
android:layout_width="70dip"
android:layout_height="60dip"
android:layout_marginRight="8dip"
android:scaleType="centerCrop"/>
<TextView android:id="#+id/checkin_shout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/elapsedTime" style="#style/GreySmall"/>
</LinearLayout>
</LinearLayout>
This took me 6 hours to figure out but it turns out I had to add:
android:focusable="false"
to all my <Button/> tags.
Related post: TextView and Button in each row and onListItemClick()
Do, registerForContextMenu(listView);
before setting the adapter, that is before:
listView.setAdapter(listAdapter);
You have mentioned that the menu is not responding correctly with the keyboard actions. Can you tell me what exactly the problem is?

How to programmatically add view in ViewFlipper

I have following main layout:
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<ViewFlipper android:id="#+id/viewstack"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Here I want to add my views which are located in separated xml files. -->
</ViewFlipper>
</LinearLayout>
Here is example of my view:
view_url.xml
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:gravity="center">
<EditText android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnGenerate"
android:text="Generate"/>
</LinearLayout>
view_text.xml
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<EditText android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_height="wrap_content"
android:contentDescription="Enter your text here"
android:layout_width="fill_parent"
android:height="200dp"/>
</LinearLayout>
I am trying to add views:
viewstack = (ViewFlipper) findViewById(R.id.viewstack);));
View viewText = (View) findViewById(R.layout.view_text);
viewstack.addView(viewText); < -- Emulator is crashing at this line
View viewUrl = (View) findViewById(R.layout.view_url);
viewstack.addView(viewUrl);
I dont have any idea what is wrong with my code. I decided to put all my views in one file, but I still want to know how to fix my initial code.
Yout View viewUrl = (View) findViewById(R.layout.view_url); is very wrong. findViewById is kinda like the get(String key) method the the directory where your directory is your current view/activity. It only looks up the element with that Id under it's children.
To create Java objects out of XML files you need use you need to use the LayoutInflater. Which is pretty straight forward, out of that you get the object you can pass to viewstack.addView(..) method.
Another way to achieve this would be to just include the other XML files into the first one by using either the include, merge tags, or the ViewStub. Depending on your requirements these might not be an option though, but what you are describing they should be, and you should use these instead of doing it programatically because it's just cleaner this way.
maybe this help:
this.flipper=(ViewFlipper)findViewById(R.id.flipper);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.viewLoader=(View)inflater.inflate(R.layout.search_result_grid, null);
flipper.addView(viewLoader);
this.viewResultGrid=(View)inflater.inflate(R.layout.search_result_grid, null);
gvSearchResult=(GridView)viewResultGrid.findViewById(R.id.gridViewSearchResult);
flipper.addView(viewResultGrid);
It's name suggests that It will find by id not by layout.You should understand difference between layout and id.use like this one View v=(View)findViewById(R.id.your_view_id);
Easiest way to add by inflating View. Here some small snippet where you can find reference.
private View viewText;
private TextView txtPost;
private void setViewFlipperPost(String postData, String postType) {
if (postType.toLowerCase().toString().equals("text")) {
viewText = LayoutInflater.from(mContext).inflate(R.layout.activity_full_screen_textpost, null, false);
viewText.setTag(TAG_TEXT);
txtPost = (TextView) viewText.findViewById(R.id.txtTextPostFullScreenText);
txtPost.setText(postData);
viewFlipper.addView(viewText);
}
}
use viewflipper
<?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:background="#drawable/white"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="410dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="first layout"
android:textColor="#845965"
android:textSize="25dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="second layout"
android:textColor="#654123"
android:textSize="25dp"
android:textStyle="bold" >
</TextView>
</LinearLayout>
//you can add many layout here
</viewFlipper>
<Button
android:id="#+id/flipbyclickNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bn1"
android:onClick="flipByClickNext" />
<Button
android:id="#+id/flipbyclickprevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="flipByClickPrevious"
android:background="#drawable/bp1" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ViewFlipper viewFlipper;
//Button next,prev;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
}
public void flipByClickNext(View v)
{
if(viewFlipper.isFlipping())//Checking flipper is flipping or not.
{
viewFlipper.stopFlipping(); //stops the flipping .
}
viewFlipper.showNext();//shows the next view element of ViewFlipper
}
public void flipByClickPrevious(View v)
{
if(viewFlipper.isFlipping())//Checking flipper is flipping or not.
{
viewFlipper.stopFlipping(); //stops the flipping .
}
viewFlipper.showPrevious();
}
}

Why is Android TabHost stealing focus from a TextView?

I have an application using the following layout :
alt text http://img15.imageshack.us/img15/238/screenshot003xbo.png
When the app starts, the focus is on the first TextView, but if you try to type any letter in it, the focus goes directly to the tabs. It seems that I am not the only one fighting with this issue, and maybe this is related to the following:
http://groups.google.com/group/android-developers/browse_thread/thread/435791bbd6c550a/8022183887f38f4f?lnk=gst&q=tabs+focus#8022183887f38f4f
Anyway, have you any idea of why that happens? And of course, any workaround would be appreciated.
I post the code below to avoid overloading the question :
The XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:padding="5px"
android:orientation="vertical"
android:id="#+id/task_edit_panel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" >
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title"
android:textStyle="bold" />
<EditText android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<TabHost android:id="#+id/edit_item_tab_host"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65px"> <!-- you need that if you don't want the tab content to overflow -->
<LinearLayout
android:id="#+id/edit_item_date_tab"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5px" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="date"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/edit_item_geocontext_tab"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5px" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lieu"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/edit_item_text_tab"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5px">
<EditText android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" />
</LinearLayout>
</FrameLayout>
</TabHost>
</LinearLayout>
<!-- Bottom pannel with "add item" button -->
<LinearLayout
android:padding="5px"
android:orientation="horizontal"
android:layout_weight="1"
android:id="#+id/task_edit_panel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E7E7E7" >
<!-- Let the height set to fill_parent until we find a better way for the layout -->
<Button android:id="#+id/item_edit_ok_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/ok"
style="?android:attr/buttonStyleSmall"
android:layout_weight="1" />
<Button android:id="#+id/item_edit_cancel_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/cancel"
style="?android:attr/buttonStyleSmall"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
And the Java code :
TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);
// don't forget this setup before adding tabs from a tabhost using a xml view or you'll get an nullpointer exception
tab_host.setup();
TabSpec ts1 = tab_host.newTabSpec("TAB_DATE");
ts1.setIndicator(getString(R.string.when), getResources().getDrawable(R.drawable.ic_dialog_time));
ts1.setContent(R.id.edit_item_date_tab);
tab_host.addTab(ts1);
TabSpec ts2 = tab_host.newTabSpec("TAB_GEO");
ts2.setIndicator(getString(R.string.where), getResources().getDrawable(R.drawable.ic_dialog_map));
ts2.setContent(R.id.edit_item_geocontext_tab);
tab_host.addTab(ts2);
TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT");
ts3.setIndicator(getString(R.string.what), getResources().getDrawable(R.drawable.ic_menu_edit));
ts3.setContent(R.id.edit_item_text_tab);
tab_host.addTab(ts3);
tab_host.setCurrentTab(0);
Well, definitly a bug : http://code.google.com/p/android/issues/detail?id=2516.
Hope it will be fixed in the next release because this is a really anoying issues for complex apps with an advanced layout.
I was facing the same kind of issue with HoneyComb but found a very simple solution. So I thought it can be helpful for someone to share my experience. Everytime when a tab is clicked, the focus is on Edit Text and you can actually type with hard keyboard but soft keyboard never pops up. Even with below code, it didnt solve the issue:
input1.requestFocusFromTouch();
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input1, InputMethodManager.SHOW_FORCED);
Solution:
In the main class where you are handling Tabs, just write below Java code:
myTabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabID) {
myTabHost.clearFocus();
}
});
If it isn't related to that other bug you pointed out (and it looks like it probably is) and if you're experiencing this on on of the G1s (and not the emulator) I suspect your problem is that the focus either isn't being frozen before the orientation change (when you fold out the keyboard and go into landscape mode) or unfrozen after the view is recreated. You could try Overriding the activity's onSaveInstanceState() and onRestoreInstanceState() methods to save/restore the focus.
this example show how to fix this issue:
userNameEditText.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
userNameEditText.requestFocusFromTouch();
return false;
}
});
after adding all the specs add requestFocus method to your edittext in java file

Categories

Resources