I have been trying to work out my first android app with samples and code from the internet.
Following them I got the following error
**"Brew_Time_Add cannot be resolved or is not a field."**
for the line in the code
**Button brewAddTime = (Button) findViewById(R.id.brew_time_up);**
The xml for this part -
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dip">
<Button
android:id="#+id/brew_time_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40dip" />
<TextView
android:id="#+id/brew_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:00"
android:textSize="40dip"
android:padding="10dip" />
<Button
android:id="#+id/brew_time_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dip" />
</LinearLayout>
The Java Code for the same -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button brewAddTime = (Button) findViewById(R.id.brew_time_up);
Button brewDecreaseTime = (Button) findViewById(R.id.brew_time_down);
Button startBrew = (Button) findViewById(R.id.brew_start);
TextView brewCountLabel = (TextView) findViewById(R.id.brew_count_label);
TextView brewTimeLabel = (TextView) findViewById(R.id.brew_time);
}
Why is the error and how can it be solved?
Please see image at- http://db.tt/kbjq6u5o
Thanks in advance.
Do you have such an image in your layout? If you have then save your project and clean it.
Got to project -> clean to clean it.
You are missing the xmls declaration in your layout - add xmlns:android="http://schemas.android.com/apk/res/android" to your layout, like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dip">
<Button
android:id="#+id/brew_time_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="40dip" />
<TextView
android:id="#+id/brew_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:00"
android:textSize="40dip"
android:padding="10dip" />
<Button
android:id="#+id/brew_time_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="40dip" />
Though you will still have errors for both brew_start and brew_count_label as they are not in the xml.
Add these in the layout file. Your layout file is missing these 2 elements
<TextView
android:id="#+id/brew_count_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:00"
android:textSize="40dip"
android:padding="10dip" />
<Button
android:id="#+id/brew_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:textSize="40dip" />
Related
I'm new to Android studio, and I need to write something on the Button here but the text doesn't show, what can I do to solve this?
Note that the LinearLayout showed in the code is placed inside another bigger LinearLayout.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/datee"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="#drawable/yellow_border"
android:text="hiiii"
android:gravity="center_horizontal"
android:paddingHorizontal="10dp"
android:textSize="15sp"></Button>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:text="Auction date: "
android:id="#+id/done"></TextView>
</LinearLayout>
you can use somthing like this
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/datee"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:text="hiiii"
android:gravity="center_horizontal"
android:textSize="15sp" />
</LinearLayout>
and try to change the android:background="#drawable/yellow_border"
because you might have some padding inside of it
I do try to get text datas from JSON, i do. But when it is time to write they are not exactly fitting. My XML file is here...
<?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:orientation="vertical" >
<TextView
android:id="#+id/txt_questionAdjective"
android:layout_width="fill_parent"
android:layout_height="139dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="15dp" />
<RadioGroup
android:id="#+id/grp_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rdb_optionA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rdb_optionB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionB"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/rdb_optionC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:id="#+id/rdb_optionD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/txt_optionD"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
And what it looks like after that.
How can i set C and D options next to radiobuttons ? Thanks in advance.
EDIT:SOLVED I deleted the textView parts in XML and used the for loop which #Gabriella Angelova provided me. That really helped. Thanks.
You could use the following code snipet to set the text of your radio buttons dynamically:
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.grp_options);
for (int i = 0; i < radioGroup .getChildCount(); i++) {
((RadioButton) radioGroup.getChildAt(i)).setText(*your value from JSON*);
}
radioButton.setText("your text");
I'm actually coding an app for Android and I need some help to do something.
I need to do a kind of popup message with this image: http://img716.imageshack.us/img716/9331/postitz.png. This image must have 2 TextView, one of them is a title and the other one is the message.
I have tried some options like Dialog with its own layout, own style, but the image fills all the screen instead of wrap the content of the TextViews. I have also tried to do it with a new Activity and occurs the same, the Image fills all the screen.
My actual layout is this:
<?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:orientation="vertical"
android:gravity="center_vertical"
android:background="#android:color/white">
<LinearLayout
android:id="#+id/img_postit"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/postit"
android:orientation="vertical">
<TextView
android:id="#+id/note_titulo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="30dp"/>
<TextView
android:id="#+id/note_descr"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="30dp"/>
</LinearLayout>
</LinearLayout>
But I have tried with a lot of different combinations, RelativeLayout, FrameLayout, and I cannot find the solution to do this.
Any idea of how to resolve it?.
Thank's for all.
you said you used as a new activity. Did you use android:theme="#android:style/Theme.Dialog in your manifest file?
And be sure that the image is little bit smaller one such that it will be suitable to act as a pop up..
If you used a dialog what exactly is the problem? Didnt you get the dialog as a pop up ?
(i think the image is larger so that it might fill the entire screen)
Create your own class extending dialog.And use layout like following. I use this in one of my apps and the dialog window is wrapped around the buttons :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_below="#+id/ll1"
android:gravity="left"
android:orientation="horizontal"
android:paddingBottom="5dp" >
<RadioButton
android:id="#+id/start"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/stop"
android:background="#drawable/start_on"
android:button="#android:color/transparent"
android:checked="true" />
<RadioButton
android:id="#+id/stop"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="#drawable/stop_off"
android:button="#android:color/transparent" />
<RadioButton
android:id="#+id/restart"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/stop"
android:background="#drawable/restart_on"
android:button="#android:color/transparent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lltext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/detail_credit"
android:layout_below="#+id/ll2"
android:layout_marginBottom="15dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/startText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/stopText"
android:text="#string/start_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/stopText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:text="#string/stop_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/restartText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/stopText"
android:text="#string/restart_server_icon"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/lltext"
android:layout_centerHorizontal="true"
android:text="OK" />
</RelativeLayout>
final Dialog dialog_my = new Dialog(FIRST.this,
android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog_my.setContentView(R.layout.about_us); //<PUT YOUR LAYOUT ID HERE>
TextView date = (TextView) dialog_my.findViewById(R.id.date);
TextView thought = (TextView) dialog_my.findViewById(R.id.thought);
TextView ok = (TextView) dialog_my.findViewById(R.id.ok);
TextView link = (TextView) dialog_my.findViewById(R.id.link);
TextView contact = (TextView) dialog_my.findViewById(R.id.contact);
WindowManager.LayoutParams lp = dialog_my.getWindow().getAttributes();
lp.dimAmount = 0.0f;
dialog_my.getWindow().setAttributes(lp);
dialog_my.getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog_my.show();
I made a TextView and a button. Clicking on the button will save the value entered by the user in a variable. The problem is, when the keyboard pops up after touching the textview, only the first character will be visible properly. The rest of the characters aren't displayed. An _ appears instead of the other characters. After i'm done typing and press the back button and when the keyboard disappears, I can see the characters properly. What is the problem?
Button okbutton = (Button) findViewById(R.id.okbutton);
okbutton.setClickable(true);
final TextView radius = (TextView) findViewById(R.id.radius);
okbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
i=radius.getText().toString();
}
});
Here is the complete layout
<?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">
<ImageView android:id="#+id/icon" android:src="#drawable/settings"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="10px"></ImageView>
<TextView android:textSize="20px" android:typeface="sans"
android:layout_height="wrap_content" android:id="#+id/alarmset"
android:layout_width="fill_parent" android:text="#string/alarmset"
android:textStyle="bold" android:gravity="center"
android:drawablePadding="10px" android:paddingTop="10px"></TextView>
<TextView android:text="#string/distance" android:id="#+id/distance"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/alarmset"></TextView>
<TextView android:id="#+id/kmvalue" android:layout_below="#+id/distance"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="#string/km" android:id="#+id/km"
android:layout_below="#+id/distance" android:layout_toRightOf="#+id/kmvalue"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingLeft="10px"></TextView>
<TextView android:text="#string/musichooser" android:id="#+id/musictext"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/km" android:paddingTop="10px"></TextView>
<Button android:id="#+id/chooser" android:layout_below="#+id/musictext"
android:drawableRight="#drawable/lov_ena"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="30px" android:layout_alignBaseline="#+id/musicfile">
</Button>
<EditText android:id="#+id/musicfile" android:layout_width="fill_parent"
android:layout_below="#+id/musictext" android:layout_toLeftOf="#id/chooser"
android:layout_height="40px">
</EditText>
<TextView android:text="#string/imgchooser" android:id="#+id/imgText"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/chooser" android:paddingTop="20px"></TextView>
<Button android:id="#+id/imgPicker" android:layout_below="#+id/imgText"
android:drawableRight="#drawable/lov_ena"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="30px" android:layout_alignBaseline="#+id/imgfile">
</Button>
<EditText android:id="#+id/imgfile" android:layout_width="fill_parent"
android:layout_below="#+id/imgText" android:layout_toLeftOf="#id/chooser"
android:layout_height="40px">
</EditText>
<RelativeLayout android:id="#+id/InnerRelativeLayout"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_centerInParent="true">
<Button android:layout_width="wrap_content"
android:layout_height="50px" android:id="#+id/setalarm"
android:layout_centerInParent="true" android:text="#string/setalarm" />
</RelativeLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imgfile"
android:text="Enter Distance" />
<Button
android:id="#+id/okbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:text="OK" />
<EditText
android:id="#+id/radius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/imgfile"
android:layout_alignTop="#+id/okbutton" />
</RelativeLayout>
This image should make the problem easy to understand
http://i39.tinypic.com/r8t64l.jpg
Try to change the TextView in your Code to EditText.
Button okbutton = (Button) findViewById(R.id.okbutton);
okbutton.setClickable(true);
EditText radius = (EditText) findViewById(R.id.radius);
okbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
i=radius.getText().toString();
}
});
try this....
by mistake you have puted any other view over your edit-text; jus go to your xml file and open Graphical layout, then find that view using mouse-pointer by clicking on that part in which your can not see the text in your shown picture, by double-clicking on that part you can goto xml, and jus remove that view from your xml
I have a dialog, which is created with this code:
final Dialog dialog1 = new Dialog(Test.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.test_layout);
dialog1.setCancelable(true);
...
The layout file 'test_layout.xml' contains a typical TextView:
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test" />
I want in the code to set this TextView invisible (gone) in some cases.
I tried following:
TextView tv = (TextView) dialog1.findViewById(R.id.tv_username);
tv.setVisibility(TextView.GONE);
But the TextView still appears in the dialog. If I set in the layout xml file android:visibility="gone" it is not appearing in the dialog. But I need to do it via code.
Here the whole code:
if (whichButton == 1) {
final Dialog dialog1 = new Dialog(Test.this);
dialog1.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog1.setContentView(R.layout.test_layout);
dialog1.setCancelable(true);
TextView tv = (TextView) dialog1.findViewById(R.id.tv_username);
tv.setVisibility(View.GONE);
...
dialog1.show();
}
Here the whole XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="250dp"
android:layout_height="match_parent"
>
<TextView
android:id="#+id/tv_name"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:textSize="14dp"
android:text="Name:" />
<EditText
android:id="#+id/username"
android:hint="#string/enterName"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:maxLength="15"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scrollHorizontally="true"
android:capitalize="words"
android:singleLine="true"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Test" />
<TextView
android:id="#+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:textSize="14dp"
android:text="#string/message" />
<EditText
android:id="#+id/tip"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:maxLength="290"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:scrollbars="vertical"
android:inputType="textMultiLine"
android:capitalize="words"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/send_message" />
</LinearLayout>
Try tv.setVisibility(View.INVISIBLE);
I found a solution or workaround.
The dialog was created in the the onClick method of an AlertDialog.
Now I use the showDialog() method and handle it in the corresponding callback method.
There it is working fine.
I do not understand why this is working, but I only want to tell other users how they can solve this issue.
I would be happy if someone can come up with a clarification.