My android app has several screens, and in each one of them I would like to have a similar options menu with a slight different.
For example screen A would have items 1,2,3,4 and screen B would have items 1,3,4,5
As you see they have items in common which make it stupid to have a different xml menu for each one, because I will have to define the items again and again.
Is there a smart way of doing that?
you could try to use this code in your various layouts
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/your_layout_with_options" />
where your_layout_with_options is an xml file containing all your options.
so you have always the same identifiers in your screens and you can chose witch items to show and witch items to hide..
hope this help
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.
I have a tabbed interface for my program - there are two tabs: take photo and view photos.
As the name suggests, the user can take a photo in "take photo" and the user can view photos taken in "view photos". Right now the way its set up I use one single MainActivity and I have TakePhotoFragment and ViewPhotoFragment -- question is: does this contradict the principles in which Fragments are supposed to be used in? I don't really anticipate having both fragments displayed in a single screen (e.g. on a tablet), but I don't see how I can use one activity for each because of the limitations of the tabbed interface (when I created the activity in eclipse, I was prompted to select what kind of layout, I chose tabbed layout, and automatically code for fragments within an activity corresponding to several tabs was generated)
Can anyone help? Should "take photo" and "view photos" be fragments or activities?
It should definitely be fragments.
This does in no way contradict anything, plus I do not understand your concern about showing both fragments in a single screen. If you do not want that to happen, you just program accordingly. That is certainly not something that just happens because of the choices that you have mentioned so far.
Fragments is the best method you can use for the purpose you mentioned above. You can check the below links to know about the usage of fragments.
Creating a fragment
Fragments
android fragments
android fragments tutorial
I have an already working android app, but I need to update it for better layout on tablets. I can rewrite it no-problem, but what I want to ask is;
I will have 2 fragments on screen (one for list, and one for details). When I select a customer from list, details will update itself (I have already done this without a problem). BUT, when I select an item from details fragment, it must change itself (it must be opened on details fragment, but of course it will be a different class). For example first details will be a list of details, but when I select an item from details list, it will be a different layout, maybe WebView or so..
To better represent the idea,
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
LLLLLL DDDDDDDDDDDDDDDDDDDDDDDDDD
Where "L" is list fragment, and "D" is details fragment. But I want to show 2 (or more) different classes (activities) (Not at the same time btw) in the details fragment. How do I do that? I was searching this for 2 full days, there is nice tutorials but I couldn't find any example for this behaviour.
Thanks in advance.
You can use a viewFlipper inside your details fragment. Of this way you can have two clases there, and navigate between then. For example:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/example_view_flipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<include android:id="#+id/first" layout="#layout/first_layout" />
<include android:id="#+id/second" layout="#layout/second_layout" />
</ViewFlipper>
I'm developing a soft keyboard for Android (respectively I'm trying ;)) and since I'm new to Android and Java it's not that easy.
The basic keyboard is working (thanks to examples in the web) and now I'm trying to create a sort of "popup" (with new buttons) which is evoked when you press and hold a button for a certain time (time should be open for customization). Then one should be able to drag the finger to the new buttons. For example: You hold e and then drag the finger to é etc.
My first attempt was to use something like:
<Key android:codes="101" android:keyLabel="e"
android:popupKeyboard="#layout/popup"
android:popupCharacters="eéè€"
/>
where popup.xml contains a seperate basic keyboard structure:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="30px" />
But there are several disadvantages:
I don't know how to tweak the position and appearance of the popup
the finger has to be lifted in order to press a new button (weird behaviour)
the time the user has to wait until the popup appears seems to be fixed
How would you solve this problem? An invisible key-row (absolute position) which is set to visible once a button is touched for a certain time (monitored with motion events etc.) ?
Do you know any tutorials which give information on this "press-hold-drag-button"-concept in keyboards? A code example would be very helpful. I looked through the LatinIME git project but unfortunately it's very overwhelming :/
This is a very, very old question, but this may help someone else like me. I am browsing the Android source code to see how to make the alt-key popup:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/inputmethodservice/KeyboardView.java#KeyboardView.onLongPress%28android.inputmethodservice.Keyboard.Key%29
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.