i've just started android programming and i can't put the image into MainActivity.java. I get an error.
Multiple markers at this line
-Syntax error, insert "... VariableDeclaratorId" to complete
FormalParameterList
- Syntax error, insert "... VariableDeclaratorId" to complete
FormalParameterList
- Syntax error on token "setImageResource", Identifier expected after
this token
Java code is
(i get an error on 2nd line)
.....
final ImageView image = (ImageView) findViewById(R.id.ImageView1);
image.setImageResource(R.drawable.ic_launcher);
}
Android is
<ImageView
android:id="#+id/ImageView1"
android:contentDescription="#string/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
/>
Help me please, i really can't understand what is that, i've been stucking on it for 2 weeks!
I guess ic_launcher is not in drawable folder, but in mipmap. So you need to use R.mipmap.ic_launcher in code and #mipmap/ic_launcher in xml
Why have you declared ImageView variable as final? If you declared a variable as final, it can never be changed after it has been defined. So, try to remove final keyword of image declaration.
You could also remove android:src declaration from your layout, instead of removing final keyword.
Related
I'm following a guide to creating a custom view in android studio whereby I define the view's attributes in XML but a few lines are causing errors in the classes. Here in the block of code causing troube:
<applicationprogramming.task401d.CustomView
android:id="#+id/custView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="5dp"
custom:circleColor="#6039ff"
custom:circleLabel="Hello"
custom:labelColor="#d9d908">
</applicationprogramming.task401d.CustomView>
The three lines of code bellow
custom:circleColor="#6039ff"
custom:circleLabel="Hello"
custom:labelColor="#d9d908
are causing the following error in one of the classes:
Error:(14) No resource identifier found for attribute 'circleColor' in package 'applicationprogramming.task401d'.
When I define these attributes:
to get the text and colors specified using the names in attrs.xml
circleText = a.getString(R.styleable.CustomView_circleLabel);
circleCol = a.getInteger(R.styleable.CustomView_circleColor, 0);//0 is default
labelCol = a.getInteger(R.styleable.CustomView_labelColor, 0);
I get 'Cannot resolve Symbol 'R.
And the only way to solve this issue is to remove where i define the attributes.
Any help would be appreciated.
Thanks in advance
You need to apply custom schema to xml. Use it like below .
<applicationprogramming.task401d.CustomView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/custView1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="5dp"
custom:circleColor="#6039ff"
custom:circleLabel="Hello"
custom:labelColor="#d9d908"></applicationprogramming.task401d.CustomView>
A few ImageViews have been already created in xml
eg:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/blue_dot1"
android:src="#drawable/checkers_blue1"
android:layout_alignBottom="#+id/y1"
android:layout_alignLeft="#+id/x1"
android:layout_alignStart="#+id/x1"/>
Is it possible to update the xml later on programatically in Java for the alignbottom, left and start variables (so that the blue_dot1 image can be moved along the x/y grid of images)
For example I am going to make a method which has a X and Y arg to allow movement in a simple grid, its just the xml part i cant figure out.
yes this is very much possible. but you are not modifying the XML instead you are modifying properties of a specific view:
check this question
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params); //causes layout update
You can not change the xml-file. But you can set LayoutParams of your view programatically.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_BOTTOM);
No, modifying the XML is not possible to do from Java code. However, you can modify the Views layout properties programatically even after the XML properties have been declared and the view has been drawn. Its fairly simple to do. You just get a reference to that image view:
ImageView myImg = (ImageView)findViewById(R.id.myImg);
myImg.setLayoutParams([Your params here]);
I am having issues trying to resolve the specified error.
Here's my xml code for activity_craft_your_story.xml layout:
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/titleGraphic"
android:src="#drawable/black_title" />
This is the code that I am currently trying to link that file with CraftYourStoryActivity.java:
public void BitmapDrawable (Resources res, Bitmap bitmap, Object BitmapDrawable)
{
ImageView titleGraphic = (ImageView) findViewById(R.id.image);
titleGraphic.getResources().getDrawable(R.drawable.black_title);
}
I don't know if that's the right one to use but I need a working title graphic. What can I do?
Please check the drawable folder.
Make sure it contains the black_title.png image and it's name is correct.
android:src="#drawable/black_title" gives you error until it doesn't got the /res/drawable/black_title.png
Do you have the drawable resource called "black_title" in your drawable folder ?
Incase If you want to keep one resource for all different resolutions then create the folder /res/drawable and place black_title.png in it. Build the project. Now this resource will be taken.
Try cleaning , building or syncing IDE with files .
Situation:
I have a TextView that have the property
android:textAlignment="center"
I am generating another TextView dinamically, based on my TextView from XML Layout, using a clone, cloning all the basic properties to work the way above.
Problem:
To do this i need to use this method:
this.myTextView.setMyTextViewProperty(MyTextView.getMyTextViewProperty());
for example:
this.MyTextView.setText(MyTextView.getText());
Note that this.MyTextViewis a local variable and MyTextView is a private var declared on the top of the file, under class name.
I do this on all the properties of the TextView but when i hit the following line of code from the TextAlignment property...:
this.myTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
I tried to set it to a Custom Aligment instead of getting from my XML TextView It gives me an error:
11-20 15:27:04.460: E/AndroidRuntime(9185): FATAL EXCEPTION: main
11-20 15:27:04.460: E/AndroidRuntime(9185): java.lang.NoSuchMethodError: android.widget.TextView.setTextAlignment
But i see on Ctrl + Space that the method exists, so i cant understand what is happening.
A second try was, to set my TextView property to the property that comes from my TextView:
this.myTextView.setTextAlignment(MyTextView.getTextAlignment());
With no success, too.
Obs: i do not want a Android XML Layout solution, i want a solution on code, because i generate the TextView dinamically on the Activity
I'm using API Level 15
Any help?
The setTextAlignment method was added in API level 17. Maybe your compiler in the IDE is above 17 and the device/emulator which you are testing is less than that. Here the link to setTextAlignment.
Added from the comments:
For API level 15 and below, you want setGravity, per this question.
For some reason I'm getting a nullPointerException when working with any new View I place in my XML. The view type (TextView, EditText, etc) doesn't matter. Any views I originally had work - it's isolated to any newly added views.
I've tried cleaning the project numerous times, deleted the entire XML file, restarted eclipse, then re-pasted the XML back into a new file, no luck. Appears to be similar as this question, but nothing has gotten this working.
I really don't want to have to re-create the entire project, but I'm not sure what else to do if recreating the XML and cleaning isn't enough.
Code is as follows:
XML
...
<TextView
android:id="#+id/dlg_add_proj_test_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Text" />
...
Activity
...
public void fireDlg() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_add_proj);
dialog.setTitle("Add Project");
//Other previous views
TextView newTxtView = (TextView) findViewById(R.id.dlg_add_proj_test_text);
newTxtView.setText("New Text"); //Null Pointer Here
}
...
you should be using dialog.findViewById(R.id.dlg_add_proj_test_text) instead of just findViewById. The findViewById uses the activity's method while dialog.findViewById uses the method in the dialog.