Simple Android app fails without starting - android

Hello there people of stack overflow, I have just started developing with android and have run into a bit of a problem. I am trying to run a very simple app that has a single button, which when clicked updates the text to the current time. However, the app does not even start up. I am following a book. "Beginning Android 2", and am just learning about XML layouts. The same app worked without an XML layout, so it may be something to do with my xml.
Anyways, I'm running it on a droid 2, if that helps.
Heres the codes.
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.Date;
public class FirstApp extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
Button btn;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(this);
updateTime();
}
public void onClick(View view) {
updateTime();
}
private void updateTime() {
btn.setText(new Date().toString());
}
}
And the XML file
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Thanks in advance.

You should enclose the button inside a layout (linear, relative, etc):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

Both answers are incorrect. The only line missing in that xml is the first one:
<?xml version="1.0" encoding="utf-8"?>
Of course you will need some kind of ViewGroup to make any meaningful UI, but setContentView can take any View as parameter

The problem is that your root layout must be a ViewGroup. Change the layout to something like this for example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/button"
android:text=""
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

Related

findViewById() from another layout not updating data

I have mainActivity, that has xml that include another xml . Both activitymain layout has a button, and the included layout has another button. Both should display into the main activity.
Now , i need to change text of both button. I can get reference to first button by findviewByID , and it works. for the second button that is into the second layout, i get view by inflating the layout, It gets properly refence to the view and the findviewbyID gets reference to this button. But when changing the text, it does not change.
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools">
<Button android:id="#+id/buttonvvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
xmlns:android="http://schemas.android.com/apk/res/android" />
<include
layout="#layout/layouttest"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
layouttest.xml
<?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">
<Button
android:id="#+id/buttonaaa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
---Mainactivity.java----------------------------------
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) view.findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
}
Results
buttonvvv text is updated and becomes ssssssss
buttonaaa text is Not updated and still Button
No need for inflating the included layout, you can just findViewById like you did with b button. It's already included with activity_main.xml layout
Replace this:
View view=LayoutInflater.from(getApplication()).inflate(R.layout.layouttest, null);
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) view.findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
With :
Button b =(Button) findViewById(R.id.buttonvvv);
Button bb =(Button) findViewById(R.id.buttonaaa);
b.setText("ssssssss");
bb.setText("ssssssss");
From Docinclude:
The root View should be exactly how you'd like it to appear in each
layout to which you add this layout.
BTW, check this answer
You don't have to inflate the layout, just find it like:
LinearLayout layout =(LinearLayout) findViewById(R.id.layouttest);
and then
Button bb =(Button) layout.findViewById(R.id.buttonaaa);

Android Screen Slide action

I am just starting to learn android java in Android Studio.
And I have bum into a question related to screen slide action.
I wanted to create a screen slide action that allow me to have additional options beside my basic layout's option, after the chose of the additional option I will return to my basic layout.
The perfect example that could represent my idea would be the Google Calculator, when user need advance Math symbol, it has a green layout that show up while user sliding the right's edge to the left, and after user choose one Math Symbol, it will return to its basic layout.
This is the screen shot of the calculator,photo belong to the internet
I am not very good at explaining, I hope you guys understand what I am trying to approach.
I have it working using SlidingPaneLayout. Let me know if this works.
XML
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/SlidingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello SlidingPaneLayout!" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:background="#F44336"
android:elevation="50dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Hello SlidingPaneLayout!" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
Activity
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SlidingPaneLayout slidingPaneLayout = (SlidingPaneLayout) findViewById(R.id.SlidingPanel);
slidingPaneLayout.setSliderFadeColor(ContextCompat.getColor(this, android.R.color.transparent));
slidingPaneLayout.openPane();
}
}

Not able to add buttons and text fields to my android app

Ok so I followed the Android Tutorials at the developer.android.com to build my first app. So to create a simple user interface I added a button and text field given in the tutorial. But when I run it on my phone, I don't see the buttons or text field.
package com.example.lookforbuttons;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv= new TextView(this);
tv.setText("Buttons");
setContentView(tv);
}
}
The .xml file where I describe the layout is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
and the strings.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Buttons</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
The target android version is 4.03 since I am testing it on 4.03 phone. When I run this I only "Buttons" printed and no button or text field. Thanks.
You call setContentView twice. When you do this, the second time is what you will see on the screen because it will overwrite whatever layout you called in the first call to setContentView(). So since you call
setContentView(tv);
last you only have the TextView. Remove that line and you should see your EditText and your Button.
In your code you are setting setContentview() twice. That means you are changing the layout which contains Button and TextView with second setContentview().
If you want to dynamically add new TextView to your layout. Remove the second setContentView() and assign an id to your LinearLayout in xml. Then find it in your Java code and say linearlayout.add(textview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lv"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edittext"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</LinearLayout>
Linearlayout lv=(Linearlayout) findViewById(R.id.lv);
lv.add(textview);

Android. How to add transparent image over another image

I know how to do this via a surfaceView and I'm wondering if I should go down that rout.
I'm simply trying to create a splashscreen that has a fullscreen image with an opaque image laid over the top (after a short delay). I can't work out how this is done in XML code.
This is what I have......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/lightDot"
android:src="#drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
/>
<ImageView
android:id="#+id/bGround"
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
So my 'lightDot' object is semi-transparent and I want to overlay this on top of my bGround resource after a short delay.
This is my code:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class SplashAct extends Activity implements OnClickListener{
Button mainButton;
Button lightDot;
ImageView background;
ImageView light;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
background = (ImageView)findViewById(R.id.bGround);
light = (ImageView)findViewById(R.id.lightDot);
background.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
//finish();
Intent toMainGame = new Intent(this, ActOptions.class);
startActivity(toMainGame);
}
}
Thank you.
What you want is a FrameLayout.
Child views are drawn in a stack, with the most recently added child on top.
You can get more fancy with where the overlays go using layout_gravity, but it sounds like this is all you need.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/image" >
</ImageView>
<ImageView
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/overlay"/>
</FrameLayout>
Use Relative layout not linear layout.
This allows you to place views ontop of one another, as opposed to in a linear list.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/lightDot"
android:src="#drawable/splashlight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#null"
/>
<ImageView
android:id="#+id/bGround"
android:src="#drawable/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
To make the Dot appear after time, use android:visiblity="INVISIBLE" in the xml. (so it starts invisible)
then use light.setVisiblity(View.VISIBLE); in your code.
You could use a LayerDrawable
Define a simple LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/splash_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
In your Activity class:
LinearLayout layout = (LinearLayout) findViewById(R.id.splash_layout);
Drawable drawableLayers[] = new Drawable[] {getResources().getDrawable(R.drawable.splash), getResources().getDrawable(R.drawable.splashlight)};
// ^ The order of drawables is important: The above line overlays splashlight on top of splash.
LayerDrawable layerDrawable = new LayerDrawable(drawableLayers);
layout.setBackgroundDrawable(layerDrawable);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
***android:background="#drawable/splash"*** >
// Try this for invisible
iv.getBackground().setAlpha(0);
//Try this for visible
iv.getBackground().setAlpha(255);
//What great about this is that you could create a loop
//to slowly increment the alpha, creating a fade effect instead of
//invisible to visible.

Why the program not found activity_main and web_view?

I write a simple program and when i run the program, the program has errors. I have a activity_main.xml in layout folder but the program has error of activity_main. Why? What is the problem?
errors: activity_main cannot be resolved or is not a field
web_view cannot be resolved or is not a field
activity_main.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">
<WebView
android:id="#+id/web_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button2"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
Button button1;
Button button2;
WebView mWeb;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWeb= (WebView)findViewById(R.id.web_view);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v){
//don't work something
}
};
}
Sometimes Eclipse adds
import android.R
to the list of imports. This is an error and it should be deleted. Removing it sometimes resolves the described problem.
Try following:
Clean you workspace and try to run again
Check if R.java is created in gen folder or not and what is the package name there?
Make sure your Web view and R.Java share the same package name if not , change the package name of your WebView or change in manifest.xml.
Try closing and opening eclipse again.

Categories

Resources