I have been sitting for at least 4 hours trying to solve this problem.
To understand this there are 3 files you need to know about:
eggCatcher.java which extends Activity, this class is not used for much more than
saving gamestate and showing the optionsmenu.
eggCatcherView.java which extends SurfaceView and contains "the game".
eggCatcher_layout.xml which is shown below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layouten">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<easter.fun.EggCatcherView
android:id="#+id/eggcatcher"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<TextView android:text="Score: "
android:id="#+id/totalscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</TextView>
<TextView android:text="Bonus: "
android:id="#+id/bonus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
</TextView>
</RelativeLayout>
</FrameLayout>
</LinearLayout>
As shown in the xml file, EggCatcherView is put in the xml file.
When the applications i started the onCreate calls setContentView(layout.eggcatcher_layout);
My question now is:
how can i, from EggCatcherView.java access and edit the TextViews defined in the xmlfile?
if it was in EggCatcher.java it would be easy, just use findViewById(id.bonus), but from
inside the surfaceView appears to be a little more difficult.
I hope i have made everything clear, if you dont understand just ask!
//micke
I think you should get the parent view and then from there on you can use findViewById() (are you sure you can't just use that method anyway since SurfaceView is a subclass of View and inherits findViewById() from it?).
For using the parent you do something like:
ViewParent vp = eggCatcherView.getParent();
FrameLayout fl = (FrameLayout) vp;
TextView tx = (TextView) fl.findViewById(R.id.bonus);
Of course you need to check if the ViewParent is indeed an instance of FrameLayout.
I found this the best way:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:id="#+id/test"/>
</LinearLayout>
TextView test = (TextView) findViewById(R.id.test);
test.setText("test");
If I understand correctly, you want to access the views in the surrounding activity? That seems like poor architecture. I think it would be better to either pass a callback to the EggCatcherView that can trigger methods in the Activity which in turn operate on the TextViews or fire some kind of events upwards.
Related
I have a PlayerView that takes up the top half of the Activity in portrait orientation with the bottom half of the screen showing some text.
I need to have the controller under the video without overlapping the video content (it will always be shown). By default when a user touches the video the controller appears at the bottom of the video covering the bottom part of the video. I my case I need the controller to stick under the video with no intersections with the video content.
I went through SimpleExoPlayer and PlayerView APIs but I haven't found any way to do so.
Question: How can I place the controller under the video with ExoPlayer?
Here is how the layout looks like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/video_view"
android:scrollbars="vertical" />
</RelativeLayout>
This will push the controls down to the bottom of the screen:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:use_controller="false" />
<com.google.android.exoplayer2.ui.PlayerControlView
android:id="#+id/controls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/video_view"
app:show_timeout="0" />
</RelativeLayout>
Then in Java:
PlayerView videoView = findViewById(R.id.video_view);
PlayerControlView controls = findViewById(R.id.controls);
controls.setPlayer(videoView.getPlayer());
Edit: Modified my answer to suggestion from #RashimiGautam
Refer to the answer by #Pierre.
Also to remove controller from above PlayerView, in that case, #id/video_view by writing player.showController(false) in java file.
You can also use app:use_controller:false in the xml.
So you will the only the video without controller on top. And link it to a new controller, in that case, #id/controls at the bottom of the video.
This might give you an idea, also have you tried to override the controls?
As an example, suppose we want our playback controls to consist of only a play/pause button positioned in the center of the view. We can achieve this by creating exo_playback_control_view.xml file in the application’s res/layout directory, containing:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton android:id="#id/exo_play"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#CC000000"
style="#style/ExoMediaButton.Play"/>
<ImageButton android:id="#id/exo_pause"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#CC000000"
style="#style/ExoMediaButton.Pause"/>
</FrameLayout>
Note that in the layout #id/exo_play and #id/exo_pause are standard ids defined by the ExoPlayer library. Use of standard ids is required so that child views can be identified, bound to the player and updated in an appropriate way. A full list of the standard ids for each view can be found in the Javadoc for PlaybackControlView and SimpleExoPlayerView. Use of each standard id is optional.
https://medium.com/google-exoplayer/customizing-exoplayers-ui-components-728cf55ee07a
I am working on an Android project in which I have two different types of Chat possibilities. One is for Groups and other is for private or one to one communication. Now, all the Groups and Users the logged-in user can chat with is shown in ConversationActivity.
For this, I have prepared two lists in the UI, and contents will be added to each list by an Async method. Each List has a separate adapter, with which I will be able to easily identify, which item was clicked.
This mechanism is working just fine, except that when the activity is opened, the list look they are layered on top of each other, obviously that is not the intention. So I indicated in the RelativeLayout, to position it above another, that also didn't help.
How can I display two lists with two different adapters in one activity page?
activity_conversations.xml :
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:layout_above="#+id/privateRelativeLay"
>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/conversationList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:id="#+id/privateRelativeLay">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/privateConversationList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
ConversationActivity.java
public class ConversationActivity extends ApplicationDrawerLoader {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversations);
if (isOnline()) {
new getConversationsForLoggedInUser(this).execute();
new getPrivateChannelsForLoggedInUser(this).execute();
}
public class getPrivateChannelsForLoggedInUser extends AsyncTask<Void,Void,ResponseEntity<PrivateChannel[]>>{
#Override
protected void onPostExecute(ResponseEntity<PrivateChannel[]> responseEntity) {
privateChannelConversationList = (ListView) findViewById(R.id.privateConversationList);
privateConversationAdapter = new PrivateConversationAdapter(conversationActivity, privateMapList);
privateChannelConversationList.setAdapter(privateConversationAdapter);
// On click adapter excluded
}
public class getConversationsForLoggedInUser extends AsyncTask<Void, Void, ResponseEntity<RestGroupAccount[]>> {
#Override
protected void onPostExecute(ResponseEntity<RestGroupAccount[]> responseEntity) {
super.onPostExecute(responseEntity);
groupAccountConversationList = (ListView) findViewById(R.id.conversationList);
groupConversationAdapter = new GroupConversationAdapter(conversationActivity, groupAccountMapList);
groupAccountConversationList.setAdapter(groupConversationAdapter);
// On click adpater excluded
}
}
I hope this much information is sufficient, if anything else is required, let me know. Any help would be nice. Thank you.
This is how it looks : screenshot :
They display on top of eachother because you put them both in a separate RelativeLayout that have the same positioning properties (none).
One fix would be to put both listViews in a LinearLayout instead. You can provide this LinearLayout with an orientation attribute vertical or horizontal, depending on how you want to display them, then use the layout_weight attribute to determine what portion of the screen each listviews occupies.
For example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Note: fill_parent is legacy. Use match_parent instead.
It is because height of your both List Views is fill_parent. Try using wrap content or in your case i don't know how exactly do you want both List views to be displayed but I would suggest you use linear layout with orientation that you want and assign weights.
The layouts stack on each other in RelativeLayout as long as the cover the whole screen width and height.
Also fill_parent attribute value is deprecated now it is better to use match_parent.
I'm working on a information orientated app using the master/detail flow and so far, so good. I would like to add images to the TextView, but it's formatted differently then what I've experienced in the past and I'm lost. from my understanding of what I've read while searching is that the scrolling text is "newer" when generating the Master/detail activity, therefore I haven't found any information on this specific issue. I would also like to pass the images in using the content activity, so it would be-
addItem(new Item(ID,Name,Detail,Image1,Image2));
what the detail XML file looks like
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bobblehead_detail"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textIsSelectable="true"
tools:context="com.example.johnson.fallout4bobbleheadlocations.BobbleheadDetailFragment" />
I tried adding ImageView's under it, but I received errors.
tl;dr I would like to add 2 images under the scrolling TextView.
I am not sure if I clearly understand what you mean but looking at your xml it seems to me that you need to add a layout (either relative or linear) to your xml file and then add a textview and two imageviews (or whatever you want) into that layout.
Something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" tools:context="com.example.somefragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id2"
android:text="Here is your text"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id"
android:src="#drawable/image1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/some_id3"
android:src="#drawable/image2"/>
</RelativeLayout>
I have an XML RelativeLayout snippet that I would like to include several times (from a loop) in my main View. The problem seems to be -- is there a way to avoid hard-coding the parent of the RatingBar, since each time I include the RelativeLayout snippet my elements will need to have different ids?
As far as I can tell, the recommended way is to get the layout snippet and then override the android:id for each element to be unique, and then override the android:layout_below manually for each element that has relative positioning. This seems a little kludgy -- is there any way to have these bindings get done automatically?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:id="#+id/relativeView">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1" />
</RelativeLayout>
you just need to change the id of the RelativeLayout
like
int BASEID=200;
View v = mLayoutInflator.inflate(R.layout.myRelativeLayout, null);
for (int i;i<10;i++){
v.findViewById(R.id.relativeView).setId(i+BASEID);
}
mRootView.addView(v,...);
then when you need to get the RatingBar for suppose the 4th RelativeLayout you added you can call
RatingBar mRatingBar = (RatingBar)mRootView.findViewById(BASEID+3).findViewById(R.id.ratingBar1);
This question already has answers here:
ERROR No package identifier when getting value for resource number
(11 answers)
Closed 5 years ago.
Okay after searching through the various threads that I could find on this topic, and trying everything that seemed to work for the others... I have succumb to the need to post my own issue.
As a total noob to Android and Java, you will have to forgive the need to talk to me like a child :)
I have an app that has 3 activities so far. A main window that works as a navigation screen to choose one of the other two. The first works perfectly, but when I try button number 2... it crashes.
I have some code that I am trying to run onCreate... so when I saw that it was often the XML layout that caused the crash.. I commented out the Java code... and presto the layout loads fine.
putting the code back in, it crashes once again.
Looking in LogCat I see a line that says "No Package identifier when getting value for resource number 0x0000000d"
Then all sorts of lines about fatal exceptions and shutting down the VM.
So, garnering confidence from other posts, I went to the R.java file to see what resource has that ID... and it's not in there. They ALL start with 0x7f... now the ONLY one that ends in "d" is a textview with the ID "game_Answer1". But I am not certain if that is what it's referring to or not.
I did try the Eclipse clean command, as well as deleting the R.java... same issue.
Below is the java code that seems to be crashing... again, hold the laughter at my spaghetti code as this is attempt #2 in Java beyond a hello world app!
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Random;
public class playgame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamelayout);
//set up first question
genQuestion();
}
Random generator = new Random();
public void genQuestion() {
int i = generator.nextInt(25);
int correct = generator.nextInt(2)+1;
TextView g_answera = (TextView) findViewById(R.id.game_Answer1);
TextView g_answerb = (TextView) findViewById(R.id.game_Answer2);
TextView g_answerc = (TextView) findViewById(R.id.game_Answer3);
g_answera.setText("-");
g_answerb.setText("-");
g_answerc.setText("-");
if(correct==1){
g_answera.setText("!");
}
if(correct==2){
g_answerb.setText("!");
}
if(correct==3){
g_answerc.setText("!");
}
}
Below is the XML layout associated with this issue... just in case I am missing something obvious:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#ffffff" android:id="#+id/gamelayoutwrapper">
<RelativeLayout android:layout_height="wrap_content" android:id="#+id/games_animalnameFrame" android:layout_width="fill_parent" android:gravity="right">
<TextView android:layout_height="wrap_content" android:id="#+id/game_animalname" android:layout_width="fill_parent" android:textSize="60sp" android:layout_alignParentRight="true" android:gravity="right" android:text="__nteater"></TextView>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:id="#+id/game_animalimageFrame" android:layout_below="#+id/games_animalnameFrame" android:layout_centerInParent="true" android:layout_height="wrap_content" android:gravity="top">
<ImageView android:id="#+id/game_animalImage" android:src="#drawable/elephant" android:scaleType="fitCenter" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingBottom="10dp"></ImageView>
</RelativeLayout>
<RelativeLayout android:layout_height="wrap_content" android:id="#+id/gameLettersFrame" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_centerHorizontal="true" android:background="#cccccc">
<TextView android:layout_height="wrap_content" android:id="#+id/game_Answer1" android:text="A" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="left" android:clickable="true" android:textSize="100sp" android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:textColor="#090999" android:background="#ccccFF"></TextView>
<TextView android:layout_height="wrap_content" android:id="#+id/game_Answer2" android:text="B" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="center_horizontal" android:clickable="true" android:textSize="100sp" android:layout_width="wrap_content" android:layout_centerInParent="true" android:textColor="#090999" android:background="#ffcccc"></TextView>
<TextView android:layout_height="wrap_content" android:id="#+id/game_Answer3" android:text="C" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_gravity="right" android:clickable="true" android:textSize="100sp" android:layout_width="wrap_content" android:layout_alignParentLeft="false" android:layout_alignParentRight="true" android:textColor="#090999" android:background="#ccffcc"></TextView>
</RelativeLayout>
I ran into this same issue a while ago, I assume that it is fixed but I figured an alternative answer is always welcome :) When using the setText() method you CANNOT send through primitive data types such as int and double, rather convert to string and then send through the string. This fixed my issue when it popped up
Android doesn't allow uppercase resource names
Rename
R.id.game_Answer1
to
game_answer_one
etc for the other's you have
I wanted to look at the ID name for a LinearLayout to make sure I was accessing the correct one in my code. This error for me happened because I had forgotten to add an android:id="#+id/myid" into one of my XML layout files (because that file wasn't the focus of my attention at that point!)
in your R.java file, does an id for gamelayout exist? This should be located in
public static final class layout {
} in the R file? Thats the first important step in determining why your application crashes.
Android (unfortunately) doesn't accept resource identifiers with upper-case letters.
Also, make sure you are defining your namespace in the root view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/game_answer1"
android:text="A"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_gravity="left"
android:clickable="true"
android:textSize="100sp"
android:layout_alignParentLeft="true"
android:textColor="#090999"
android:background="#ccccFF"
/>
</RelativeLayout>
there might be others errors in your project because of which your R.java is not created well. hence while executing it is not getting the resource with that id. R.id contains id of all the resources in your projects.
It's quite late to answer, though might be useful for those to will find this thread through search.
The setText method requires the identifier as an argument.
So, there should be a string in the values/strings.xml with name like "empty_answer", containing the value "-". In other words, the line like:
-
Then the setText should be called like:
g_answera.setText(R.string.empty_answer);
Possibly, what happens in your case is that the string "-" was somehow turned into resource id 0x0000000d, which of course doesn't exist in your app. Although it's strange that such code was compiled.
One more comment: in Android it IS POSSIBLE to name resources like IDs with Capital characters. What is NOT possible - to have Capital chars in the NAME of resource files.