Button not displayed on emulator or on Device - android

I am a beginner on Android. I have created 2 activities. First activity has 1 button (say Button1) and 1 TextView (say TextView1) and the Second activity has 1 TextView (say TextView2). The text value for TextView in 1st activity is "Hello World!", when the Button1 is pressed opens up 2nd Activity passing the value of the TextView1 (i.e. "Hell World!"). The 2nd Activity assigns the obtained value to the TextView2 and displays.
All of this is working fine.
When I add a new Button (say Button2) to the 1st activity and write a onClick() method to change the text of TextView1 from "Hello World!" to "Hello Android!" and vice versa. Now when I run this on my device or emulator I cannot see this added button (Button2) it still works as earlier when I click Button1, but I cannot see Button2.
Can anyone help me on this ?
XML Layout : MainActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/ourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginRight="37dp"
android:onClick="buttonClick"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</RelativeLayout>

It's because you have android:layout_below="#+id/textView" twice. Try this instead:
<!-- below the previous view : ourButton -->
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textView"
android:layout_below="#id/ourButton"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
Also, don't use #+id/ for align your layout. You use it right when you add an id but when the id is created, you just have to align on it as android:layout_toLeftOf="#id/prevId" with #id.
Example:
Here the layout:
|---- text -----|
<- both are below text
|---- view1 ----|| <- view align right
view align left -> ||---- view2 ----|
As you can see, the left and right alignment are on the "border" of the TextView because you set an alignment below the TextView. So it looks like this:
|------ text ------|
<- both are below text
view1 align left -> ||---- view 1&2 ----|| <- view2 align right
Your views are overlapping each other. If you want to have the two buttons below the text and side by side, try to add a container, like this following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/ourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:onClick="buttonClick"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</LinearLayout>
</RelativeLayout>
Hope this helps.

Please try to remove
android:layout_marginRight="37dp"
and then try. Are you able to see output on xml graphical layout ? When I tried this on my xml I am able to see in Graphical Layout(I used the same code which you mentioned along with your question).

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/ourButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginRight="37dp"
android:onClick="buttonClick"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ourButton"
android:layout_marginLeft="50dp"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</RelativeLayout>

// try this way
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="#+id/ourButton"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:onClick="buttonClick"
android:layout_marginRight="5dp"
android:text="#string/button_string" />
<Button
android:id="#+id/ourButton2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:focusable="false"
android:onClick="buttonClick2"
android:text="#string/button_string2" />
</LinearLayout>
</LinearLayout>

Related

Custom View with text view is not coming

I am inserting custom view inside a relative layout. The action bar turning into white color
Here is the xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dark_tablet_converted"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<com.test.myview
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp" />
<TextView
android:id="#+id/text1"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:text="text : "
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
and here is the image.
I am not getting where is the problem.
Android support library used
'android support appcompat-v7:23.0.1'
'android.support v4:23.0.1'
targetSdkVersion 23
compileSdkVersion 23
buildToolsVersion '23.0.1'
EDIT:
Whatever placed above my view will get displayed. only if any layout/view is placed below my customview will get vanished.
your views are child of relative layout
so, they are drawed one on the other
and you can't really decide who will be in foreground and who will be in background
add a framelayout as parent of your custom and text view
then, in your code, find your textview and call "bringToFront"
try this,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dark_tablet_converted"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<com.test.myview
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_gravity="center"
android:padding="8dp" />
<TextView
android:id="#+id/text1"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_below="#+id/view"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:text="text : "
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>

Android Listview with upvote button on left

I'm trying to make a list, much like in reddit, where there is a upvote button on the left and some text to the right of that (a title, user who asked, number answers).
I'm trying to make a list containing viewgroups like so:
I haven't been able to add that Upvote button on the left side of the image.
Whenever I add a button there, I can no longer click on the list item itself (previously if you clicked on the list item it would take you to another intent)
How can I create a list, where each item of the list is clickable to go to an intent based off that row item, but also have an upvote button for each row item of the list?
Here is what I am currently at:
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="2dip"
android:paddingBottom="2dip"
>
<Button
android:layout_width="16dip"
android:layout_height="8dip"
android:layout_marginLeft="12dip"
android:layout_marginRight="12dip"
android:layout_marginTop="4dip"
android:background="#drawable/vote_up_gray"
android:id="#+id/vote_up_button"/>
<TextView
android:layout_width="40dip"
android:layout_height="32dip"
android:layout_alignParentLeft="true"
android:layout_below="#id/vote_up_button"
android:singleLine="true"
android:ellipsize="marquee"
android:gravity="center"
android:textSize="14sp"
android:textStyle="bold"
android:text="0"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/Q_list_descriptions">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/title_text_view"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/askedBy_text_view" />
<TextView
android:layout_width="fill_parent"
android:gravity="right"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/answers_text_view" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Use RecyclerView instead. Never had such problems with it. You can specify OnClickListener for each item in your list_item, and also for the list_item as a whole, inside a ViewHolder in your adapter.
https://www.codexpedia.com/android/defining-item-click-listener-for-recyclerview-in-android/

Adding margins to listview

I am following a tutorial and I did not understand why exactly the margins that I applied to a list view contained within a relative layout does not affect the look when I test it on a device. However, if I put everything in a LinearLayout container it looks as it should. I don't understand why the layout does not work without this LinearLayout.
This is my code in layout for my listView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/inbox_horizontal_margin"
android:layout_marginRight="#dimen/inbox_horizontal_margin"
android:layout_marginTop="#dimen/inbox_vertical_margin"
android:background="#android:color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageIcon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/message_list_item_vertical_margin"
android:paddingBottom="#dimen/message_list_item_vertical_margin"
android:layout_alignParentStart="true"
android:contentDescription="#string/content_desc_message_icon"
android:src="#drawable/ic_action_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/senderLabel"
android:layout_centerVertical="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_toRightOf="#+id/messageIcon"
android:layout_toEndOf="#+id/messageIcon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" created at: 03.17.15"
android:id="#+id/createdAtLabel"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/senderLabel"
android:layout_toEndOf="#+id/senderLabel"/>
</RelativeLayout>
</LinearLayout>
This is the result of the code:
this is the code without the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/inbox_horizontal_margin"
android:layout_marginRight="#dimen/inbox_horizontal_margin"
android:layout_marginTop="#dimen/inbox_vertical_margin"
android:background="#android:color/white">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/messageIcon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/message_list_item_vertical_margin"
android:paddingBottom="#dimen/message_list_item_vertical_margin"
android:layout_alignParentStart="true"
android:contentDescription="#string/content_desc_message_icon"
android:src="#drawable/ic_action_picture"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/senderLabel"
android:layout_centerVertical="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_toRightOf="#+id/messageIcon"
android:layout_toEndOf="#+id/messageIcon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=" created at: 03.17.15"
android:id="#+id/createdAtLabel"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/senderLabel"
android:layout_toEndOf="#+id/senderLabel"/>
</RelativeLayout>
And this is the result of this layout:
Margin is outside a view , ListView uses AbsListView.LayoutParams by default, which doesn't include any margin support, just the height and width, thats why, it simply ignores the params value for margins. Try using padding for your relative layout instead

Multiple lines in a android linear layout

I'm fiddling around with writing android apps and what I'm currently trying to do is to have a text box with a send button at the top of the page and a black button bar at the bottom.
I understand from this question that the correct way to do this would be to write something like this:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_height="wrap_content"
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:hint="#string/edit_message"
android:layout_gravity="top|center"/>
<Button
android:layout_width="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
<LinearLayout android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="#android:style/ButtonBar">
<Button android:id="#+id/saveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/menu_done" />
<Button android:id="#+id/cancelButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/menu_cancel" />
</LinearLayout>
</LinearLayout>
but the black bar and the text box just end up on top of each other :(
I'm not sure if these are cut-and-past errors, but I noticed a few things in the xml. The first nested linear layout is missing a '>' before the EditText and doesn't specify a layout_height. And the first button also doesn't have a layout_height.

How to stop layout from taking all width

I have this activity and I need to stop filling all width of textview and button. Currently these are shown in centre but filling all width. How to stop that and make width fixed? Please see layout below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="1280dp"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Login" >
<TextView
android:id="#+id/lblUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:text="#string/UserName" />
<EditText
android:id="#+id/txt4UserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblUserName"
android:layout_below="#+id/lblUserName"
android:ems="10"
android:inputType="text|textFilter|textNoSuggestions" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/lblPassword"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblUserName"
android:layout_below="#+id/txt4UserName"
android:layout_marginTop="20dp"
android:text="#string/Password" />
<EditText
android:id="#+id/txt4Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblPassword"
android:layout_below="#+id/lblPassword"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/btnForLogin"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txt4Password"
android:layout_alignRight="#+id/txt4Password"
android:layout_below="#+id/txt4Password"
android:layout_marginTop="30dp"
android:maxLength="50"
android:onClick="Verification"
android:text="#string/Login" />
</RelativeLayout>
Required highlighted in red
change width of your parent layout to wrap_content, if it doesn't change anything then try to put some margin to left and right of your parent layout as your choice
<RelativeLayout
...
android:layout_width="wrap_content"
...>
or
<RelativeLayout
...
android:layout_marginLeft="someValue"
android:layout_marginRight="someValue"
...>
You have mentioned
android:layout_width="1280dp"
in your relative layout
set it to
android:layout_width="wrap_content"
and in both the edittexts you have specified layout_width as fill_parent .. I think this is causing the problem
try setting edittext's width to whatever you want or simply to wrap_content
and then your layout will not occupy whole screen
Change your EditText's width to wrap_content & your RelativeLayout's width to match_parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Login" >
<EditText
android:id="#+id/txt4UserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblUserName"
android:layout_below="#+id/lblUserName"
android:ems="10"
android:inputType="text|textFilter|textNoSuggestions" >
<EditText
android:id="#+id/txt4Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/lblPassword"
android:layout_below="#+id/lblPassword"
android:ems="10"
android:inputType="textPassword" />
I think you could use
android:layout_centerInParent="true"
in your relative layout

Categories

Resources