5
I’m making a screen where register some users. On this screen, I have a ScrollView
with a Gridlayout
inside, with 2 columns. Each column has 1 button, which when clicking, opens a link, in this case, Youtube.
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_columnWeight="1"
android:gravity="center"
android:layout_gravity="fill_horizontal"
android:background="@drawable/bg_button"
android:text="DemonDies"
android:onClick="demondies"
android:textColor="@color/textColorPrimary"
android:id="@+id/demondies" />...
In JAVA I do the rest:
Button demondies,...
demondies = (Button) view.findViewById(R.id.demondies);
demondies.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Uri uri = Uri.parse("https://www.youtube.com/channel/UCEWQoXe934RcJr04efPm9OQ");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});...
So far blz, only now it’s starting to have a lot of buttons, and it’s getting kind of hard to keep creating button like this. Would there be a way to create buttons dynamically, retrieving the name and url of the database link or something? I was going to add the image here to see what the layout looks like, but it’s too big.
Edit The layout looks like this: http://imgur.com/uzEoq5l
Using a Listview would not solve the problem?
– ramaral
put the photo on a photo hosting site
– Mr_Anderson
Edit: posted the image.
– Marceloawq
How to make buttons clickable in a listview?
– Marceloawq
You’ll have to make a custom listview. Basically, you will have an xml that will represent a single item in the list and you will also have an adapter class that will have to receive a list of Strings with the button names and will also be responsible for linking the data from this list of Strings in the buttons that appear on the screens. Wait for the code.
– Mr_Anderson
got it, do it with a listview of images currently, if you have how to do with buttons, great
– Marceloawq
Recycler View...
– itscorey
Images Voce uses Imageview in xml. Buttons just use Button in xml... It’s the same principle; only changes the widget’s appearance.
– Mr_Anderson
Easier than Listview I believe you are using Gridview. https://developer.android.com/guide/topics/ui/layout/gridview.html
– Alisson Marqui
What you want is to take various data (URL, name, etc.) and create the buttons automatically, correct? Yes, first you need to pull the data from somewhere (sqlite, remote database, webservice, json, etc.) and run a script for that. I’ve seen it once. I’ll see if I can find it and put it on. But to get ahead of it, there’s a way.
– rbz
That’s a similar question I asked, and I haven’t tried it yet: Take programmatically generated Edittext values ... You can get an idea I believe.
– rbz