Problem with basic Android xml GUI - java

I've just started working on a basic Android GUI using XML - when I run it, logcat tells me "Binary XML file line #16: You must supply a layout_width attribute."
When I try and the program, it throws the error "The application HelloAndroid has stopped unexpectedly.
Its the basic hello Android code -
UPDATE - Got it so far! Thanks for the help! :)

There is a part in your layout:
<TextView
android:text="#string/main_title"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
android:marginBuottom="20dip"
android:textSize="24.5sp" />
As you can already see from the code highlighting here, some parts of this element are black. Thats because you closed this TextView via /> on the second line. All the other parameters are not belonging to this element anymore. Therefore the parser can't find layout_width, layout_height and so on - and complains about this ("you didnt specify ..."). Remove the /> on the top and you should be fine.
Edit: The correct version should look like this:
<TextView
android:text="#string/main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:marginBottom="20dip"
android:textSize="24.5sp" />
or alternatively (since this here is the shorthand version
<TextView
android:text="#string/main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:marginBottom="20dip"
android:textSize="24.5sp"> </TextView>

This view looks OK. But check your landscape xml. There's an issue with layout_width property in the xml - it's missing or maybe there's some typing mistakes (usually android:layout_width replaced with android.layout_width).
android:layout_width and android:layout_height properties are required for every control you add to the xml.
EDIT:
<TextView
android:text="#string/main_title"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
android:marginBuottom="20dip"
android:textSize="24.5sp" />
You are ending your TextView element after the android:text property - android:text="#string/main_title"/>. Remove />
EDIT:
Also you have an typing mistake in android:marginBuottom="20dip". This line should be a android:layout_marginBottom="20dip".

Related

Card view not working below Lollipop version

I am using card view but the elevation and card view properties are not working below version 5.0.
I tried to use card_view:cardUseCompatPadding="true" by searching some SO posts but this also did not work.
It should look like this
And it looks like this on 4.2.2
Here is layout:
<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/tools"
android:id="#+id/card_view"
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="05dp"
android:layout_marginTop="05dp"
card_view:cardElevation="12dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/bg">
<View
android:layout_width="3dp"
android:layout_height="match_parent"
android:background="#color/cardLineColor"></View>
<LinearLayout
android:id="#+id/linearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txt_id"
android:layout_width="103dp"
android:layout_height="wrap_content"
android:text="ID"
android:textColor="#color/white" />
<TextView
android:id="#+id/txt_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="05dp"
android:text="23/3/2015"
android:textColor="#color/textColor"
android:textSize="10sp" />
</LinearLayout>
<TextView
android:id="#+id/txt_trans_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/linearLayout6"
android:layout_centerHorizontal="true"
android:text="sent money"
android:textColor="#color/white"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/linearLayout8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/linearLayout6"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/txt_trans_type"
android:layout_toRightOf="#+id/txt_trans_type"
android:orientation="vertical">
<TextView
android:id="#+id/txt_balance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="$3214"
android:textAlignment="viewEnd"
android:textColor="#color/white"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="05dp"
android:text="#string/balance"
android:textAlignment="viewEnd"
android:textColor="#color/white"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
Edit: I created 9 patch file like this
But this is giving this error:
Error:Some file crunching failed, see logs for details
Error:Execution failed for task ':app:mergeDebugResources'.
> Error: Some file crunching failed, see logs for details
May be its height width is much? I tried with small height width but then it leaves some gap. How can I match it to all devices?
Use app:cardElevation="12dp"
instead of card_view:cardElevation="12dp"
Hope it helps.
Sadly, elevation, as you mentioned it, is not available for API 19 and below.
The solution I chose for my similar issue is to replace my cardviews by regular layouts and use 9-patch images as background to generate the shadows. It has the advantage to be very modulable, plus, unlike the elevation property, you can have more customization like shadow colors if you don't want a simple black shadow.
The only problem is that you have to design the image yourself and add it to the project. But fear not, there is some handy generators like this one that can generate it for you. Just make sure that you set a minimum size that is less than your layout to be sure it fits without deforming it. I usually generates a 10*10 pixels wide image, and shadows won't be deformed even when applied to a much larger view.
When you got your image (name should look like myimage.9.png), you simply add it to your drawables, without removing the .9.png extension, and then set it as a regular background drawable to your layout, via xml or code. And you're done !
EDIT : Did not notice you also have lighting on the top of the cardview.. What you can try if you have something like Photoshop, is to create yourself the 9-patch, by applying drop shadows for the dark one, then creating a second layer to add another drop shadow but this time set to white and with the appropriate angle to display it above. Then you save it as png and use the 9-patch generator to convert it to a .9.png image.
See the 9-patch documentation here. If you're using Android Studio, there is also a built-in generator for 9-patches.
Sadly, user interface was kinda limited for advanced displaying like shadows before Material Design stepped in with Android 5. Maybe you can add your cardviews programmatically, so you can do something like this
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.LOLLIPOP){
//Add regular layout to your views with 9 patch
} else{
//Add cardview to get desired effect
}
That way most of your users will have your desired cardviews, but it can quickly increase the amount of code you'll need if you need your element to be clickable/draggable/etc.

abbreviations in android via xml or java

first of all.
i have tried searching my question in stackoverflow and not founded the exact answer there are some related questions but those are not the answer of my questions.
may be i dont know what we call that thing which i gonna ask you.
i am creating an xml file in eclipse in which i am going to show abbreviations of words.
for example
fb: facebook //no new line added for bigger screen
gm: gmail
sof: stack over flow
these are just examples.
in my code i want to create 2 textviews or something like that
first textview will show the abbreviations and 2nd will show the exact English for that abbreviation.
but some times on small screens the abbreviation may go out of screen or may include /n or new line and my all other abbreviations will be wrong like this example.
fb: face //a new line added for small screen
gm: book
sof: gmail
:stack over flow
basically i want you to tell me that how to arrange the code so the textview should either scroll and do not include the new line.
or if textview2 include enter than textview1 should have an enter too.
please help me
You should use TableLayout class and HorizontalScrollView
Just try something like this
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height=wrap_content"
android:fillViewport="true">
<TableLayout
android:layout_width="wrap_content" <!-- text should go along one line for each row-->
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="fb:"/>
<!-- when singleLine atrubutte set : ) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="facebook"/>
</TableRow>
</TableLayout>
</ScrollView>

android TextView doesn't show hebrew fonts

I'm trying to present Hebrew text in m TextView.
I have read here I need to change the font from default android's to a hebrew supporting one.
I have tried few solutions, but none of them worked:
Typeface font= Typeface.createFromAsset(mContext.getAssets(), "fonts/Raanana.ttf");
titleTextView.setTypeface(font);
titleTextView.setText(mTitles[position]);
or:
titleTextView.setText(Html.fromHtml(mTitles[position]));
here is my xml:
<ImageView
android:id="#+id/todo_row_image"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="21dp"
android:contentDescription="my image" />
<TextView
android:id="#+id/todo_row_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lines="1"
android:text="#+id/titleTextView"
android:layout_toRightOf="#id/todo_row_image"
android:textSize="12sp" >
</TextView>
<TextView
android:id="#+id/todo_row_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/todo_row_title"
android:layout_marginTop="6dp"
android:lines="1"
android:text="#+id/dateTextView"
android:textSize="12sp" >
</TextView>
any idea how to solve this?
is there a way to define default font per application? per Layout?
Hebrew in a TextView works well "right out of the box". Maybe there are issues with the BIDI, but generally it displays it good.
Regarding you second question, how can you set up a layout to be used when using hebrew, take a look at this:
RTL Languages support in android and resource qualifiers
Base on my experience, if you specify on the TextView android:singleLine="true" it will not show Hebrew characters, it's definitely a bug in the android source code and might be fixed in a later release.
based on that I believe that android:lines="1" is what is causing the issue for you.

Eclipse not displaying TextView

Okay, so the thing is that I'm trying to display text but it won't let me. the code is :
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAlignment="center"
android:textColor="#000000"
android:textColorLink="#android:color/black"
android:textStyle="normal" />
What have I done wrong? (the text won't appear on screen)
At your string resources app_name might be empty or null.
There is another view that prevents your TextView to be displayed at your screen, take a look at your layout file.
The value for TextView modified to a null/empty value in your code.
Try this
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Flights"
android:textColor="#android:color/white" />
Your code is correct as it is working for me. The probable mistake you have done is that your String resource variable "app_name" is empty or you can provide the full xml code for us to have a better look at this problem.

Crashing due to "No package identifier when getting value of resource number..." [duplicate]

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.

Categories

Resources