I am developing an android app as an educational project. I have created a GridView object, which uses a custom GridAdapter. The grid, as default, loads a set of placeholder images. I want the user to be able to replace those placeholder images with either their own image, some text, or a link to a youtube video.
I can't find any info on whether this is feasible. I specify an Imageview in my XML file, so I don't know how I could change this at run time? I wondered if I should create a TextView object in the XML, and layer them, and then create a method that brings one to the front? Any advice at all would be very much appreciated. It would be great to know if it's doable or not.....
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_centerInParent="true"
/>
My suggestion is define TextView etc. in xml with gone visibility. When user has some actions get visible them and set visibility gone of ImageView.
Set the visibilities of the container based on user input... What ever user chooses make tat container visible and other two as Gone... This would work
Related
I'm creating my first Android App, and as part of it, I need to add new buttons after a click on another button.
Then I need to create() a button after a onClick() event on another button.
How to do it?
Things you should know:
I am using Android Studio to develop the app,
I have already imported these:
import android.view.*;
import android.widget.Button;
import android.widget.TextView;
And I'd like to know where to get the whole Library list for android, or better, I'd like you to say what's your best android library to develop on such these things, I need to find and get experienced on a library so I'll need suggestions!
Thanks a lot in advance :)
P.S. Just typed those create() and onClick() to make it clear that I was talking about methods, but not that both of 'em are real ones or the ones I'll need ;)
P.P.S. I don't know how many buttons will be ther becuase I do NOT choose that, the user does, it's somethig like: I want to create a new thing, then I press the button '+' and create a new button (the new thing), for this reason, the amount of buttons isn't known, that's why I can't use the visibility trick, but yeah I first thought about that aswell!
rockfight's answer is correct, but I would recommend you to use the button in your layout with android:visibility="gone" and then show it when the user taps on the first button using button2.setVisibility(View.Visible).
It's always easier to create your layouts and views in xml instead of code.
Personally, I'm using support-v4 library, recyclerview, recently-released design library and some of the Google Play Services libraries.
EDIT: if you're going to add a lot of buttons, and you don't know how many, I'd recommend you to use ListView or RecyclerView. I personally prefer RecyclerView, but it might be more difficult to set up. Anyway, you'll be adding items to your list. That items are basically buttons, so your item layout would look like this:
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a new button!"
android:layout_margin="16dp"
android:gravity="center_vertical|center_horizontal"
android:elevation="2dp"
android:translationZ="6dp" />
<!-- Material Design. See http://www.google.com/design/spec/what-is-material/elevation-shadows.html#elevation-shadows-elevation-android- -->
<!-- The attributes you need...... -->
That's all. You don't have to mess with ugly code. If you want to get an idea of the advantages of RecyclerView, see http://antonioleiva.com/recyclerview/.
Create a button object
Button myButton = new Button(this);
Then add button to your layout
LinearLayout layout = (LinearLayout) findViewById(R.id.my_layout);
layout.addView(myButton);
I am not sure what library are you talking about. To learn about android go to android developer website and click search button and search what you want to know. You will get all libraries there.
You do not need libraries for making onClick methods. When you have your button, set onClick method like this: button.setOnClickListener(new onClickListener...) if you press Ctrl+Space you will get possible choices of what to choose.
How many buttons do you wish to add programmatically? Maybe it would be better if you create buttons in xml and use Visibility.GONE and Visibility.VISIBLE if there are not many buttons and you know the end number of them.
Using android, I'd like to get a vertical switch widget implementation in place. As far as I can tell, it looks like switches only have a horizontal orientation. I'd like to have something like the following:
After looking through the threads here and searching google, I have yet to find something that can give me this. Everything I search for gives me the horizontal implementation only.
so I can generate your typical horizontal switch
<Switch
android:id="#+id/switch_button"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/label"
android:layout_gravity="center"
android:switchMinWidth="130dp"
android:thumb="#drawable/switch_selector"
android:track="#drawable/track_selector" >
</Switch>
but there doesn't seem to be a good way to set the orientation. I know that the question is a bit high level. Is there some attribute that is immediately available that will allow me to have a vertical switch? Or will I have to create a custom switch and possibly modify the onDraw method so that it is flipped vertically? Any help is appreciated. Thanks.
Try android:rotation="90" like this:
<Switch
android:id="#+id/switch_button"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/label"
android:layout_gravity="center"
android:switchMinWidth="130dp"
android:thumb="#drawable/switch_selector"
android:track="#drawable/track_selector"
android:rotation="90">
</Switch>
There is no quick attribute for vertical orientation.. Sorry :)
First you can look at the code of the switch and see if you can copy and manipulate it.
Second you can just implement your on. Have a layout with a button inside it. use onTouchListener to slide it from side to side. No need to use "onDraw".
Try toggle button witch graphics and use pictures like this You included in Your post. Here is example of such toggle buttons: Toggle button using two image on different state
I was testing my android app on device by enabling device show layout boundaries in developer option on device.
I check my listview with inflated view with textviews , rating bar and other views clearly seen as shown below .
later I tried twitter app but surprise to see only single view ???
anyone know how to get twitter like single view on listview ??
anyone know how to get twitter like single view on listview ??
Each list item is a single custom View object, not a ViewGroup or layout. Essentially, all the content is drawn directly onto the Canvas in onDraw() rather than relying on child ImageView and TextView elements. Images can be drawn easily enough by calling Drawable.draw() or Canvas.drawBitmap() and text is typically drawing using a Layout.
Additionally, this means all touch events are handled directly inside onTouchEvent() to handle taps on the lower icons and/or the avatar image, so there are no click listeners.
Edit: Here's a quick 30 second example that should be enough to get you started: https://gist.github.com/devunwired/8704007
They are probably using the include tag to include another layout file
you can do the same thing if you make your list_row_item layout something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
>
<include layout="#layout/another_layout"/>
</LinearLayout>
The benefit to doing this is that you can swap layouts on the fly, perhaps even using a server side changes, rather than having to issue a software update
I'm using RelativeLayout. I understand using Graphical Layout you can't position a button too well. How can I do it in XML? I've tried changing the marginLeft, but nothing moves.
All I want to do is position my buttons!
<Button
android:id="#+id/ListsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/EditButton"
android:layout_alignBottom="#+id/EditButton"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:text="Li" />
You can use marginLeft, marginRight, marginTop, and marginBottom for small adjustments. If you want to do larger adjustments you can use blank elements in combination with weights, or use a RelativeLayout in combination with alignParentBottom, alignParentTop, alignParentRight, and/or alignParentLeft
If you are beginner in android programming then I would recommend using graphical layout since you won't know every attribute for positioning and styling your views. You can see #Freelancer answer to see how many attributes you have to consider while positioning any view correctly.
Using graphical layout is very easy once you get a little hang of it. You can use Relative Layout which will help you moving any view like button or textView wherever you want. Direct xml coding will require some skills that you will no doubt get in time.
I'm writing an application for Android phones for Human vs. Human chess play over the internet. I was looking at some tutorials, to learn how to develop Android applications and found a very nice example of making galleries (it was a GridView usage example for making a gallery about dogs) and the idea came to draw the chess table using a GridView, because the example project also handled the point & click event and I intended to use the same event in the same way, but for a different purpose. The game works well (currently it's a hotseat version), however, I'm really frustrated by the fact that whenever I rotate the screen of the phone, my GridView gets hysterical and puts some empty space in my chess table between the columns. I realized that the cause of this is that the GridView's width is the same as its parent's and the GridView tries to fill its parent in with, but there should (and probably is) be a simple solution to get rid of this problem. However, after a full day of researching, I haven't found any clue to help me to make a perfect drawing about my chess table without a negative side effect in functionality.
The chess table looks fine if the phone is in Portrait mode, but in Landscape mode it's far from nice.
This is how I can decide whether we are in Portrait or Landscape mode:
((((MainActivity)mContext).getWindow().getWindowManager().getDefaultDisplay().getWidth()) < ((MainActivity)mContext).getWindow().getWindowManager().getDefaultDisplay().getHeight())
In the main.xml file the GridView is defined in the following way:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="8"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>
...
</LinearLayout>
I appreciate any help with the problem and thank you for reading this.
Portrait: http://www.freeimagehosting.net/image.php?f388b3ec64.png
Landscape: http://www.freeimagehosting.net/image.php?ee790603a2.png
A GridView probably isn't what you want here. GridView, like ListView, is for efficiently presenting scrolling, unbounded data sets. A chess board is neither. Populating a TableLayout programmatically is probably what you want instead.
The reason your GridLayout doesn't seem to be honoring android:layout_width="wrap_content" is that since GridView is meant for displaying unbounded data where each item can have a different size, it doesn't trust that items have a uniform width that can be reasonably measured. (If an adapter has 10,000 items, should GridView measure all of them to determine the correct column width?)
If you're going to try to keep using GridView for this anyway (which you shouldn't), try setting an explicit value for android:layout_width rather than wrap_content. This will stop the GridView from expanding to fill the available space. You can also use alternate layouts for different screen orientations using the resource system as described here. Alternatively you can disable landscape mode using android:screenOrientation="portrait" on the activity tag in your manifest. ;)
The problem is simply solvable using the setPadding method of your GridView object.