Android TextView with Marquee and layout_weight not scrolling - java

I have a dialog popping up with a title and a close button. This is the XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/text_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="24dp"
android:layout_marginRight="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:maxLines="1"
<!--- android:singleLine="true" <-- Do not comment about this, it is deprecated --->
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textSize="20sp"
android:textColor="#android:color/black"
/>
<ImageView
android:id="#+id/backburger"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:src="#drawable/ic_close"
android:foreground="?android:attr/selectableItemBackground"
/>
</LinearLayout>
Like you can see the TextView has all the things it needs for a marquee, the singleLine options is deprecated, so I can't use it. I do call setSelected(true) in my Java. There are lots of questions about a marquee, but none with layout_weight and from '17.
This is the preview of the XML:

Try this one:
In your Xml:
<TextView
android:id="#+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textSize="20sp"
android:textColor="#android:color/black" />
and your Code:
tv = (TextView) this.findViewById(R.id.text_title);
tv.setSelected(true);
Hope its help:)

public void setticker(String text, TextView view) {
if (text != "") {
view.setText(text);
//view.setTextSize(25.0F);
Context context = view.getContext(); // gets the context of the view
// measures the unconstrained size of the view
// before it is drawn in the layout
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
// takes the unconstrained width of the view
float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();
// gets the screen width
float screenWidth = ((Activity) context).getWindowManager()
.getDefaultDisplay().getWidth();
// view.setLayoutParams(new LinearLayout.LayoutParams((int) width,
// (int) height, 1f));
System.out.println("width and screenwidth are" + width + "/"
+ screenWidth + "///" + view.getMeasuredWidth());
// performs the calculation
float toXDelta = width - (screenWidth - 0);
// sets toXDelta to -300 if the text width is smaller that the
// screen size
if (toXDelta < 0) {
toXDelta = 0 - screenWidth;// -300;
} else {
toXDelta = 0 - screenWidth - toXDelta;// -300 - toXDelta;
}
// Animation parameters
Animation mAnimation = new TranslateAnimation(screenWidth,
toXDelta, 0, 0);
mAnimation.setDuration(15000);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setRepeatCount(Animation.INFINITE);
view.setAnimation(mAnimation);
//parent_layout.addView(view);
}
}

Related

I want to change the text Color in android studio

I had a problem I didn't know how to change colors in Java
I want to change the text color At the end of the code
in the Calculation result The app is an application that tries to calculate body mass(bmi)
I don't know what codes to use to change the color of writing
this is my java code and xml code can I get help plz
java code
package com.example.bmicalculator;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
//Main activity class start here
public class MainActivity extends Activity {
//Define layout
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the references to the widgets
final EditText e1 = (EditText) findViewById(R.id.et1);
final EditText e2 = (EditText) findViewById(R.id.et2);
final TextView tv4 = (TextView) findViewById(R.id.tv4);
findViewById(R.id.ib1).setOnClickListener(new View.OnClickListener() {
// Logic for validation, input can't be empty
#Override
public void onClick(View v) {
String str1 = e1.getText().toString();
String str2 = e2.getText().toString();
if(TextUtils.isEmpty(str1)){
e1.setError("Please enter your weight");
e1.requestFocus();
return;
}
if(TextUtils.isEmpty(str2)){
e2.setError("Please enter your height");
e2.requestFocus();
return;
}
//Get the user values from the widget reference
float weight = Float.parseFloat(str1);
float height = Float.parseFloat(str2)/1;
//Calculate BMI value
float bmiValue = calculateBMI(weight, height);
//Define the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue + "-" + bmiInterpretation));
}
});
}
//Calculate BMI
private float calculateBMI (float weight, float height)
{
return (float) (weight / (height * height));
}
// her I want change color
private String interpretBMI(float bmiValue)
{
if (bmiValue < 16) {
return "Severely underweight" ;
} else if (bmiValue < 18.5) {
return "Underweight" ;
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}
xml code
<!-- Linear layout start here -->
<LinearLayout 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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/pic"
tools:context=".MainActivity">
<!-- Text view for BMI Text -->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="187dp">
<TextView
android:id="#+id/tv1"
android:layout_width="305dp"
android:layout_height="93dp"
android:layout_gravity="center"
android:layout_marginStart="53dp"
android:layout_marginLeft="53dp"
android:layout_marginEnd="53dp"
android:layout_marginRight="53dp"
android:layout_marginBottom="30dp"
android:fontFamily="#font/sss"
android:paddingLeft="15dp"
android:paddingTop="40dp"
android:shadowColor="#android:color/black"
android:shadowDx="4"
android:shadowDy="4"
android:text="1_MAC_Community"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="25sp"
android:typeface="serif"
app:layout_constraintBottom_toTopOf="#+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="33dp"
android:layout_marginRight="33dp"
android:text="plz Fill YOUR information"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="25sp"
android:typeface="serif"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv1"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/tv9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="30dp"
android:text="WEIGHT (KG)"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold|italic"
android:typeface="serif" />
<!-- Edit text for entering weight with hint in kgs -->
<EditText
android:id="#+id/et1"
android:layout_width="314dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:fadingEdgeLength="10dp"
android:hint="IN KGs"
android:inputType="numberDecimal"
android:scrollbarTrackHorizontal="#android:color/background_dark"
android:textAlignment="center"
android:textColorHighlight="#000000"
android:textColorLink="#00FFFFFF"
android:textCursorDrawable="#android:color/background_dark">
<requestFocus />
</EditText>
<!-- Text view for HEIGHT(CM)text -->
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="30dp"
android:text="HEIGHT (Meter)"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold|italic"
android:typeface="serif" />
<!-- Edit text for entering height with hint in cm -->
<EditText
android:id="#+id/et2"
android:layout_width="281dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="IN Meter"
android:textAlignment="center"
android:inputType="numberDecimal"></EditText>
<!-- Button for calculating the formula, when pressed, with calculate written over it -->
<Button
android:id="#+id/ib1"
android:layout_width="158dp"
android:layout_height="51dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:fadingEdge="vertical"
android:longClickable="true"
android:nextFocusRight="#color/colorPrimaryDark"
android:text="Calculate MY BMI"
android:visibility="visible" />
<!-- Text view for showing result -->
<TextView
android:id="#+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="20dp"
android:text=""
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#android:color/holo_orange_dark"/>
</LinearLayout>
<!-- Linear layout ends here -->
first remove final keyword from your TextView Object
this code
final TextView tv4 = (TextView) findViewById(R.id.tv4);
Replace
TextView tv4 = (TextView) findViewById(R.id.tv4);
then you can set color
you can write hex code
tv4.setTextColor("#ffffff");
or yuu can write color name
tv4.setTextColor(Color.WHITE);
or you can call it from color.xml file
tv4.setTextColor(getResources().getColor(R.color.your_colour));
Quite simple
tv4.setTextColor(Color.RED);
Or if you have a speific colour resource
tv4.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.your_colour));
tv4.setTextColor(getResources().getColor(R.color.your_colour));
Update:
Decare your view elements globally outside of onCreate
public class MainActivity extends Activity {
private EditText e1;
private EditText e2;
private TextView tv4;
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
e1 = (EditText) findViewById(R.id.et1);
e2 = (EditText) findViewById(R.id.et2);
tv4 = (TextView) findViewById(R.id.tv4);
}
Its Quite easy
1)Find Reference to Text View of which you want to change color
2)Use Method
MytextView.setColor(Color.BLUE);
Or if you have defined your custom color in colors.xml file
MytextView.setColor(getResourses().getColor(R.color.Mycolor));

Inconsistent TableRow width - Android

I have a TableLayout which is dynamically populated. On getting to the page, it shows as expected but the moment I switch away from the app or a call comes in, the layout is distorted, resulting in inconsistent column width as shown in the images below.
Find below, the code used to generate the table
XML
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical|horizontal"
android:background="#color/colorPrimaryDark">
<TableLayout
android:id="#+id/predictionTableView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:stretchColumns="*"
android:background="#color/colorPrimaryDark">
<TableRow>
<TextView
android:text="RESULT"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:background="#000"
android:layout_marginRight="1dp"/>
<TextView
android:text="TIME"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="LEAGUE"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="TEAM"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="PREDICTION"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:layout_marginRight="1dp"
android:background="#000"/>
<TextView
android:text="SCORE"
android:padding="1dp"
android:textSize="12sp"
android:textColor="#color/white"
android:background="#000"/>
</TableRow>
</TableLayout>
</HorizontalScrollView>
Java
int i = 1;
for (Prediction prediction : predictionCategory.getPredictions()) {
//int backgroundColor = (i%2 == 0 ? fragmentActivity.getResources().getColor(R.color.colorPrimaryDark) : fragmentActivity.getResources().getColor(R.color.colorAccent));
Drawable backgroundDrawable = fragmentActivity.getResources().getDrawable(R.drawable.table_cell_even); //(i%2 == 0 ? fragmentActivity.getResources().getDrawable(R.drawable.table_cell_even) : fragmentActivity.getResources().getDrawable(R.drawable.table_cell_odd));
int textColor = fragmentActivity.getResources().getColor(R.color.white);
TableRow tableRow = new TableRow(fragmentActivity);
tableRow.setBackground(backgroundDrawable);
int colWidth = predictionTableView.getChildAt(0).getWidth();
TextView timeTextView = new TextView(fragmentActivity);
TextView leagueTextView = new TextView(fragmentActivity);
TextView teamTextView = new TextView(fragmentActivity);
TextView predictionTextView = new TextView(fragmentActivity);
TextView scoreTextView = new TextView(fragmentActivity);
TextView resultTextView = new TextView(fragmentActivity);
timeTextView.setTypeface(Typeface.DEFAULT);
timeTextView.setText(prediction.getTime());
timeTextView.setTextSize(13);
timeTextView.setPadding(3, 3, 3, 3);
timeTextView.setBackground(backgroundDrawable);
timeTextView.setTextColor(textColor);
leagueTextView.setTypeface(Typeface.DEFAULT);
leagueTextView.setText(prediction.getLeagueName());
leagueTextView.setTextSize(13);
leagueTextView.setPadding(3, 3, 3, 3);
leagueTextView.setBackground(backgroundDrawable);
leagueTextView.setTextColor(textColor);
teamTextView.setTypeface(Typeface.DEFAULT);
teamTextView.setText(prediction.getLocalTeamName() + " - " + prediction.getVisitorTeamName());
teamTextView.setTextSize(13);
teamTextView.setPadding(3, 3, 3, 3);
teamTextView.setBackground(backgroundDrawable);
teamTextView.setTextColor(textColor);
predictionTextView.setTypeface(Typeface.DEFAULT);
predictionTextView.setText(prediction.getPrediction());
predictionTextView.setTextSize(13);
predictionTextView.setPadding(3, 3, 3, 3);
predictionTextView.setBackground(backgroundDrawable);
predictionTextView.setTextColor(textColor);
scoreTextView.setTypeface(Typeface.DEFAULT);
scoreTextView.setText(prediction.getScores());
scoreTextView.setTextSize(13);
scoreTextView.setPadding(3, 3, 3, 3);
scoreTextView.setBackground(backgroundDrawable);
scoreTextView.setTextColor(textColor);
resultTextView.setTypeface(Typeface.DEFAULT);
resultTextView.setText(prediction.getResult());
resultTextView.setTextSize(13);
resultTextView.setPadding(3, 3, 3, 3);
resultTextView.setBackground(backgroundDrawable);
resultTextView.setTextColor(textColor);
if (!prediction.getResult().matches("")) {
if (prediction.getResult().toUpperCase().matches("WIN")) {
resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorSuccess));
} else {
resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorDanger));
}
} /*else {
resultTextView.setText("LOST");
resultTextView.setBackgroundColor(fragmentActivity.getResources().getColor(R.color.colorDanger));
}*/
tableRow.addView(resultTextView);
tableRow.addView(timeTextView);
tableRow.addView(leagueTextView);
tableRow.addView(teamTextView);
tableRow.addView(predictionTextView);
tableRow.addView(scoreTextView);
TableLayout.LayoutParams trParamsSep = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(trParamsSep);
predictionTableView.addView(tableRow);
i++;
}
What is it that I'm doing wrong? How do I maintain equal column width irrespective of whether the user switch away and come back to the app or stayed on it for long.
Reloading data onResume() works well for me. Thanks #SumitShukla

ImageView- resize depending on android screen

I have a problem and I can't solve it, I will appreciate it if somebody would help me. I want to put two images on the top of the screen. Both images should have width = screen_width/2 (just because when the screen is smaller, images should resize).I've tried this, but it isn't enough. Thank you!
<RelativeLayout
android:id="#+id/imagelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/r"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:background="#drawable/back" />
<TextView
android:id="#+id/resulttext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:text="Hello"
android:layout_alignLeft="#id/r"
android:layout_alignTop="#id/r"
android:layout_alignRight="#id/r"
android:layout_alignBottom="#id/r"/>
</RelativeLayout>
For reference : http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=0CC0QtwIwAg&url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DmlojNcvoW68&ei=RoX4U8zwLdbvaKmOgMgH&usg=AFQjCNE5rRlHfVw-n_jgZAXEhK-Q1dd_Mg&sig2=mt8454k4FzqoVBprIqorNg&bvm=bv.73612305,d.d2s
<RelativeLayout //Change this to a linear layout
android:id="#+id/imagelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2" > //add this to linear layout attribute
<ImageView
android:id="#+id/r"
android:layout_width="0dp" //change width to 0dp (in case of horzontal layout)
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:background="#drawable/back"
android:layour_weight = "1"/> //add this to both view
<TextView
android:id="#+id/resulttext"
android:layout_width="0dp" //change width to 0dp (in case of horzontal layout)
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:text="Hello"
android:layout_alignLeft="#id/r"
android:layout_alignTop="#id/r"
android:layout_alignRight="#id/r"
android:layout_alignBottom="#id/r"
android:layour_weight = "1"/> //add this to both view/>
</RelativeLayout> //This again has to be linear layout
Use LinearLayout instead/inside RelativeLayout and set the weight property to 0.5 in each child view. you should also use layout:gravity to set the child view on the top.
Try this:
private int columnWidth;
//get Screen Size
public int getScreenWidth() {
int columnWidth ;
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
columnWidth = point.x;
return columnWidth;
}
//set height and width of Imageview
int height=columnWidth;
int width=columnWidth;
img.setLayoutParams(new RelativeLayout.LayoutParams(height,width));
Hope this may help you

How do I draw a line between two ImageButtons in android?

I am trying to create a line between 2 ImageButtons on screen. I have the buttons placed on the screen in an xml file and I am trying to code a way to draw a set of lines that will correctly scale when I use a different screen size. When I try to use getX() it states that the returned values are all 0. Is there some way I'm missing to get the x and y values of where I placed the buttons in the xml? Do I have to put the line in manually for each different screen size?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
super.onCreate(savedInstanceState);
size = 0;
View v = (RelativeLayout)inflater.inflate(R.layout.minor_fragment_two, container, false);
final ImageButton one = (ImageButton)v.findViewById(R.id.one);
final ImageButton two = (ImageButton)v.findViewById(R.id.two);
final ImageButton three = (ImageButton)v.findViewById(R.id.three);
final ImageButton four = (ImageButton)v.findViewById(R.id.four);
final ImageButton five = (ImageButton)v.findViewById(R.id.five);
final ImageButton six = (ImageButton)v.findViewById(R.id.six);
final ImageButton seven = (ImageButton)v.findViewById(R.id.seven);
final ImageButton eight = (ImageButton)v.findViewById(R.id.eight);
imageView = (ImageView) v.findViewById(R.id.imView);
//Display currentDisplay = getWindowManager().getDefaultDisplay();
Display currentDisplay = ((WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
onex = one.getX();
oney = one.getY();
twox = two.getX();
twoy = two.getY();
Toast.makeText(getActivity(),
"Position :" + onex + " " + oney + " " + twox + " " + twoy, Toast.LENGTH_LONG)
.show();
canvas.drawLine(onex,oney,twox,twoy,paint);
imageView.setImageBitmap(bitmap);
//return (LinearLayout)inflater.inflate(R.layout.minor_fragment_two, container, false);
return v;
}
<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/reduced"
android:weightSum="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imView"/>
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/one"
android:background="#drawable/first_alpha"
android:layout_marginTop="20dp"
android:layout_marginLeft="170dp"
android:layout_gravity="center_horizontal" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/two"
android:background="#drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:layout_marginLeft="120dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/three"
android:background="#drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="210dp"
android:layout_marginLeft="85dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/four"
android:background="#drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="330dp"
android:layout_marginLeft="120dp" />
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/five"
android:background="#drawable/first_alpha"
android:layout_gravity="right"
android:layout_marginTop="360dp"
android:layout_marginLeft="145dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/six"
android:background="#drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="380dp"
android:layout_marginLeft="50dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/seven"
android:background="#drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="410dp"
android:layout_marginLeft="190dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="#+id/eight"
android:background="#drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="460dp"
android:layout_marginLeft="135dp" />
</RelativeLayout>
You can create a line by adding a view to your xml.
Vertical line:
<View
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#FF0000FF"
/>
Horizontal line:
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#FF0000FF"
/>
Adjust the background color to your desired line color.

Android TranslateAnimation to Center

I need to apply a translation animation to an ImageView, this ImageView is positioned at certain place and i need to animate it to the center of the screen. Here is my translate xml code, but it doesn't animate the ImageView to the center.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="50%p"
android:toYDelta="50%p" />
</set>
This is the XML where the ImageView is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/WelcomeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/welcome_background"
android:orientation="vertical" >
<ImageView
android:id="#+id/welcome_innovaLogo"
android:layout_width="300dp"
android:layout_height="150dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="1dp"
android:layout_marginTop="5dp"
android:contentDescription="#string/welcome_innovaLogoString"
android:scaleType="fitXY"
android:src="#drawable/innova_logo" >
</ImageView>
<TextView
android:id="#+id/welcome_innovaInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/welcome_innovaLogo"
android:layout_marginTop="20dp"
android:layout_toRightOf="#+id/welcome_innovaLogo"
android:text="#string/welcome_innovaInfoString"
android:textColor="#9acd32"
android:textStyle="bold" />
<TextView
android:id="#+id/welcome_innovaHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/welcome_innovaInfo"
android:layout_marginRight="20dp"
android:text="#string/welcome_innovaHeaderString"
android:textColor="#9acd32"
android:textSize="20sp"
android:textStyle="italic" />
<ListView
android:id="#+id/welcome_commandsListView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/welcome_innovaLogo"
android:layout_marginTop="40dp"
android:cacheColorHint="#00000000" />
<LinearLayout
android:id="#+id/welcome_loadingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="450dp"
android:orientation="horizontal"
android:visibility="invisible" >
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:indeterminateDrawable="#drawable/progress_large" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:text="Loading Data..."
android:textColor="#color/green_color"
android:textSize="19sp" >
</TextView>
</LinearLayout>
<!--
<fragment
android:id="#+id/welcome_facebookFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/welcome_innovaLogo"
android:layout_marginLeft="20dp"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_toRightOf="#+id/welcome_commandsListView"
class="innovaEgypt.com.Welcome.FacebookFragment" >
</fragment>
-->
Have a look at this question. I gave a similar answer but the animation is not from an xml but from code. Hope it helps.
EDIT:
Your problem is that the animation doesn't take into account the image dimensions. The animation is centering the origin coordinate of your ImageView.
I still suggest my solution.
I tried the code in the answer that I linked and it works. Just copy this function:
private void moveViewToScreenCenter( View view )
{
RelativeLayout root = (RelativeLayout) findViewById( R.id.WelcomeLayout );
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics( dm );
int statusBarOffset = dm.heightPixels - root.getMeasuredHeight();
int originalPos[] = new int[2];
view.getLocationOnScreen( originalPos );
int xDest = dm.widthPixels/2;
xDest -= (view.getMeasuredWidth()/2);
int yDest = dm.heightPixels/2 - (view.getMeasuredHeight()/2) - statusBarOffset;
TranslateAnimation anim = new TranslateAnimation( 0, xDest - originalPos[0] , 0, yDest - originalPos[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
and call it passing your view as the parameter:
ImageView img1 = (ImageView) findViewById( R.id.welcome_innovaLogo );
moveViewToScreenCenter(img1);
If your image still disappear, maybe is because it is behind your other widgets, like the ListView. If that is the case, reestructure your xml layout definig the ImageView at the end, just after the Listview (just cut and paste the ImageView).
For future updates...
Apply view property animation to move position of widget. Image view in this case
int layoutWidth = parentLayout.getWidth();
layoutWidth = ((int) layoutWidth / 2) - (imageView.getWidth()) ;
Log.d(TAG, " position - " + layoutWidth);
imageView.animate()
.translationX(-layoutWidth)
.translationY(0)
.setDuration(500)
.setInterpolator(new LinearInterpolator())
.start();
Try adding delay to this code.
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
moveViewToScreenCenter( view );
}
},
1000);

Categories

Resources