I have a little problem updating UI in OnClick Event.
When I do this:
case R.id.radStripMiningCoal :
{
LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout4 = (LinearLayout)findViewById(R.id.layout3);
LinearLayout layout = (LinearLayout)findViewById(R.id.recomended);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.basic);
int origWidth = getWindowManager().getDefaultDisplay().getWidth();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(origWidth,LayoutParams.WRAP_CONTENT);
layout2.setLayoutParams(params);
layout.setLayoutParams(params);
testLayout.setLayoutParams(params);
layout3.setLayoutParams(params);
layout4.setLayoutParams(params);
stripminingcoal.setVisibility(View.VISIBLE);
VSLists.setVisibility(View.GONE);
Next.setVisibility(View.GONE);
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.multispd1), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.img1), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.shocktubecluster1), 5000);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R.id.imgBasicIS);
imageAnim.setBackgroundDrawable(animation);
animation.start();
AnimationDrawable animation2 = new AnimationDrawable();
animation2.addFrame(getResources().getDrawable(R.drawable.unidelaysp), 5000);
animation2.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);
animation2.setOneShot(false);
ImageView imageAnim2 = (ImageView) findViewById(R.id.imgRecomendedIS);
imageAnim2.setBackgroundDrawable(animation2);
animation2.start();
AnimationDrawable animation3 = new AnimationDrawable();
animation3.addFrame(getResources().getDrawable(R.drawable.leadin2), 5000);
animation3.addFrame(getResources().getDrawable(R.drawable.tubestarter),5000);
animation3.setOneShot(false);
ImageView imageAnim3 = (ImageView) findViewById(R.id.imgRecomendedBSS);
imageAnim3.setBackgroundDrawable(animation3);
animation3.start();
txtBasicIS.setText("Shock Tube Multi SPD / Detonating Cord / Cluster");
txtRecomendedIS.setText("Shock Tube Uni-Delay SP / Trunkline");
txtRecomendedBSS.setText("Lead-In Line / Electric Shock Tube Starter");
Everything updates and displays fine.
But s soon as I do the same code in a different on "case" it does not update the images to the new images:
case R.id.radNarrowReefGold:
{
LinearLayout testLayout = (LinearLayout)findViewById(R.id.layouttest);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout4 = (LinearLayout)findViewById(R.id.layout3);
LinearLayout layout = (LinearLayout)findViewById(R.id.recomended);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.basic);
int origWidth = getWindowManager().getDefaultDisplay().getWidth();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(origWidth,LayoutParams.WRAP_CONTENT);
layout2.setLayoutParams(params);
layout.setLayoutParams(params);
testLayout.setLayoutParams(params);
layout3.setLayoutParams(params);
layout4.setLayoutParams(params);
stripminingcoal.setVisibility(View.VISIBLE);
VSLists.setVisibility(View.GONE);
Next.setVisibility(View.GONE);
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.udlp3), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.udlp6), 5000);
animation.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);
animation.setOneShot(false);
ImageView imageAnim = (ImageView) findViewById(R.id.imgBasicIS);
imageAnim.setBackgroundDrawable(animation);
animation.start();
AnimationDrawable animation2 = new AnimationDrawable();
animation2.addFrame(getResources().getDrawable(R.drawable.udlp3vivid), 5000);
animation2.addFrame(getResources().getDrawable(R.drawable.udlp6vivid), 5000);
animation2.addFrame(getResources().getDrawable(R.drawable.trunkline), 5000);
animation2.setOneShot(false);
ImageView imageAnim2 = (ImageView) findViewById(R.id.imgRecomendedIS);
imageAnim2.setBackgroundDrawable(animation2);
animation2.start();
AnimationDrawable animation3 = new AnimationDrawable();
animation3.addFrame(getResources().getDrawable(R.drawable.leadin2), 5000);
animation3.addFrame(getResources().getDrawable(R.drawable.tubestarter),5000);
animation3.setOneShot(false);
ImageView imageAnim3 = (ImageView) findViewById(R.id.imgRecomendedBSS);
imageAnim3.setBackgroundDrawable(animation3);
animation3.start();
txtBasicIS.setText("Uni-Delay LP (3) / Uni-Delay LP (6) / Trunkline");
txtRecomendedIS.setText("Shock Tube Uni-Delay SP / Trunkline");
txtRecomendedBSS.setText("Lead-In Line / Electric Shock Tube Starter");
}
What am I missing or doing wrong?
The textViews does not change either.
Thanks in advance.
Patrick
It is due to the missing "break;" statement in your switch case structure.
You can test this using the following small snippets i wrote on the fly. If you remove the break; statement, you could not swicth the text to MESSI, it shows only RONALDO. In other words, the logic is broken.
If the "break;" is there, you can switch between MESSI and RONALDO.
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
TextView myText;
private Button b1;
private Button b2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView)this.findViewById(R.id.textView1);
b1 = (Button)this.findViewById(R.id.buttonMessi);
b1.setOnClickListener(this);
b2 = (Button)this.findViewById(R.id.buttonRonaldo);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonMessi:
myText.setText("MESSI");
break;
case R.id.buttonRonaldo:
myText.setText("RONALDO");
break;
}
}
}
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonMessi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="40dp"
android:text="Messi" />
<Button
android:id="#+id/buttonRonaldo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="40dp"
android:text="Ronaldo" />
</LinearLayout>
</RelativeLayout>
Try to add img.SetImageResource(0); before the previous line.
It worked kudos to you
Related
I want to display underneath my AutoCompleteTextView what the user entered when they press the button beside it. So basically adding a textview in response to button clicks. I have "hello world" displaying where i want them to be right now, but am stuck on how to do this inside a button listener. Any help is appreciated thank you.
Here is an image example of what I am trying to accomplish exactly.
sample display
my onCreate so far:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = new TextView(this);
textView.setText("hello world");
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
XML for layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.tester.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:text="AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/autoCompleteTextView"
android:layout_weight="1" />
<Button
android:text="PRESS ME"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Try this
Button button1 = (Button) findViewById(R.id.button2);
AutocompleteTextView autoTv = (autocompleteTextView) findViewById(R.id.autocompleteTextView);
button1.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String enteredText = autoTv.getText().toString();
if (!enteredText.isEmpty()) {
TextView textView = new TextView(this);
textView.setText(enteredText);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_main);
//LayoutParams is a class to specify layout properties when adding a view to a layout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, layoutParams);
}
}
});
I have a linear layout with a certain amount of padding. I want an image button to be placed randomly anywhere within that linear layout without going outside the layout. Here is my xml code:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="45dp"
android:text="Text 1"
android:id="#+id/text1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30dp"
android:text="Text 2"
android:id="#+id/text2" />
<LinearLayout
android:id="#+id/lay1"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_margin="20dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:src="#drawable/ib"
android:id="#+id/button1"/>
</LinearLayout>
</LinearLayout>
I want the ImageButton button1 to be placed randomly within layout lay1.
I have tried getting the width and height of the layout and feeding them into a random function which I then use to provide a left margin and top margin to the imagebutton, but my app keeps crashing whenever I do that. Here's the Java code:
ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) b.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
b.setLayoutParams(params);
The above method did not work and my app always crashes when I open this activity. Can someone please help me out?
Your method looks good, but you also have to take into account the image size.
So I'd put something like :
params.leftMargin = new Random().nextInt(width - b.getWidth());
params.topMargin = new Random().nextInt(height - b.getHeight());
Now you're gonna have to be more specific about the crash you're experiencing.
EDIT : Also SnyersK is right, if you're going through that piece of code before the element got their dimensions, then you'll need to use a ViewTreeObserver.
I've got a partial solution for you.
The following code places a button at a random location BUT it can be put off screen. You should be able to prevent this by playing around with the random generator.
public class MainActivity extends ActionBarActivity {
int width, height;
LinearLayout linearLayout;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.ll);
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.v(getClass().getSimpleName(), "onGlobalLayout");
width = linearLayout.getWidth();
height = linearLayout.getHeight();
placeButton();
if (Build.VERSION.SDK_INT >= 16) {
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
linearLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
button = (Button) findViewById(R.id.button);
}
private void placeButton() {
Log.v(getClass().getSimpleName(), "width: " + width + " Height: " + height);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
Random rand = new Random();
params.leftMargin = rand.nextInt(width);
params.topMargin = rand.nextInt(height);
Log.v(getClass().getSimpleName(), "left: " + params.leftMargin + " top: " + params.topMargin);
button.setLayoutParams(params);
button.invalidate();
}
}
You just wait for your linearlayout to be drawn. Then you get it's width and height and put the button on a random place.
Note
This will move the button around when you rotate the screen. you could prevent this by only adding an OnGlobalLayoutListener if savedInstanceState == null
Just replace your code with this
ImageButton b = (ImageButton) findViewById(R.id.button1);
LinearLayout l = (LinearLayout) findViewById(R.id.lay1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)l.getLayoutParams();
int width = l.getWidth();
int height= l.getHeight();
params.leftMargin = new Random().nextInt(width);
params.topMargin = new Random().nextInt(height);
l.setLayoutParams(params);
I can't do addView() two times.
In fact, I want one TextView(left) and one Button(right) besides.
So how I can position them in LinearLayout?
if (success == 1) {
content = json.getJSONArray(TAG_COMMENTS);
layoutVertical.removeAllViews();
final int N = content.length();
final Button[] myButton = new Button[N];
final LinearLayout[] myLayout = new LinearLayout[N];
final TextView[] myTextView = new TextView[N];
for (int i = 0; i < content.length(); i++) {
JSONObject c = content.getJSONObject(i);
String name = c.getString(TAG_CONTENT);
final LinearLayout layoutHorizontal = new LinearLayout(
getActivity());
layoutHorizontal
.setOrientation(LinearLayout.HORIZONTAL);
layoutHorizontal.setId(i);
layoutHorizontal.setBackground(getResources()
.getDrawable(R.drawable.borderadmincomment));
final TextView rowTextView = (TextView) new TextView(
getActivity());
rowTextView.setText(name);
rowTextView.setId(i);
final Button rowButton = new Button(getActivity());
rowButton.setLayoutParams(new LayoutParams(1,
LayoutParams.MATCH_PARENT));
rowButton.setId(i);
rowButton.setBackgroundResource(R.drawable.croix);
rowButton
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
layoutHorizontal.addView(rowTextView, 0);
layoutHorizontal.addView(rowButton, 1);
rowTextView.setWidth(400);
//rowTextView.setHeight(150);
//rowButton.setHeight(150);
layoutVertical.addView(layoutHorizontal);
myButton[i] = rowButton;
myLayout[i] = layoutHorizontal;
myTextView[i] = rowTextView;
}
}
It's not works.
Thank you.
You forgot to set layout params for Horizontal layout and rowTextView.
For example:
layoutHorizontal.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
Create linear layout parameter. Do something like this:-
LinearLayout LL = new LinearLayout(this);
LL.setBackgroundColor(Color.CYAN);
LL.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams LLParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
LL.setWeightSum(2f);
LL.setLayoutParams(LLParams);
Please let me know it this will work.
Make a xml layout file and name it item.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="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical" >
<TextView
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="1" />
<Button
android:id="#+id/rowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Write following code in your Activity
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layoutHorizontal = inflater.inflate(R.layout.item, null);
TextView rowTextView = (TextView) layoutHorizontal.findViewById(R.id.rowTextView);
Button rowButton = (Button) layoutHorizontal.findViewById(R.id.rowButton);
layoutVertical.addView(layoutHorizontal);
I have been looking around and searching for an answer and really trying to dive into android programming before asking any questions. I know that this site is here to help, but also not an outlet to be redundant and lazy. Though I am a begginer with android and am in need of some assistance if some of you have the time. I looked at a question that I really thought would help (URL: Button animations in android) But it just ended up making my program crash. I also checked out this one http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-a-simple-property-animation/ , which also didn't really get me to the correct answer I was looking for. Now I have no code unfortunately but have tried to replicate the code from both of these example sites I have provided. They both crash when I get to the declaration of animation objects in the actual java code
i.e.
ImageView confettiStart = (ImageView) findViewById(R.id.confetti);
AnimatorSet confettiSet =(AnimatorSet)AnimatorInflater.loadAnimator(this,R.animator.startconfettianimations);
My goal is to basically just have a confetti animation come from the top of my screen and roll down to the bottom of my screen. I have created about 8 different images in order to create an "animation" but I really do not know how to implement this in Android itself. If someone could help or point me in the right direction I would greatly appreciate it. Thank you so much.
Use objectAnimator for that view and in that keep x parameter same while Change y parameter to 0 to 600 or something same . Hope it works. or translator animator will do the trick try it keep x1=0 x2=0 and y1=0 y2=600.
package com.shubh;
import android.os.Build;
import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
public void startAnimation(View view) {
float dest = 0;
ImageView aniView = (ImageView) findViewById(R.id.imageView1);
switch (view.getId()) {
case R.id.Button01:
dest = 360;
if (aniView.getRotation() == 360) {
System.out.println(aniView.getAlpha());
dest = 0;
}
ObjectAnimator animation1 = ObjectAnimator.ofFloat(aniView,
"rotation", dest);
animation1.setDuration(2000);
animation1.start();
// Show how to load an animation from XML
// Animation animation1 = AnimationUtils.loadAnimation(this,
// R.anim.myanimation);
// animation1.setAnimationListener(this);
// animatedView1.startAnimation(animation1);
break;
case R.id.Button02:
// Shows how to define a animation via code
// Also use an Interpolator (BounceInterpolator)
Paint paint = new Paint();
TextView aniTextView = (TextView) findViewById(R.id.textView1);
float measureTextCenter = paint.measureText(aniTextView.getText()
.toString());
dest = 0 - measureTextCenter;
if (aniTextView.getX() < 0) {
dest = 0;
}
ObjectAnimator animation2 = ObjectAnimator.ofFloat(aniTextView,
"x", dest);
animation2.setDuration(2000);
animation2.start();
break;
case R.id.Button03:
// Demonstrate fading and adding an AnimationListener
RelativeLayout mainContainer = (RelativeLayout) findViewById(R.id.layout);
LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(this, R.anim.main_layout_animation);
mainContainer.setLayoutAnimation(controller);
dest = 1;
Button button3=(Button)findViewById(R.id.Button03);
button3.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.hyperspace_jump));
if (aniView.getAlpha() > 0) {
dest = 0;
}
ObjectAnimator animation3 = ObjectAnimator.ofFloat(aniView,
"alpha", dest);
animation3.setDuration(2000);
animation3.start();
break;
case R.id.Button04:
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(aniView, "alpha",
0f);
fadeOut.setDuration(2000);
ObjectAnimator mover = ObjectAnimator.ofFloat(aniView,
"translationX", -500f, 0f);
mover.setDuration(2000);
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(aniView, "alpha",
0f, 1f);
fadeIn.setDuration(2000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(mover).with(fadeIn).after(fadeOut);
animatorSet.start();
break;
default:
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, HitActivity.class);
startActivity(intent);
return true;
}
}
and the xml is
<?xml version="1.0" encoding="utf-8" ?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
- <LinearLayout android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startAnimation"
android:text="Rotate" />
<Button android:id="#+id/Button04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startAnimation"
android:text="Group" />
<Button android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startAnimation"
android:text="Fade" />
<Button android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startAnimation"
android:text="Animate" />
</LinearLayout>
<ImageView android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img" />
<TextView android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView1"
android:layout_alignRight="#+id/imageView1"
android:layout_marginBottom="30dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Try this
public class HitActivity extends Activity {
private ObjectAnimator animation1;
private ObjectAnimator animation2;
private Button button;
private Random randon;
private int width;
private int height;
private AnimatorSet set;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doctor_chemist_list);
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();
randon = new Random();
set = createAnimation();
set.start();
set.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
int nextX = randon.nextInt(width);
int nextY = randon.nextInt(height);
animation1 = ObjectAnimator.ofFloat(button, "x", button.getX(),
nextX);
animation1.setDuration(1400);
animation2 = ObjectAnimator.ofFloat(button, "y", button.getY(),
nextY);
animation2.setDuration(1400);
set.playTogether(animation1, animation2);
set.start();
}
});
}
public void onClick(View view) {
String string = button.getText().toString();
int hitTarget = Integer.valueOf(string) + 1;
button.setText(String.valueOf(hitTarget));
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
private AnimatorSet createAnimation() {
int nextX = randon.nextInt(width);
int nextY = randon.nextInt(height);
button = (Button) findViewById(R.id.Button01);
animation1 = ObjectAnimator.ofFloat(button, "x", nextX);
animation1.setDuration(1400);
animation2 = ObjectAnimator.ofFloat(button, "y", nextY);
animation2.setDuration(1400);
AnimatorSet set = new AnimatorSet();
set.playTogether(animation1, animation2);
return set;
}
}
Good day.
I have three layouts: first is the root, second and third lie in first. I try add TextView object in third layout and objects had been added in third layout (I saw it in debage mode) but this objects didn't showed on screen.
May be someone know where is the problem?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/addJokeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
/>
<EditText
android:id="#+id/newJokeEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
protected void initLayout() {
setContentView(R.layout.advanced);
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.advanced, null);
m_vwJokeEditText = (EditText) findViewById(R.id.newJokeEditText);
m_vwJokeButton = (Button) findViewById(R.id.addJokeButton);
m_vwJokeLayout = (LinearLayout) linearLayout.getChildAt(1);
}
protected void addJoke(Joke joke) {
m_arrJokeList.add(joke);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView textView = new TextView(this);
setColor(textView);
textView.setLayoutParams(lparams);
textView.setText(joke.getJoke());
m_vwJokeLayout.addView(textView);
}
Refer this sample example
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class HelloAndroid extends Activity {
TextView textview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll= (LinearLayout) findViewById(R.id.LinearLayout01);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout childLayout= new LinearLayout(this);
childLayout.setOrientation(LinearLayout.VERTICAL);
TextView text = new TextView(this);
text.setText("High");
childLayout.addView(text);
ll.addView(childLayout, lp);
}
}`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello"/>
</LinearLayout>
or make use of following link
Creating Linear Layout with TextViews using a for loop
Try to access LayoutParams like this
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
and also you are declaring linearLayout as local variable on fallowing line use it as class variable. so that it is accessible out side method.
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.advanced, null);