Cannot Click Button on App - android

When I launch my app, I go to one of my screens to test out my game. Though, when I go to tap on the buttons, they don't work and do not change images like I wanted them to. So after various amounts of googling and testing, I believe I came across the problem. I believe I just have my text layer on top of my button layer, causing it so the buttons cannot be clicked as they are not the top layer. My issue here is I need the text on top to change constantly. How do I make it so my text does not interfere with my buttons.
I apologize for the big amount of code. The buttons are in the third relative layout, and the text is all in the middle.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:background="#drawable/backgroundupg" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal" android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#drawable/upgrades"
android:layout_gravity="center_horizontal" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal" android:layout_weight=".5">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state2_xml"
android:id="#+id/upgbutton"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state3_xml"
android:id="#+id/upgbutton1"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state4_xml"
android:id="#+id/upgbutton2"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/selector_state5_xml"
android:id="#+id/upgbutton3"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="8">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext"
android:text="Accuracy"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext"
android:text="100"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext"
android:text="Eh, close enough."
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext2"
android:text="Base Cookies"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext2"
android:text="500"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:gravity="center_vertical"
android:layout_alignRight="#+id/upgtext2"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext2"
android:text="Double the cookies? Sweet!"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext3"
android:text="Stopping Time"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext3"
android:text="1000"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:gravity="center_vertical"
android:layout_alignRight="#+id/upgtext3"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext3"
android:text="I save an extra second!"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/upgtext4"
android:text="Secret"
style="#style/Base.TextAppearance.AppCompat.Large"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight=".5"
android:gravity="center_vertical|right"
android:id="#+id/pricetext4"
android:text="250 000"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.5"
android:id="#+id/upg1cost"
android:gravity="center_vertical"
android:layout_alignRight="#+id/upgtext4"
android:background="#drawable/cookie" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:id="#+id/bottomtext4"
android:text="End game?!?!"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center_horizontal" android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
</FrameLayout>
I also included an image to help give a visual representation of my code.
As well as my selector state for those who would like to know exactly what I put in it
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button" android:state_enabled="true"></item>
<item android:drawable="#drawable/buttonclick" android:state_pressed="true"></item>
<item android:drawable="#drawable/buttongray" android:state_enabled="false"></item>
</selector>
Button is used when the user has enough cash to purchase the upgrade.
Buttongray is used when the user does not have enough cash, I do not want the button to be pressable then. The Button color just turns gray.
Buttonclick is just a highlight of Button.
I have tried interchanging both states as I thought that was the initial problem. Neither can be pressed.

the problem is because you are having your TextView objects above your Button objects and they steal the click event which you intend for the Button. The easiest fix would be to set:
android:focusable="false"
for all the TextView objects that overlay Button objects.
But I would still say your coding style needs to be improved. Why not dynamically change text in code based on events that happen in run time. You can use the setText(CharSequence text) method for buttons to dynamically set the text they should display.

You can use Button's attribute android:text="TEXT_HERE" in xml to set initial text for your button and later on with on click you can set a different text by this
Button button = (Button)findViewById(R.id.upgbutton);
button.setText("New_TEXT");
you button code in XML will look like this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text = "Accuracy"
android:background="#drawable/selector_state2_xml"
android:id="#+id/upgbutton"
android:layout_gravity="center_horizontal" />

Since each "button" is so complex, you should consider creating a custom view.

Related

Make FrameLayout element scrollable

I am working on adapting an app which was designed for a larger vertical screen dimension (1200px) to a device with a smaller size (vertical=800px). I've managed to get things displaying properly by editing the layout XML files--except for one thing. As you can see, the crossword puzzle is cut off at the bottom. Its original dimensions in the layout were 570x570px. At that size, the lower part of the puzzle ended up peeking out from behind the keyboard.
After resizing both the keyboard and the clue drop-down at the top of the screen so they work properly, I'm left with a vertical space of 390px for the puzzle. Absent any other ideas, I've been trying to implement scrolling in that FrameLayout, following several examples such as Scrollable FrameLayout.
I can't seem to get it to work. My knowledge of layout is sketchy. Can anyone suggest a solution or a different approach?
(the puzzle layout on the screen is given in line 6)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:gravity="bottom|center" android:orientation="vertical" android:background="#color/root_background" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout android:gravity="top|center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0">
<FrameLayout android:gravity="top|center" android:orientation="vertical" android:id="#id/field_container" android:paddingTop="160.0px" android:layout_width="600.0px" android:layout_height="wrap_content">
<com.encore.crossword.view.CrosswordView android:layout_gravity="top|center" android:id="#id/crosswordView" android:layout_width="570.0px" android:layout_height="390.0px" />
<LinearLayout android:gravity="center" android:id="#id/difficulty_layout" android:paddingTop="146.0px" android:visibility="invisible" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:layout_gravity="center" android:orientation="vertical" android:background="#drawable/c_dialog_box" android:paddingLeft="30.0px" android:paddingTop="30.0px" android:paddingRight="30.0px" android:paddingBottom="20.0px" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:textAppearance="#style/DialogHeader" android:gravity="center" android:id="#id/TextViewGameOver" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Game Difficulty" />
<RadioGroup android:gravity="left|center" android:orientation="vertical" android:id="#id/RadioGroupDifficulty" android:paddingTop="6.0px" android:paddingBottom="6.0px" android:layout_width="fill_parent" android:layout_height="wrap_content">
<RadioButton android:textAppearance="#style/ButtonTextMain" android:textColor="#color/button_radio" android:id="#id/RadioButtonEasy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:button="#drawable/sudoku_radio_button" android:text="#string/easy" android:width="160.0px" />
<RadioButton android:textAppearance="#style/ButtonTextMain" android:textColor="#color/button_radio" android:id="#id/RadioButtonMedium" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="#drawable/sudoku_radio_button" android:text="#string/medium" android:width="160.0px" />
<RadioButton android:textAppearance="#style/ButtonTextMain" android:textColor="#color/button_radio" android:id="#id/RadioButtonHard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="#drawable/sudoku_radio_button" android:text="#string/hard" android:width="160.0px" />
<RadioButton android:textAppearance="#style/ButtonTextMain" android:textColor="#color/button_radio" android:id="#id/RadioButtonExtreme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="#drawable/sudoku_radio_button" android:text="#string/themeless" android:width="160.0px" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
</FrameLayout>
<LinearLayout android:gravity="top|center" android:orientation="vertical" android:id="#id/clue_container" android:background="#drawable/cross_top_dialog_box" android:paddingLeft="5.0px" android:paddingRight="5.0px" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:gravity="top|center" android:orientation="vertical" android:id="#id/mini_clue_container" android:paddingLeft="5.0px" android:paddingRight="5.0px" android:visibility="visible" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0">
<LinearLayout android:id="#id/ClueTop1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:textAppearance="#style/ClueInfoText" android:id="#id/TextViewClueWordNumber1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1.0" />
<TextView android:textAppearance="#style/ClueInfoText" android:gravity="right" android:id="#id/TextViewClueWordLength1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1.0" />
</LinearLayout>
<TextView android:textAppearance="#style/ClueText" android:id="#id/TextViewClue1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10.0px" android:text="" android:lines="2" />
<LinearLayout android:id="#id/ClueTop2" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:textAppearance="#style/ClueInfoText" android:id="#id/TextViewClueWordNumber2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1.0" />
<TextView android:textAppearance="#style/ClueInfoText" android:gravity="right" android:id="#id/TextViewClueWordLength2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:layout_weight="1.0" />
</LinearLayout>
<TextView android:textAppearance="#style/ClueText" android:id="#id/TextViewClue2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10.0px" android:text="" android:lines="2" />
</LinearLayout>
<LinearLayout android:gravity="top|center" android:orientation="vertical" android:id="#id/big_clue_container" android:paddingLeft="0.0px" android:paddingRight="0.0px" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0">
<TextView android:textAppearance="#style/ClueTextBold" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10.0px" android:text="ACROSS" />
<LinearLayout android:gravity="top|center" android:orientation="vertical" android:id="#id/big_clue_across_container" android:background="#drawable/c_dialog_box" android:paddingTop="17.0px" android:paddingBottom="17.0px" android:layout_width="fill_parent" android:layout_height="200.0px">
<ListView android:scrollbarStyle="outsideOverlay" android:id="#id/big_clue_across_list" android:paddingLeft="5.0px" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<TextView android:textAppearance="#style/ClueTextBold" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10.0px" android:text="DOWN" />
<LinearLayout android:gravity="top|center" android:orientation="vertical" android:id="#id/big_clue_down_container" android:background="#drawable/c_dialog_box" android:paddingTop="17.0px" android:paddingBottom="17.0px" android:layout_width="fill_parent" android:layout_height="200.0px">
<ListView android:id="#id/big_clue_down_list" android:paddingLeft="5.0px" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ImageView android:id="#id/ClueContainerArrow" android:background="#drawable/cross_down_arrow" android:layout_width="30.0px" android:layout_height="20.0px" android:layout_marginBottom="10.0px" android:layout_weight="1.0" />
</LinearLayout>
<LinearLayout android:gravity="bottom" android:layout_gravity="bottom|left|center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<include android:id="#id/bottom_layout" android:layout_width="fill_parent" android:layout_height="160.0px" layout="#layout/bottom_layout" />
</LinearLayout>
<include android:id="#id/bottom_menu" android:layout_width="fill_parent" android:layout_height="fill_parent" layout="#layout/bottom_menu" />
</FrameLayout>
<LinearLayout android:gravity="center" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50.0dip">
<LinearLayout android:gravity="center" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0">
<Button android:textAppearance="#style/ButtonText" android:textColor="#color/solid_white" android:id="#id/GameButton" android:background="#drawable/btn_footer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2.0dip" android:layout_marginRight="0.0dip" android:text="Game" android:layout_weight="1.0" />
<ImageView android:id="#id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/footer_line_divider" />
<Button android:textAppearance="#style/ButtonText" android:textColor="#color/solid_white" android:id="#id/LastWordButton" android:background="#drawable/btn_footer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0.0dip" android:layout_marginRight="0.0dip" android:text="Prev Word" android:layout_weight="1.0" />
<ImageView android:id="#id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/footer_line_divider" />
<Button android:textAppearance="#style/ButtonText" android:textColor="#color/solid_white" android:id="#id/NextWordButton" android:background="#drawable/btn_footer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="2.0dip" android:layout_marginRight="0.0dip" android:text="Next Word" android:layout_weight="1.0" />
<ImageView android:id="#id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/footer_line_divider" />
<Button android:textAppearance="#style/ButtonText" android:textColor="#color/solid_white" android:id="#id/KeyboardButtonHint" android:background="#drawable/btn_footer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0.0dip" android:layout_marginRight="0.0dip" android:text="Hint" android:layout_weight="1.0" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Best way to do the same than you can do with nested linearlayouts

I have to create a windows8 like screen. I create it easily using nested linearlayouts. I know from a performance point of view it is not a good idea be having nested linearlayouts, so I want to ask you which other way I can do that, which scale well between portrait and landscape and have a better performance. Even because really each textview in the next xml code will be really implemented with another linearlayout, because it have to include text and icons. Sadly I am new in stackoverflow so I can publish images
<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/bg1_blurred"
android:gravity="center"
tools:context="com.lm2a.w8opt.MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="300dp"
android:layout_height="200dp"
android:orientation="vertical"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:orientation="horizontal"
android:weightSum="5" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:textColor="#color/txt_color_white"
android:textSize="#dimen/text_big"
android:background="#color/square_color_red"
android:padding="10dp"
android:text="T" />
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:layout_weight="4"
android:background="#00000000"
android:text="W" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="horizontal"
android:weightSum="5" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:layout_weight="1"
android:background="#color/square_color_yellow"
android:text="X" />
<TextView
android:id="#+id/textView4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:layout_weight="2"
android:background="#color/square_color_blue"
android:text="Y" />
<TextView
android:id="#+id/textView5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:layout_weight="2"
android:background="#color/square_color_green"
android:text="Z" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

How to merge Linear Layout and Table Layout at android?

I'm new android. I have a little bit stack in designing layout for Android. In my scenario, I have two button with Linear Layout. Here my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#color/isp_home_color"
>
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="#string/sync_text"
android:background="#color/sync_text_color"
android:id="#+id/btn_syncpin"
android:layout_gravity="left"
android:textStyle="bold"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:text="#string/topup_text"
android:background="#color/topup_text_color"
android:id="#+id/btn_topup"
android:layout_gravity="right"
android:textStyle="bold"/>
<!--Table Layout will come here -->
</LinearLayout>
Here image for this.
When I add new TableLayout inside Linear Layout. I got this.
Here my xml for Table Layout.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</TableLayout>
Actually, I don't want like this. I want two button as shown in first image. Then, next table layout will occur under this two button. Please help me how can I do this layout. Please pointing to me how to make this. Thanks with advanced.
Give orientation as vertical to the LinearLayout not the TableLayout.Put Two buttons in a LinearLayout with orizontal orientation and give orientation "vertical" for the parent LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#456789"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">
<Button
android:id="#+id/btn_syncpin"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#123456"
android:text="tring/"
android:textStyle="bold" />
<Button
android:id="#+id/btn_topup"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#978969"
android:text="topup_text"
android:textStyle="bold" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</TableLayout>
</LinearLayout>
Check this out, I just checked it on my Machine and it works as desired, although the background preference and the display strings have been removed.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_syncpin"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="left"
android:layout_weight="1"
android:textStyle="bold" />
<Button
android:id="#+id/btn_topup"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="right"
android:layout_weight="1"
android:textStyle="bold" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/linearLayout2">
</TableLayout>
</RelativeLayout>
take Relative Layout as a parent layout.
Following is an example. it will display your buttons and table layout too
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/isp_home_color"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_syncpin"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="left"
android:layout_weight="1"
android:background="#color/sync_text_color"
android:text="#string/sync_text"
android:textStyle="bold" />
<Button
android:id="#+id/btn_topup"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#color/topup_text_color"
android:text="#string/topup_text"
android:textStyle="bold" />
</LinearLayout>
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/linearLayout2" >
</TableLayout>
</RelativeLayout>
thanks
This answer tells only implementation of button_bar_layout for all android versions with buttons at the bottom/top of an activity which look like AlertDialog with OK/Cancel buttons.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/listDivider"
android:orientation="vertical"
android:showDividers="middle" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:divider="?android:attr/listDivider"
android:dividerPadding="12dp"
android:orientation="horizontal"
android:showDividers="middle" >
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:drawable/list_selector_background"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="One" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:drawable/list_selector_background"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Two" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</TableLayout>
</LinearLayout>
Credits: Who else ? Johnkil
// try this way here i gave you alternative to achieve your requirement from give all of above
1. header.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="wrap_content"
android:orientation="horizontal"
android:background="#color/isp_home_color">
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="#string/sync_text"
android:background="#color/sync_text_color"
android:id="#+id/btn_syncpin"
android:textStyle="bold"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:text="#string/topup_text"
android:background="#color/topup_text_color"
android:id="#+id/btn_topup"
android:textStyle="bold"/>
</LinearLayout>
2.main.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:padding="5dp"
android:orientation="vertical">
<include
layout="#layout/header"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1">
</TableLayout>
</LinearLayout>

RelativeLayout over ListView, onclick not detected when RealtiveLayout is shown as

I am currently a relative layout is shown in a listview. Turns out that when I click RelativeLayout, is always caught the click event in the list view.
I wanted to detect a gesture on the layout when it is being shown, but when I click, the item is selected from the listview that is underneath.
Anyone know a solution to this problem?
<TabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/start_tabs_topbar" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="0dp" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="0dp" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
<RelativeLayout
android:id="#+id/playertab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back_artista"
android:gravity="center_vertical|left"
android:paddingLeft="15dp" >
<LinearLayout
android:id="#+id/music_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity=""
android:orientation="horizontal" >
<ImageView
android:id="#+id/artist_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="9dp"
android:layout_marginTop="4dp"
android:src="#drawable/placeholder" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/playlist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="playlist_name"
android:textColor="#3BBEF9"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="#+id/artist_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="artist_album"
android:textColor="#EEEEEE"
android:textSize="11dp" />
<TextView
android:id="#+id/artist_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="artist_album"
android:textColor="#EEEEEE"
android:textSize="11dp" />
<TextView
android:id="#+id/artist_song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="artist_song"
android:textColor="#EEEEEE"
android:textSize="11dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
The listview as been inserted into:
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="0dp" />
If you are using GestureDetector try calling mDetector.setIsLongpressEnabled(true); first and set view long clickable via convertView.setLongClickable(true);

LinearLayout usage

What I just want to do it to put some texts in the screen, together with a border that I'm doing(this border will be in the entire screen.)
The problem is that the LinearLayout is adding some texts to outside of the screen, and this I cannot understand why. Why the LinearLayout cannot just keep the elements in the screen size, even if will be very small sizes.
Take a look:
<?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" android:background="#color/White">
<ImageView android:layout_height="wrap_content"
android:minHeight="2px" android:maxHeight="2px"
android:layout_gravity="top" android:src="#drawable/horizontal_bar"
android:scaleType="fitXY" android:layout_weight="1"
android:layout_width="fill_parent" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView android:src="#drawable/vertical_bar"
android:minWidth="2px" android:maxWidth="2px" android:scaleType="fitXY"
android:layout_gravity="left" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
<TextView android:text="testing" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:paddingLeft="15px"
android:textStyle="bold" android:textColor="#color/grey"
android:textSize="20px" android:layout_weight="1"
android:layout_gravity="center_horizontal|center" />
<TextView android:text="blablabla" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:paddingLeft="15px"
android:textStyle="bold" android:textColor="#color/grey"
android:textSize="20px" android:layout_weight="1"
android:layout_gravity="center_horizontal|center" />
<TextView android:text="0000" android:id="#+id/number"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_gravity="left" android:textStyle="bold"
android:textColor="#color/green" android:textSize="20px"
android:layout_weight="1" />
<ImageView android:src="#drawable/vertical_bar"
android:minWidth="2px" android:maxWidth="2px" android:scaleType="fitXY"
android:layout_gravity="right" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>
<ImageView android:layout_height="wrap_content"
android:minHeight="2px" android:maxHeight="2px"
android:layout_gravity="top" android:src="#drawable/horizontal_bar"
android:scaleType="fitXY" android:layout_weight="1"
android:layout_width="fill_parent" />
</LinearLayout>
Where horizontal_bar is a image with 6x4 and vertical_bar is a image with 4x6.
This image needs to be stretch to be able to became a border.
Any idea why the LinearLayout is going out of the screen size?
Your
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
should have vertical orientation.

Categories

Resources