First of all, I want to tell you that I'm new here and I'm from Spain and my english level isn't so good, so please... try to understand me haha. Also, you must know that I'm a telematic engineering student and programming is just my favourite hobby. With this info, I just want you to be comprehensive with my question: I know that it's a rookie question and it's worse when I'm pretending to create an Android game... But I'm learning bit by bit.
PROBLEM
The problem that I have is the next one. I want to set a .PNG image in my principal activity called "*activity_principal*". This image has lot of empty space and the background is initially black (due to the theme selected at the beginning I guess). Well, this image has some details in black and them merges with the background colour.
My first solution was trying to set the two backgrounds from the XML corresponding code, but rapidly I realized it was impossible to use it twice in a same layout. So I thought that it would be fixed by the next way: I set the "android:background="#android:color/white"" in the XML file and in the .java file I set the other resource to the background:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
RelativeLayout fondo = (RelativeLayout) findViewById(R.layout.activity_principal);
fondo.setBackgroundResource(R.drawable.prototipoestructurapantalla);
}
I used to use this structure and never had problems but today, trying to do this, I noticed something strange. Due not to find the error, I put two breakpoints at the lines under "setContentView(..." and debugging, when the cursor reached the last code line "fondo.setBack..." the variable "fondo" was "null" and I think that there is the problem, so when I resume the debugging the app crashes...
I hope you can help me. Thank you!
The problem is that findViewById(R.layout.activity_principal); is going to return null.
You are passing a layout ID (R.layout.activity_principal) instead of a View ID.
It should look something like findViewById(R.id.fondo);.
Related
So, what I have currently is basically
displayText.setText();
scroll.fullScroll(View.FOCUS_DOWN);
This goes over and over with user input, and eventually displayText gets big enough that it moves on to another line, which is why I want it to scroll down. I'm pretty new to this and so I'm not too sure what I'm doing wrong.
I'm not completely sure about what you want to do, but if I understood well, look at this, it might help you : How to get the Android TextView to scroll down to the end automatically?
I need some help.
I'm wanting to make an activity similar to this, but I'm not sure where to start.
Basically, it's like a texting UI, with the users question on the right and the answer on the left. I was going to use a list view for the sake of simplicity, but I dont' think it'd support this kind of structure.
I googled some layouts where I can scroll, but most of them require me to premake them, which isnt an option because its a dynamic chat log.
Does anyone here have experience with this kind of UI? Can you point me in any direction? I hate to ask such a strange question, but I don't even know how I'd word this situation on Google.
Thanks in advance.
What you're looking for is a ListView where you can return a different layout depending on whether the message is sent or received. A ListView is the most efficient implementation for this because its ideal for displaying a potentially large data set without having to keep a view in memory for every row.
This is achieved by extending the BaseAdapter in your application and overriding the getItemViewType(), getViewTypeCount(), and getView() methods (along with all the other methods required by a ListView adapter).
This is a good tutorial that serves as a walkthrough for this pattern.
Instead of using ListView, You may programmatically construct a viertical LinearLayout, and add TextView in each line, you can set the alignment of the TextView in lines accordingly.
I'm creating an app for the Android OS, and I'm running into a bit of a stumbling block on one issue. Here's what I want to do:
I'm currently capturing the "Back" button event just fine, but I need it to behave slightly differently, depending on the current layout the user is viewing. I have four layouts that I'm using, and if the user is on layout 1, 2, or 3, I want "Back" to take them to layout 1; but if they are on layout 4, I want them to go back to layout 3 instead.
The problem is, I can't for the life of me figure out the code that will return the id of the CURRENT layout. I'm sure this is a pretty simple problem, so I'm hoping someone will have a quick solution.
Thanks for the pointers - for some reason I'm having trouble getting getCurrentFocus() to work, though...probably due to my own ineptitude in programming Java.
I've broken it down into the following:
View thisView = getCurrentFocus();
if (thisView != null){
int viewID = thisView.getId();
toastLong(Integer.toString(viewID));
} else {
toastLong("thisView is null.");
}
The problem now is that thisView is always null - it's not returning any values. I tried putting in the activity name that I'm using in place of myActivity (making it:
View thisView = myActivityName.getCurrentFocus();
but the IDE gives me the following error and won't compile:
Cannot make a static reference to the non-static method getCurrentFocus from the type Activity.
I'm obviously missing something, and my assumption is that it's a very basic something that I'm missing. Any pointers?
I had this problem too and found a very simple solution. Though this is an old question, I'll post it here for people who are searching for help for this problem.
Just create an integer as attribute in your class:
int layoutId;
Then override the setContentView method and save the ID from the parameter:
#Override
public void setContentView(int layoutResID) {
this.layoutId = layoutResID;
super.setContentView(layoutResID);
}
Very simple trick!
You can use ViewFlipper to hold your layouts and implement a simple state machine to control transitions.
another option might be creating a separate Activity for every one of your layouts - hard to tell without exact knowledge about what your app is doing.
I'm not looking for exact code here, just a direction on what to look for and what I should be reading about so I can figure this out.
I have a layout that I would like to remain static, with only the listview changing depending on what's selected from the list. I've reloaded data in the list, but I would like the fancy transition animations between choices, and would like the app to go to the previous menu when pressing back.
Someone suggested using a viewswitcher, which seems like it'd be great, but I am still unsure about how to fill a listview in a layout with a regular row layout, then on selection do an animated transition to a custom row. Also, it seems the viewswitcher is limited to two views, so it may be a limitation when I want to go a few menus deeper.
Preferably, I'd like to put each menu in it's own class so that I can handle filling it in that class, if possible...
Hope this isn't too vague, but if it is I'll be more than happy to explain myself further.
Don't know if it's what you meant but have you tried showing and hiding the views?
findViewById(R.id.listViewID).setVisibility(LinearLayout.GONE); //hide the one you want
findViewById(R.id.listViewID2).setVisibility(LinearLayout.VISIBLE); //show the one you want
Hope it's what you meant :)
I'm trying to create a "scrollable" layout in Android. Even using developers.android.com, though, I feel a little bit lost at the moment. I'm somewhat new to Java, but not so much that I feel I should be having these issues--being new to Android is the bigger problem right now.
The layout I'm trying to create should scroll in a sort of a "grid". I THINK what I'm looking for is the Gallery view, but I'm really lost as to how to implement it at the moment. I want it to "snap" to center the frame, like in the actual Gallery application.
Essentially, if I had a photo gallery of 9 pictures, the idea is to scroll between them up/down AND side to side, in a 3x3 manner. Doesn't need to dynamically adjust, or anything like that, I just want a grid I can scroll through.
I'm also not asking for anyone to give me explicit code for it--I'm trying to learn, more than anything. But pointing me in the right direction for helpful layout programming resources would be greatly appreciated, and confirming if it's a Gallery view I'm looking for would also be really helpful.
EDIT: To clarify, the goal is to have ONE item on screen at a time. If you scroll between one item and the next, the previous one leaves the screen, and the new one snaps into place. So if it were a photo gallery, each spot on the grid would take up the entire screen size, approximately, and would be flung out of the viewable area when you slide across to the next photo, in either direction. (Photos are just an example for illustration purposes)
This page gives a good summary of the different built in layout objects. From your description a GridView or possibly a TableLayout might work. GalleryView looks to be horizontal only.
I believe GridView is what you're looking for. Here's a tutorial: http://developer.android.com/resources/tutorials/views/hello-gridview.html
You should check out the ViewPager widget, which is available in the Android compatibility package. I spent a loooong time trying to get the Gallery widget to behave properly, but finally settled on a ViewPager which returned ImageView objects instead. Works like a charm.