Android;Body of ProgressDialog? - android

I saw this source as layout of AlertDialog in res\layout folder of Android SDK:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/body"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:paddingLeft="8dip"
android:paddingTop="10dip"
android:paddingRight="8dip"
android:paddingBottom="10dip">
<ProgressBar android:id="#android:id/progress"
style="#android:style/Widget.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="10000"
android:layout_marginRight="12dip" />
<TextView android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</FrameLayout>
I tried to find TextView that it's id is "message" like this:
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
It works fine.Then I tried to find LinearLayout that it's id is "body" like this:
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);
But eclipse show this error:
body cannot be resolved or is not a field
This is snippet code:
ProgressDialog dialog = new ProgressDialog(WriteOpinionActivity.this);
dialog.setMessage("some text");
dialog.show();
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(android.R.id.body);
Why this error occurs and how I can reach "body"?

Basically, not all resources are accessible to You from platforms res folder.
E.g. I'm able to find #+id/message in global_actions_item.xml, usb_storage_activity.xml, alert_dialog_holo.xml, js_prompt.xml, progress_dialog_holo.xml, progress_dialog.xml, alert_dialog.xml (for API 17), and so, there's no warranty that You get id which defined in progress_dialog.xml (it can be defined in another file and just because of the same name You is able to find it in progress dialog).
Actually, public resources are defined here, however, it looks quite outdated, so some resources might be missed. Checkout these questions about lists of all accessible resources: 1 and 2

Use this instead.
TextView tvMessage = (TextView)dialog.findViewById(R.id.message);
LinearLayout body = (LinearLayout)dialog.findViewById(R.id.body);
You weren't actually using android.R.id.message in the first place. #+id/message means you are creating a new id resource named message.
To use an id that exists in the Android framework, you can see here that you use #android:id/progress. And in source, you use it like you had it: android.R.id.progress.
Otherwise, use android:id="#=id/myIdentifier" in XML and
findViewById(R.id.myIdentifier) in code.
http://developer.android.com/guide/topics/resources/overview.html

Related

Edit .xml using simple requests

What I need to do is, give the app into hands of a non-programmer and make it simple for him to edit the xml and java files. To do that I thought maybe sending a simple request (such as, add product(name, description, price)) through some portal could edit the xml file and java file. For example, I need to repeat the similar code every time a new product request is posted:
activity_main.xml
<LinearLayout
android:id="#+id/hw5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="10dp"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hardware 5"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="$-"/>
</LinearLayout>
MainActivity.java
LinearLayout hw5;
hw5 = (LinearLayout) findViewById(R.id.hw5);
hw5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent hw5 = new Intent(MainActivity.this, Hw5.class);
startActivity(hw5);
}
});
Thanks!
What you might be looking for here is a domain-specific XML file paired with an appropriate XSL transformation. The XML file that the non-technical person would be editing would define the business entities, relationships with them and potential actions performed on them. The XSL transformation would convert this into application code at build time.
While this is not a common approach, it is rather robust. Keep in mind tho, that such an approach is difficult to maintain and requires that your business logic is mature enough before implementation.

Automatically Generating java code for xml layout in Eclipse

I have got an idea to get rid of some coding when we do initializion of views.
When we create an xml layout in android. At that movement same name class is created with UpperCase Letters with a dialogue with permission. And as i created views with ids. It should create Views and initialize them automatically.
for e.g
close.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/closeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/close_img" />
<TextView
android:id="#+id/closeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing Text" />
</LinearLayout>
And automatically generated java code should be.
public class Close extends Activity
{
TextView CloseText;
Button CloseBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CloseText = (TextView)findViewById(R.id.closeText);
CloseBtn = (Button)findViewById(R.id.closeBtn);
}
}
I can read xml and do the other stuff as I explained. But how could i add this module to eclipse and How could i create a service which work in Background all the time in eclipse.
Suggest me how to add this module(Plugin) to eclipse and i am going to use JAXB to generate Java Objects from XML document. Is there any better option.
Here in this website i find that just paste the xml and you will get your java code ready for activity class.
i had attached the link

button press in Android app creates casting error

I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skytrek"
/>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/edit_message" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Try cleaning your project and run it again..
Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)

MigLayout for design android Forms

I am on a project. My instructor has told me to use MigLayout to design the forms in android. I am finding it difficult to find proper examples and while Using Eclipse for development its showing errors that android perfix is required to use the attributes.
please help me with some examples..
code that i pasted from the source site,mig4android.
<com.saynomoo.mig4android.MigLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout_constraints="wrap 4, debug 1"
column_constraints="[]15[75px]25[min]25[]"
row_constraints="[]15"
>
<TextView android:text="label1" component_constraints="skip"/>
<TextView android:text="label2" />
<TextView android:text="label3" component_constraints="wrap"/>
<TextView android:text="label4" />
<TextView android:text="label5" component_constraints="wmin 30"/>
<TextView android:text="label6" component_constraints="wmin 30"/>
<TextView android:text="label7" component_constraints="wmin 30"/>
</com.saynomoo.mig4android.MigLayout>
The component_constraints gave the error
This error means you forget to add android word before any attribute in xml file.
For example-
id="#+id/my_location"
You need to write android before id like below.
android:id="#+id/my_location"
Your code is perfect. But "skip" in component_constraints appears to be ignored. I had to manually add place holder to the layout to "fill in" empty spots. I tried "skip 1" and that also had no impact.
For more information see following link.
Skip as a constraint is not working

TextView not Found

This is driving me absolutely crazy and Im not sure what is going on.
I have an xml layout with a TextView within a clickable RelativeLayout.
<RelativeLayout
android:id="#+id/bg_section"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#color/almost_black"
android:clickable="true"
android:onClick="goToBG"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Go To B.G."
android:textColor="#color/white"
android:textSize="20sp" />
<ImageView
android:id="#+id/bg_arrow"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="-10dp"
android:src="#drawable/arrow_icon" />
<TextView
android:id="#+id/current_bg_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/bg_arrow"
android:text="3"
android:textColor="#color/holo_blue"
android:textSize="22sp" />
</RelativeLayout>
Now in my code I try to update the textview "current_bg_count"
private void updateBGCount(){
try{
RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section);
TextView bgCountTV = (TextView) bgSection.getChildAt(2);
bgCountTV.setText(tempBG.size());
}
catch(Exception e){
e.printStackTrace();
Logger.d(TAG, "exception in updateBGCount");
}
}
This gives a ResourceNotFountException on the line where I setText although the RelativeLayout is found without a problem. Even when I try finding it just by id like this:
TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count)
bgCountTV.setText(tempBG.size());
it gives the same error.
All the other views in the layout are found easily and updated just fine. Only this one TextView is giving me the problem. Does anyone have any idea what the problem is?
You need to convert your size of the ArrayList to a String at this line
setText(tempBG.size())
Since tempBG.size() returns an int then setText() is looking for a resource with the id of whatever that returns. You are using this setText(ResId int) method which is used for if you have a string resource that you want to use to set the text.
So change it to
setText(String.valueOf(tempBG.size()));
Try setting your tempBG.size() to string like this
bgCountTV.setText(""+tempBG.size());
This should work
Are you using eclipse? Check there is no errors and warning in your project. Sometimes there are errors in xml that let you compile, but doesn't work properly.
the Problem is with this code
bgCountTV.setText(tempBG.size());
tempBG.size() this code is returning an int value which it is assuming as string Resource id something like R.string.VARIABLE_NAME coz it have a corresponding int value which TextView is assuming as id
What you can do
bgCountTV.setText(tempBG.size()+"");
OR
bgCountTV.setText(String.ValueOf(tempBG.size()));
OR
bgCountTV.setText(tempBG.size().toString());

Categories

Resources