I was working on a canvas path and require to implement more buttons and potentially images in the future. I know how to hard-code to avoid the objects that I do not require, however is there a way to do this easier? Potentially a single line of code...
So basically I have currently created a path to go from Button1 to Button2, without it passing through Button3 but rather going around it. The line I created I have hard-coded this to say go around Button3 but is there an easier way to do this, especially if there are more buttons in the future?
Current hard-coded solution image
DrawView:
public class DrawView extends View {
Paint paint = new Paint();
View startView;
View endView;
View obstacle;
Path path = new Path();
public DrawView(Context context, View startView, View endView, View obstacle) {
super(context);
paint.setColor(Color.parseColor("#4CAF50"));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(15);
this.startView = startView;
this.endView = endView;
this.obstacle = obstacle;
obstacle.setPadding(50, 50, 50, 50);
}
#SuppressLint("NewApi")
public void onDraw(Canvas canvas) {
path.moveTo(startView.getX() + startView.getWidth() / 2, startView.getY() + startView.getHeight() / 2);
path.lineTo(obstacle.getX() + obstacle.getWidth() / 2, obstacle.getY());
path.moveTo(obstacle.getX() + obstacle.getWidth() / 2, obstacle.getY());
path.lineTo(obstacle.getX() + obstacle.getWidth(), obstacle.getY());
path.moveTo(obstacle.getX() + obstacle.getWidth(), obstacle.getY());
path.lineTo(obstacle.getX() + obstacle.getWidth(), obstacle.getY() + obstacle.getHeight());
path.moveTo(obstacle.getX() + obstacle.getWidth(), obstacle.getY() + obstacle.getHeight());
path.lineTo(obstacle.getX() + obstacle.getWidth() / 2, obstacle.getY() + obstacle.getHeight());
path.moveTo(obstacle.getX() + obstacle.getWidth() / 2, obstacle.getY() + obstacle.getHeight());
path.lineTo(endView.getX() + endView.getWidth() / 2, endView.getY() + endView.getHeight() / 2);
canvas.drawPath(path, paint);
}
}
MainActivity:
public class MainActivity extends Activity {
DrawView drawView;
Button b1, b2, b3, clear;
RelativeLayout rl;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.Button1);
b2 = findViewById(R.id.Button2);
b3 = findViewById(R.id.Button3);
clear = findViewById(R.id.clear_button);
rl = findViewById(R.id.rl);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawView = new DrawView(MainActivity.this, b1, b2, b3);
rl.addView(drawView);
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rl.removeAllViewsInLayout();
}
});
}
}
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"
android:orientation="vertical"
tools:context="com.example.fyp.linecreator.MainActivity">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:layout_marginStart="100dp"
android:layout_marginTop="40dp"
android:text="Button2" />
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Button2"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_marginTop="4dp"
android:text="Button1"
android:layout_marginStart="40dp"
android:layout_alignStart="#+id/Button2" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Button2"
android:layout_alignLeft="#+id/Button2"
android:layout_marginBottom="23dp"
android:layout_marginLeft="35dp"
android:text="Button3"
android:layout_alignStart="#+id/Button2"
android:layout_marginStart="35dp" />
<Button
android:id="#+id/clear_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" />
</RelativeLayout>
Related
I am trying to make a simple app. The program should create a table with given row and column numbers. I'm adding my Java and xml files below:
public class MainActivity extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout, mainLayout;
private TextView txtResult;
private Random random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
LinearLayout mainLayout = findViewById(R.id.mainLayout);
TableLayout table = new TableLayout(this);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainActivity.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainActivity.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
mainLayout.addView(table);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txtRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp" />
<EditText
android:id="#+id/txtColumn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET" />
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
This is the error message that I got:
The specified child already has a parent. You must call removeView() on the child's parent first.
Therefore, whenever I tried to add mainLayout.removeView(table); to the code nothing happens when I click the button create. Thank you all. I will be appreciated for any kinds of comments.
You have the following errors:
1: The layout above the mainLayout layout is letting the height be match_parent. Make mainLayout not displayable.
2: The layout mainLayout is also setting the height to match_parent which is also wrong. I wonder do you understand what match_parent means?
3: The first time you add successfully, but because you are setting the height to match_parent, it makes it not visible, in fact it successfully added the table. The second click because it has been added to the table, it will crash.
The code below I have fixed for you, let's try it.
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/txtRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter row number"
android:inputType="textCapWords"
android:textSize="18sp"
android:text="3"/>
<EditText
android:id="#+id/txtColumn"
android:text="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter column number"
android:inputType="textCapWords"
android:textSize="18sp" />
<TextView
android:id="#+id/txtResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Result"
android:background="#E91E63"
android:textSize="18sp"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREATE"/>
<Button
android:id="#+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CALCULATE" />
<Button
android:id="#+id/btnReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET" />
<Button
android:id="#+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT" />
</LinearLayout>
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
JAVA:
public class MainAct extends AppCompatActivity {
private Button btnCreate, btnCalculate, btnReset, btnExit;
private EditText txtColumn, txtRow;
private LinearLayout linLayout, mainLayout;
private TextView txtResult;
private Random random;
TableLayout table;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCalculate = (Button) findViewById(R.id.btnCalculate);
btnReset = (Button) findViewById(R.id.btnReset);
btnExit = (Button) findViewById(R.id.btnExit);
txtColumn = (EditText) findViewById(R.id.txtColumn);
txtRow = (EditText) findViewById(R.id.txtRow);
txtResult = (TextView) findViewById(R.id.txtResult);
txtColumn.requestFocus();
random = new Random();
LinearLayout mainLayout = findViewById(R.id.mainLayout);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (table != null) {
mainLayout.removeView(table);
}
table = new TableLayout(getBaseContext());
table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
table.setShrinkAllColumns(true);
table.setStretchAllColumns(true);
int columnNumber = Integer.parseInt(txtColumn.getText().toString());
int rowNumber = Integer.parseInt(txtRow.getText().toString());
for (int i=0; i < rowNumber; i++) {
TableRow row = new TableRow(MainAct.this);
for (int j=0; j < columnNumber; j++) {
int value = random.nextInt(100) + 1;
TextView tv = new TextView(MainAct.this);
tv.setText(String.valueOf(value));
row.addView(tv);
}
table.addView(row);
}
if (table != null) {
mainLayout.addView(table);
}
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
System.exit(0);
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtColumn.setText(null);
txtRow.setText(null);
txtResult.setText(null);
}
});
}
}
in my Android App I have 3 Buttons which i set to 33% of the Screen-Width with the weight parameter. Now i want these buttons to have the same height as width, but without "hardcoding" a specific sice to the height, to adjust to different screensizes.
How can i handle this?
Thanks!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/buttonPlus1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_margin="5dp"
android:background="#drawable/buttonstyle"
android:text="+1" />
<Button
android:id="#+id/buttonPlus5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_margin="5dp"
android:background="#drawable/buttonstyle"
android:text="+5" />
<Button
android:id="#+id/buttonPlus10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.33"
android:layout_margin="5dp"
android:background="#drawable/buttonstyle"
android:text="+10" />
</LinearLayout>
I have come up with a solution and it's tested and working properly.
inside onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = findViewById(R.id.button1);
b2 = findViewById(R.id.button2);
b3 = findViewById(R.id.button3);
layout = findViewById(R.id.linearContainerOfButton);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int width = b1.getWidth();
ViewGroup.LayoutParams params = b1.getLayoutParams();
Log.d(tag, "width " + width);
params.height = width;
b1.setLayoutParams(params);
b2.setLayoutParams(params);
b3.setLayoutParams(params);
}
});
I've been working on a single screen android application, but I'm having trouble getting my TranslateAnimation method to function properly. I'm looking for the view group to slide from off app to where it's designed to finish.
Can you guys take a gander and let me know what I'm doing wrong?
Java Activity:
public class AboutProcess extends AppCompatActivity {
Button myButton;
View myView;
boolean isUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_process);
myView = findViewById(R.id.my_view);
myButton = findViewById(R.id.my_button);
//set as invisible
myView.setVisibility(View.INVISIBLE);
isUp = false;
}
public void slideUp(View view) {
view.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
view.getHeight(), // fromYDelta
0); // toYDelta
animate.setDuration(50);
animate.setFillAfter(true);
view.startAnimation(animate);
}
// slide the view from its current position to below itself
public void slideDown(View view) {
TranslateAnimation animate = new TranslateAnimation(
0, // fromXDelta
0, // toXDelta
0, // fromYDelta
view.getHeight()); // toYDelta
animate.setDuration(50);
animate.setFillAfter(true);
view.startAnimation(animate);
}
public void onSlideViewButtonClick(View view) {
if (isUp) {
slideDown(myView);
myButton.setText("SHOW LINKS");
} else {
slideUp(myView);
myButton.setText("HIDE LINKS");
}
isUp = !isUp;
}
}
And here is the XML for the button click and the viewgroup that I'm trying to have animate onto the screen. Within the LinearLayout are a set of four images.
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/lasvegas"
android:text="SHOW LINKS"
android:backgroundTint="#color/colorPrimaryDark"
android:textSize="#dimen/med_font"
android:textColor="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="parent"
app:layout_constraintRight_toLeftOf="parent"/>
<LinearLayout
android:id="#+id/my_view"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_marginBottom="#dimen/padding50"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<ImageButton
android:contentDescription="#string/desc"
android:id="#+id/fb"
android:onClick="buttonFB"
android:src="#drawable/white_fb"
android:background="#null"
android:scaleType="centerInside"
android:layout_height="match_parent"
android:layout_width="0dp"
android:padding="#dimen/padding15"
android:layout_weight="1" />
<ImageButton
android:contentDescription="#string/desc"
android:id="#+id/youtube"
android:onClick="buttonTube"
android:src="#drawable/white_tube"
android:background="#null"
android:scaleType="centerInside"
android:layout_height="match_parent"
android:layout_width="0dp"
android:padding="#dimen/padding15"
android:layout_weight="1" />
<ImageButton
android:contentDescription="#string/desc"
android:id="#+id/tweet"
android:onClick="buttonTweet"
android:src="#drawable/white_tweet"
android:background="#null"
android:scaleType="centerInside"
android:layout_height="match_parent"
android:layout_width="0dp"
android:padding="#dimen/padding15"
android:layout_weight="1" />
<ImageButton
android:contentDescription="#string/desc"
android:id="#+id/www"
android:onClick="buttonWWW"
android:src="#drawable/white_www"
android:background="#null"
android:scaleType="centerInside"
android:layout_height="match_parent"
android:layout_width="0dp"
android:padding="#dimen/padding15"
android:layout_weight="1" />
</LinearLayout>
When I implement this code on my device, the button shows and is 'clickable' but the view group is not on my screen, nor can I find it at any point. Any ideas of what I'm missing (or how to program the animation differently for this concept)?
I want to have a transition in my android views on view is Button and other is EditText, transitions must be like this animation
I tried in this way
Layout xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="app.itc.org.todo.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/title_tv"
android:text="#string/to_do"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/date_tv"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorGray"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:text="#string/what_do_you_want_to_do_today"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:text="#string/start_adding_items_to_your_to_do_list"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/transitions_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<Button
android:id="#+id/add_btn"
android:layout_width="200dp"
android:layout_height="60dp"
android:text="#string/add_item"
android:textSize="18sp"
android:paddingLeft="20dp"
android:textColor="#android:color/white"
android:drawableLeft="#drawable/ic_add_24dp"
android:background="#drawable/rounded_black_bg"
android:layout_gravity="center"/>
<EditText
android:id="#+id/item_input_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:minHeight="50dp"
android:layout_marginLeft="#dimen/margin_30dp"
android:layout_marginRight="#dimen/margin_30dp"
android:paddingLeft="#dimen/dimen_20dp"
android:paddingRight="#dimen/dimen_50dp"
android:textColor="#android:color/black"
android:inputType="text"
android:background="#drawable/rounded_edit_text"
android:layout_gravity="center"/>
</FrameLayout>
</RelativeLayout>
Preview is
Java code is
public class MainActivity extends AppCompatActivity {
private EditText mItemInputEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView dateTextView = (TextView) findViewById(R.id.date_tv);
mItemInputEditText = (EditText) findViewById(R.id.item_input_et);
final Button addButton = (Button) findViewById(R.id.add_btn);
final ViewGroup transitionsContainer = (ViewGroup) findViewById(R.id.transitions_container);
mItemInputEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionSet set = new TransitionSet()
.addTransition(new Fade())
.setInterpolator(new FastOutLinearInInterpolator());
TransitionManager.beginDelayedTransition(transitionsContainer, set);
}
addButton.setVisibility(View.VISIBLE);
mItemInputEditText.setVisibility(View.GONE);
}
});
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
TransitionSet set = new TransitionSet()
.addTransition(new Fade())
.setInterpolator(new FastOutLinearInInterpolator());
TransitionManager.beginDelayedTransition(transitionsContainer, set);
}
addButton.setVisibility(View.GONE);
mItemInputEditText.setVisibility(View.VISIBLE);
}
});
SimpleDateFormat dt = new SimpleDateFormat("EEE d MMM yyyy");
dateTextView.setText(dt.format(new Date()));
}
}
but with this code the resulting transition is
As you can see this is bit weird as compared to expected one, can any one suggest me some changes to get the desired transitions.
Transitions API is surely nice thing, but it cannot solve each and everything for you. You haven't instructed how to perform that animation to the transition framework, how come it would understand what is the final animation that you want to perform?
I'm not sure this animation can be achieved using solely Transitions API. Instead, you can stick to standard animations APIs, e.g. ValueAnimator.
The animation consists of several stages. When a button is clicked, you want it to become slightly wider, also losing its transparency. And after this is done, you want the EditText to come into the scene and be animated to its final value starting from the width, where the button was landed at.
So, inside button click listener:
#Override
public void onClick(View v) {
final int from = addButton.getWidth();
final int to = (int) (from * 1.2f); // increase by 20%
final LinearInterpolator interpolator = new LinearInterpolator();
ValueAnimator firstAnimator = ValueAnimator.ofInt(from, to);
firstAnimator.setTarget(addButton);
firstAnimator.setInterpolator(interpolator);
firstAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = addButton.getLayoutParams();
firstAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
addButton.setAlpha(1 - animation.getAnimatedFraction());
addButton.requestLayout();
}
});
firstAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
// reset alpha channel
addButton.setAlpha(1.0f);
addButton.setVisibility(View.GONE);
mItemInputEditText.setVisibility(View.VISIBLE);
ValueAnimator secondAnimator = ValueAnimator.ofInt(to, editTextWidth);
secondAnimator.setTarget(mItemInputEditText);
secondAnimator.setInterpolator(interpolator);
secondAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = mItemInputEditText.getLayoutParams();
secondAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
mItemInputEditText.requestLayout();
}
});
secondAnimator.start();
}
});
firstAnimator.start();
}
Similar actions are performed when coming back from EditText to button:
#Override
public void onClick(View view) {
final int from = mItemInputEditText.getWidth();
final int to = (int) (from * 0.8f);
final LinearInterpolator interpolator = new LinearInterpolator();
ValueAnimator firstAnimator = ValueAnimator.ofInt(from, to);
firstAnimator.setTarget(mItemInputEditText);
firstAnimator.setInterpolator(interpolator);
firstAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = mItemInputEditText.getLayoutParams();
firstAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
mItemInputEditText.requestLayout();
}
});
firstAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
mItemInputEditText.setVisibility(View.GONE);
addButton.setVisibility(View.VISIBLE);
ValueAnimator secondAnimator = ValueAnimator.ofInt(to, buttonWidth);
secondAnimator.setTarget(addButton);
secondAnimator.setInterpolator(interpolator);
secondAnimator.setDuration(DURATION);
final ViewGroup.LayoutParams params = addButton.getLayoutParams();
secondAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
params.width = (Integer) animation.getAnimatedValue();
addButton.setAlpha(animation.getAnimatedFraction());
addButton.requestLayout();
}
});
secondAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
addButton.setAlpha(0.0f);
}
});
secondAnimator.start();
}
});
firstAnimator.start();
}
editTextWidth and buttonWidth are initial sizes of views:
private int editTextWidth, buttonWidth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
// `transitions_container` is the parent of both `EditText` and `Button`
// Thus, posting on it ensures that both of those views are laid out when this runnable is executed
findViewById(R.id.transitions_container).post(new Runnable() {
#Override
public void run() {
editTextWidth = mItemInputEditText.getWidth();
// `mItemInputEditText` should be left visible from XML in order to get measured
// setting to GONE after we have got actual width
mItemInputEditText.setVisibility(View.GONE);
buttonWidth = addButton.getWidth();
}
});
}
Here's the output:
You can have the patch file with changes here.
First, change your Layout XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="app.itc.org.todo.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:background="#android:color/white"
android:gravity="center_horizontal|center_vertical">
<TextView
android:id="#+id/title_tv"
android:text="#string/to_do"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/date_tv"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorGray"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:text="#string/what_do_you_want_to_do_today"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:text="#string/start_adding_items_to_your_to_do_list"
android:textSize="16sp"
android:textColor="#android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/transitions_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<include layout="#layout/a_scene" />
</FrameLayout>
</RelativeLayout>
Then create your first scene for the button.
The layout for the first scene is defined as follows:
res/layout/a_scene.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scene_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/add_btn"
android:layout_width="200dp"
android:layout_height="60dp"
android:text="#string/add_item"
android:textSize="18sp"
android:paddingLeft="20dp"
android:textColor="#android:color/white"
android:drawableLeft="#drawable/ic_add_24dp"
android:background="#drawable/rounded_black_bg"
android:layout_gravity="center"/>
</RelativeLayout>
The layout for the second scene contains editText (with the same IDs) and is defined as follows:
res/layout/another_scene.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scene_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/add_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:minHeight="50dp"
android:layout_marginLeft="#dimen/margin_30dp"
android:layout_marginRight="#dimen/margin_30dp"
android:paddingLeft="#dimen/dimen_20dp"
android:paddingRight="#dimen/dimen_50dp"
android:textColor="#android:color/black"
android:inputType="text"
android:background="#drawable/rounded_edit_text"
android:layout_gravity="center"/>
</RelativeLayout>
Java Code:
public class MainActivity extends AppCompatActivity {
private EditText mItemInputEditText;
private Scene mAScene;
private Scene mAnotherScene;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView dateTextView = (TextView) findViewById(R.id.date_tv);
mItemInputEditText = (EditText) findViewById(R.id.item_input_et);
final Button addButton = (Button) findViewById(R.id.add_btn);
final ViewGroup transitionsContainer = (ViewGroup) findViewById(R.id.transitions_container);
// Create the scenes
mAScene = Scene.getSceneForLayout(mSceneRoot, R.layout.a_scene, this);
mAnotherScene = Scene.getSceneForLayout(mSceneRoot, R.layout.another_scene, this);
mItemInputEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Transition transition = new ChangeBounds();
TransitionManager.go(mAScene, transition);
}
addButton.setVisibility(View.VISIBLE);
mItemInputEditText.setVisibility(View.GONE);
}
});
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Transition transition = new ChangeBounds();
TransitionManager.go(mAnotherScenetransition);
}
addButton.setVisibility(View.GONE);
mItemInputEditText.setVisibility(View.VISIBLE);
}
});
SimpleDateFormat dt = new SimpleDateFormat("EEE d MMM yyyy");
dateTextView.setText(dt.format(new Date()));
}
}
You can use FABReveal Layout for that. Take a reference from that and change Relative layout to button. and modify it as per requirement.
XML
<com.truizlop.fabreveallayout.FABRevealLayout
android:id="#+id/fab_reveal_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/fab_reveal_height"
>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:backgroundTint="#color/some_color"
android:src="#drawable/some_drawable"
/>
<RelativeLayout
android:id="#+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</RelativeLayout>
<RelativeLayout
android:id="#+id/secondary_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</RelativeLayout>
</com.truizlop.fabreveallayout.FABRevealLayout>
https://github.com/truizlop/FABRevealLayout
https://github.com/saulmm/Curved-Fab-Reveal-Example
I'm trying to click on row in list view but my code only works when i pressed between two rows
you can see this :
http://www.screencast.com/t/2vei8yOQh
note in rep.xml i using table layout and inside it rowTable
i uses this code :
public class LastDaysActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last_days);
final Button newFourm = (Button) findViewById(R.id.button1);
LastDaysAdapter adapter = new LastDaysAdapter(this, getDates(),
getApplicationContext(), getBusID());
setListAdapter(adapter);
newFourm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(LastDaysActivity.this,
TabHostActivity.class);
startActivity(i);
}
});
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView myView = (TextView) v.findViewById(R.id.textView2);
String text = myView.getText().toString();
Toast.makeText(this, text + " selected", Toast.LENGTH_LONG).show();
}
edit xml :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/TableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow
android:id="#+id/TableRow05"
android:layout_width="fill_parent"
android:layout_height="27dp"
android:layout_marginTop="4dp"
android:background="#drawable/trans01" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="رقم اللوحة" />
<TextView
android:id="#+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="التاريخ"
android:textSize="12sp" />
</TableRow>
</TableLayout>