Layout : Splash screen background is still showing when new layout comes - android

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();
}
}
}

Related

How to make a texField open with click of a button?

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);
}
});
}

Can't put an image as android activity background

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);
}

Respond to send button

I installed the Android SDK bundle today and I am following the "My First App" tutorial and I am stuck, it states:
Open the MainActivity class (located in the project's src/ directory) and add the corresponding method:
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Where do I put this in the file? and is this the "MainActivity.java" file?
I have tried and I keep getting errors so I am obviously going wrong somewhere.
activity_main.xml:
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MainActivity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Hope I have made my problem clear, I looked on the forum for an answer but I couldn't find anything.
If you have a button in your(say activity_main.xml) xml layout and you have the below attribute for button
android:onClick="sendMessage"
and you have the below in MainActiivty.java
setContentView(R.layout.activity_main);
You should have the below in MainActivity.java
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
Example:
MainActivity.java
// Your imports
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //setting the layout to activity
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
activity_main.xml
<?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:orientation="vertical">
// other widgets
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:onClick="sendMessage"
android:text="Button" />
</RelativeLayout>
If you are a new android developer and doing your first so start from basic like launch new activity it contain hello world or any text view, button then you will clear idea about application.
create your android application
in XML layout drag the button and text view
run your first app.
you will get your output.
Put it in MainActivity.java at the top right after
public class MainActivity extends ActionBarActivity {
After you do this, you may have to import. Do this by pressing control / shift / O (not zero)

Simple Android slideshow with a parameter and button

i'm a newbie of android :)
I want to create a simple slideshow with a numeric parameter(x) and a button.I have 21 images and, at the click of the button, I want to catch x and "slide" only the first x images of the list.Between one image and the next i want wait 1 second.
At the end i want to know how much time passed during the operation. (sorry for my english)
paste below the code of the activity and the layout:
package com.superpibenchmarknative;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageActivity extends Activity implements OnClickListener {
private Button start;
private EditText iteractions;
private TextView time;
private ImageView display;
private long after,before;
private int images[] = {R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,
R.drawable.image8,R.drawable.image9,R.drawable.image10,R.drawable.image11,
R.drawable.image12,R.drawable.image13,R.drawable.image14,R.drawable.image15,
R.drawable.image16,R.drawable.image17,R.drawable.image18,R.drawable.image19,
R.drawable.image20,R.drawable.image21,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
setUpUI();
}
private void setUpUI() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.bntStartCalcolation);
iteractions = (EditText) findViewById(R.id.etIterations);
time = (TextView) findViewById(R.id.tvTime);
start.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
//Creo le imageview in base al # di iteractions
before = System.currentTimeMillis();
for (int j = 0; j < Integer.parseInt(iteractions.getText().toString());j++){
display = (ImageView) findViewById(R.id.ivDisplay);
display.setImageResource(images[j]);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
display = null;
System.gc();
}
}, 1000);
}
display = null;
System.gc();
after = System.currentTimeMillis();
time.setText(String.valueOf(after-before-1000* Integer.parseInt(iteractions.getText().toString())));
}
}
and this is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ImageActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Benchmark del rendering di immagini" />
<EditText
android:id="#+id/etIterations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:hint="# di iterazioni"
android:inputType="number"
android:padding="5dp" />
<Button
android:id="#+id/bntStartCalcolation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text="Start Calcolation" />
<ImageView android:id="#+id/ivDisplay"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight="50"
android:text="Risultato ottenuto in: " />
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight="50" />
</LinearLayout>
this code dosn't work, it create an exception
OutOfMemoryError: bitmap size exceed the vm budget
anyone can help me to solve this problem?? :) thanks at all however :D
great all works :D but i've a little more questions
I don't understand why the time is always negative!!!
anyone of you know why?? [postDelayed not works?!? :( ]
solved with: android.os.SystemClock.sleep(1000);
Another error, the ImageView dosn't change after the setting of an image, any suggestions??
There is a small guide within the developer site regarding loading large bitmap files. The idea is to load lower resolution files so that they should fit in memory: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Add the largeheap to your application in your Manifest file.
<application ... android:largeHeap="true" >
Alternatively, make your photos smaller. You might have a variety of sizes dependent on what size of screen you are using, that will make it much less likely to take all of the memory up. See this page to find Android screen resolutions.

How to add animation to the linear layout?

I am using linear layout to get the message with image from .net server.
when the new message is come the position of the layout is increased and the new message is added to the top of the layout one by one.
the problem is when the new message will come,the new message is added to layout suddenly.
I want to apply animation to the layout and make my app like when the new message is come the message is added to the layout slowly. means the previous messages move down slowly and new message is added top of the layout.
Use android:animateLayoutChanges on the LinearLayout that shall hold the data. This will trigger an animation when adding new content. It starts by moving the old data down making room for more content. Then follows a second step where the new data will fade into the free space.
Example code
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/baseLL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- button used to add data -->
<Button
android:layout_width="192dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add Content"
android:onClick="onAddContentClick" />
<!-- button used to remove data -->
<Button
android:layout_width="192dip"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove Content"
android:onClick="onRemoveContentClick" />
<!-- data will be added to this LinearLayout at run time -->
<LinearLayout
android:id="#+id/dataLL"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
>
</LinearLayout>
</LinearLayout>
basicanimation.java
package com.test.animation.basic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class BasicAnimationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onAddContentClick(View v) {
LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
int dataCount = dataLL.getChildCount();
TextView newDataTV = generateData(dataCount);
dataLL.addView(newDataTV, 0);
}
public void onRemoveContentClick(View v) {
LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
if (dataLL.getChildCount() > 0) {
dataLL.removeViewAt(0);
}
}
private TextView generateData(int dataCount) {
TextView TV = new TextView(this);
TV.setText("Data " + dataCount);
TV.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
return TV;
}
}

Categories

Resources