I've set up a ListView and assigned a custom layout to make the ListView items better looking. However, I'm looking for a way to add a button that would appear with each element in it's top right corner, like Youtube's videos: they all have that little options button with things like add to, share, etc. The problem is that you cant put a widget inside of a widget, at least I don't know a way to do it. The custom layout contains a TextView which gives the ListView items the looks. Can someone suggest a way on how should I approach this?
Use FrameLayout for your custom ListView items. like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<Button android:id="#+id/button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
NB - when widgets overlaps in layout, last written widget will be topmost.
Here is a link where you will find what you want.
It shows you how to add button in list view.
You will need to change some XML code and adjust the onClickListeners but it's similar to what you are looking for.
Related
I want to ask, can I add next and back button under scrollview that filled with textview and imageview in one screen, both with it's xml, java and screenshot result for android?
Here's the picture I intend to ask.
I got from here: https://sensortower.com/android/nl/softartstudio/app/selfishop-camera/com.selfishop.camera/
enter image description here
Where actually, what I really aim is a view like a blog (when you see an image on your above text that can be scrolled, where it's end with Back button that will go to previous page on the bottom left, and Next button that will go to the next page at the reverse side).
Really would like an example in xml, java, and it's screenshot result.
Again, thank you very much
Very appreciate your answer. Thank you
This is what XML is for.
Create your layout in xml and import it via java.
You will want to create your own "bar layout" for the bars in the center, though you could I suppose, fill 3 separate LinearLayouts to achieve the same effect, making a separate layout and putting in it here will be easier for you to read, and follow along with what is happening.
Below is a mock up of the xml layout you would be using to get a view very similar to what you have in your picture. I have not tested this, this was made on the fly so it may not be perfect but it will get you very close to what you are looking for.
<LinearLayout>
android:orientation="vertical"
android:weightSum=10
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout>
android:weight=1
android:layout_height=0dp
android:layout_width="match_parent">
<TextView>
android:width="wrap_content"
android:id="#+id/info_bar"
android:centerHorizontal="true"/>
</RelativeLayout>
<LinearLayout>
android:weight=8
android:width="match_parent"
android:height=0dp>
<TextView>
android:id="#+id/text_view1"
android:width="match_parent"
android:height="wrap_content"/>
<com.yourpackage.yourapp.createabarlayout>
</com.yourpackage.yourapp.createabarlayout>
<com.yourpackage.yourapp.createabarlayout>
</com.yourpackage.yourapp.createabarlayout>
<com.yourpackage.yourapp.createabarlayout>
</com.yourpackage.yourapp.createabarlayout>
</LinearLayout>
<RelativeLayout>
android:weight=1
android:layout_width="match_parent"
android:layout_height=0dp>
<Button>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/back_button"
android:text="#string/back_button_hint"/>
<TextView>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/steps_textview"
android:text="#string/steps_hint"
android:centerHorizontal="true"/>
<Button>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/next_button"
android:text="#string/next_button_hint"/>
</RelativeLayout>
</LinearLayout>
I am fairly new to Android development.
I want to create a screen that has a horizontal scroll view of buttons (group names). Every time I click a group name, it will display the members below these buttons (I assume I will have to create a fragment with a listView of members. I haven't gotten to the last part. I was able to create a horizontal scroll view in XML and add 6 button to the XML file. But what I want to do is use Java to find the number of groups every time onCreate is called, so it can create the appropriate number of buttons based on groups. (Group names are currently stored in a temporary ArrayList (for testing purposes) and I use arrayList.size() to get the number of buttons needed to be created. The difficulty I am having is figuring out how to create these buttons inside the LinearLayout inside the HorizontalScrollView inside the RelativeLayout. I messed around with creating one layout adding a button, but I don't know how to create nested layouts, or access the LinearLayout in which I want to create my buttons using Java. Here is my XML file (I created one button for testing. That button location is where I want to create my buttons using Java. I don't want to have anything written there in XML if possible. Everytime a new group is created, a button has to be created):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFBB00">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/buttonList">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me 1"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
How do I add buttons using my Java class in this LinearLayout?
Use this link how to add button dynamically in android? and there is another way doing this by using loop, Read this article for making button using loops
Since you are talking about dynamic content, you might want to try something like a horizontial list view and create an adapter instead of creating a bunch of buttons at runtime. It will be a bit slower and more resource intensive to do it that way.
Here is an example using RecyclerView:
How to build a Horizontal ListView with RecyclerView?
I have a list view in an Android application used to display an ArrayList containing Strings and I can't find any properties of the ListView that would allow me to align the text of the ListView Items to the right of it instead of the left. Any help would be great, thanks.
The answer depends on how exactly do you build the list. If you use the the standard ArrayAdapter then you could use the constructor:
ArrayAdapter<String>(Context, R.layout.aligned_right, values);
where R.layout.aligned_right is a xml layout like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="right"/>
This is because these parameters are not defined in the ListView, but in the ListAdapter instead. When you specify an adapter for the list, you should set it to return such a layout which aligns items on the right.
Add the below to the layout
android:layoutDirection="rtl"
This should suffice
android:layout_gravity="right"
For Expandable list view we create two row XMLs namely
One is group_row.xml and Other is child_row.xml.
make group_row.xml as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/row_group_name"
android:layout_width="wrap_content"
android:textSize="24sp"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:layout_height="wrap_content" />
That is, a Text view aligned to parentRight inside a Relative Layout (which is match parent).
After 2 hours of struggling, I achieved this a moment ago.
image after
Pay attention to one small thing.
If you use listitem inside the listview, the gravity attribute of the listitem might not work if the listview layout:width is not set to match_parent.
I have been working on an Android app and I want to display a multicolumn ListView in Android showing text, image and Radio button. Though I have been reading few blogs which offered me choice to use GridView instead or Multiple ListViews,I am still in doubt what shall i use.
Can anyone give me a hint on how to implement the same. May be some references be helpful too.
for multiple column into listview you need to customize the listview and need to create you layout that you have mention
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/list_item_text" android:text="item text" android:weight="1"/>
<ImageView android:layout_width="50dp" android:layout_height="50dp" android:id="#+id/list_image" />
<RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
you custom view code like this but in this for radio button you have to check the by coding and to maintain the radio button state for your list. Because in listview 1 row has only 1 radio button so you cannot maintain with the radio group button.
now you need to create the custom adapter ok here is the link for listview check this how you can create your custom adapter
Create a ListAdapter subclass and override the getView() method to return a custom view that contains the elements you need.
I have a main menu screen with a simple ListView that contains "links" to further screens in my app (Browse, Bookmarks, Settings, About, etc.). Underneath the ListView there is a TextView (more accurately, a TextSwitcher that rotates TextViews) that changes every 10 seconds to display a new "tip".
In portrait mode, this works fine. There are my five list items in the ListView , and my tip label underneath. However, when I switch to landscape mode, the ListView is taller than the screen. The ListView scrolls normally, but I cannot scroll past the end of the ListView to see the TextView underneath.
I have tried every possible combination of Layouts, wrappers, ScrollViews, and layout_height parameters and I simply cannot get it to behave.
Here is the simplest code I can use to get the result pictured above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ListLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/TipLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="#+id/ListLayout">
<TextSwitcher android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/TipSwitcher">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="7pt"
android:id="#+id/Tip1TextView" android:text="Tip: Hello, Android!"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tip: This is the second TextView in the TipSwitcher!"
android:id="#+id/Tip2TextView" android:textSize="7pt"></TextView>
</TextSwitcher>
</LinearLayout>
</RelativeLayout>
Like I've said, I've already tried so many different combinations that I can't list them, and for the most part I was randomly inserting XML in an attempt to get something to work the way I wanted. So I'd greatly appreciate suggestions as to how I would go about doing this the right way.
Thanks.
EDIT: Something I forgot to mention, this may or may not be relevant. My MainMenuActivity is extending ListActivity. According to the docs, "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen." But, "If you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate()." So I don't think the ListActivity is interfering.
Put the TextSwitcher in the ListView itself. You can use addFooterView() for this.