I'm trying to put an image as background, but I don't understand why it doesn't work.
This is my activity_start.xml
<?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="match_parent"
android:background="#drawable/startbackg" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="153dp"
android:textColor="#555555"
android:textSize="30sp"
android:text="Hello" />
</RelativeLayout>
and the StartActivity.java
import android.app.Activity;
import android.os.Bundle;
public class StartActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
}
In eclipse I can see correctly the background, like this:
But when I run it in a nexus 7, I can't see any background image
I don't know what to try... any ideas?
This is the image I'm using
use
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
myLInearLayout.setBackground(myImage);
myLInearLayout.setBackgroundDrawable(myImage);
} else {
myLInearLayout.setBackground(myImage);
}
Related
when I close my application I want to remove it from the "recent app list" also in android.
I created an empty project and activity.
Here my main XML file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rebirthing.clear_recent_list_app.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:onClick="closeApp"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Here My MainActivity.java file:
`package com.example.rebirthing.clear_recent_list_app;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void closeApp(View v){
finish();
}
}
`
When I click the TextView which inside of my Xml File. It closes my App. but when I Check "Recent App List" its still there. How do I fix it?
if you want to do something with your app when you close your application.
then use this method
protected void onDestroy() {
super.onDestroy();
//do what you want to do here
}
this method is called when you close your application.
if you find this answer useful then please mark it correct.
I'm brand new to Android Studio and for whatever reason am experiencing really weird errors if I could please get some help.
I have very basic code that is supposed to, on button click change the text of my button from "button" to "clicked!". However everytime I press the button, the app crashes and I get "Appname has stopped" on the emulator.
What is incredibly weird is that in my activity_main.xml Design view, the onClick dropdown shows two functions of the same name (https://puu.sh/t2h5I/42ad4379d6.png)
H owever the code only works when the bottom one is selected. AND each time I run the app, it deselects the bottom one and reselects the top, only to stop working.
Here is my MainActivity:
package com.example.john.ameladay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button melButtonCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonPress(View v){
melButtonCode = (Button) v;
((Button) v).setText("Has been clicked!");
}
}
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.john.ameladay.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mainText"
android:id="#+id/textView" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/mela"
android:id="#+id/melPhoto" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginBottom="43dp"
android:id="#+id/button"
android:onClick="buttonPress (MainActivity)" />
Any help would be greatly appreciated!!
Simple. You should write:
android:onClick="buttonPress"
Why happened
If you wrote buttonPress (MainActivity), Android tries to find buttonPress (MainActivity) method (not MainActivity.buttonPress()), but MainActivity doesn't have buttonPress (MainActivity) method. So the error happened.
Simply replace this Tag in button
Remove this
android:onClick="buttonPress (MainActivity)"
And Paste This
android:onClick="buttonPress"
A better way to do it is, get a reference to the button in your Java code using findViewById() method and set an OnClickListener to the button.
For your current problem, use
android:onClick="buttonPress"
instead of
android:onClick="buttonPress (MainActivity)"
According to me this is the better way to set click on button
public class MainActivity extends AppCompatActivity {
public Button melButtonCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
melButtonCode = (Button).findViewById(R.id.button);//find button by Id
melButtonCode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
melButtonCode.setText("Has been clicked!");
}
});
}
}
For your problem
replace it
android:onClick="buttonPress (MainActivity)"
With
android:onClick="buttonPress"
Make the method public - protected works when you are in instantrun mode, but not when not. No idea why!
I am new to android programming and there are a few things I don't quite know how to do yet. I am doing a course on udemy and was trying to put together everything I have learned up to a certain point.
What I am trying to do is have the user click on a button (i have 12) and have it bring up a textField where they can enter two numbers. I just want to be able to get the user's two numbers and i'm pretty sure I can figure out the rest ( I hope). I just don't understand how to go about doing this.
Any help would be much appreciated. Basically all I want to do is to be able to have the user click on one of the 12 buttons and be asked to enter two values, then take those values and perform a calculation on it.
Your xml could be like 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" >
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/b_ok"
android:text="Click Me"/>
<EditText android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/et_showme"/>
</LinearLayout>
and your activity could be like this:
package com.william.kinaan.welcomeback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends Activity implements OnClickListener {
private Button b_ok;
private EditText et_showme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
initialize();
}
private void initialize() {
this.b_ok = (Button) findViewById(R.id.b_ok);
this.b_ok.setOnClickListener(this);
this.et_showme = (EditText) findViewById(R.id.et_showme);
}
#Override
public void onClick(View v) {
this.et_showme.setVisibility(View.VISIBLE);
}
}
Create an Activity that has 2 EditTexts
When user click a button the Activity is started and user can enter the Numbers
Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_id"
android:visibility="invisible"
android:text="sample text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/click"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in your activity, you can do like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button = (Button) findViewById(R.id.click);
final EditText text = (EditText) findViewById(R.id.tv_id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
}
});
}
Using Kitkat but also tried on jellybean and im not able to get a custom error icon to appear.
Im using these two overloaded methods for setError(...) from Android doc
Im just doing very simple code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity implements OnCheckedChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.report_issue);
RadioGroup rg = (RadioGroup) findViewById(R.id.myradio_group);
rg.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radio_two) {
EditText et = (EditText) findViewById(R.id.et_city);
// rb.setError("i got a single error");
et.setError("my error",
getResources().getDrawable(R.drawable.ic_launcher));
//as you can see from above im setting a image for the error thru code
et.requestFocus();
}
}
}
heres my mylayout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mycontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/myradio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="radio1" />
<RadioButton
android:id="#+id/radio_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="radio2" />
</RadioGroup>
<EditText
android:id="#+id/et_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter city" />
</LinearLayout>
Here is a screenshot of what im seeing on jellybean and kitkat:
here is a image where its working fine when i remove the custom image icon and only call :et.setError("my error");
I actually got it to work. I had to first set the bounds on the drawable something like this:
Drawable d= getResources().getDrawable(R.drawable.ic_launcher);
d.setBounds(0, 0,
d.getIntrinsicWidth(), d.getIntrinsicHeight());
et.setError("my error",d);
Android OS bug with some devices running Jelly Bean/4.2.1 - TextView.setError(CharSequence error) Missing icon
i found this and it working like charm, hope this work with you too
I have a problem in layout. In my activity at first I show a splash screen and after that based on some condition i need to show different layouts.when i display one layout it looks like transparent white (like splash view) and other one is ok because its background color is matching with it.
When ever i press on transparent white view(text view) it looks normal. That is it acts as button in the sense on pressing and releasing different views.
I tried with giving background color but still problem exist.Unfortunately i need to give white color only for that view.
Can any one help me to change that transparent white showing in my new layout
this is layout of view having background problem
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/white"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/abc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/button_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="5dp"
>
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="#string/btn1"
android:background="#drawable/btn1"
/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/btn2"
android:background="#drawable/btn2"
/>
</RelativeLayout>
In code
oncreate(){
setcontentview(splashscreen)
setview()
}
void setview(){
if(condition1){
setContentView(R.layout.a1);
}
else{
setContentView(R.layout.a2);
}
}
Why don't you just move to another activity when the splash ends? Like this. Just for the sake of SO, I copy the code of that page here:
package com.itcuties.tutorial.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
public class SplashActivity extends Activity {
private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 5; // Sleep for some time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.splash);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
}
}
}