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);
}
});
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);
}
});
}
}
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>
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 am trying to change my LinearLayout height to 50% of the parent layout when i click a button. I trired it with LinearLayout.LayoutParams but it doesn't change the height of my layout if i call it when buttonClicked from a method outside onCreate . But if i keep LinearLayout.LayoutParams inside onCreate then it works. How can i do it when i click a button?
This Code Works :
public class ViewReport extends AppCompatActivity {
Button button;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_report);
button=(Button) findViewById(R.id.button);
layout=(LinearLayout) findViewById(R.id.fold);
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
lp.height = 0;
lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
lp.weight=0.5f;
}
}
This Code Doesn't Work :
public class ViewReport extends AppCompatActivity {
Button button;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_report);
button=(Button) findViewById(R.id.button);
layout=(LinearLayout) findViewById(R.id.fold);
}
public void Click(View view)
{
Toast.makeText(getApplicationContext(),"tapped",Toast.LENGTH_LONG).show();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
lp.height = 0;
lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
lp.weight=0.5f;
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.imbishal.imageblur.ViewReport"
android:weightSum="1">
<LinearLayout
android:id="#+id/fold"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/profilepageback"
android:orientation="vertical">
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="3dp"
android:background="#drawable/my_button"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="5dp"
android:background="#drawable/my_button"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/just_border"
android:onClick="Click"
android:text="Button" />
</LinearLayout>
You have changed your LayoutParams but haven't set them. Add this line
layout.setLayoutParams(lp);
I'm a newbie.
I want to add 3 EditText's when a user click on a button.
I try to realize it, but it isn't work.
Widgets do not appear.
P.S. Widgets must be in others layout's.
Thank you for help.
Sorry for my bad English.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_fill"
android:id="#+id/simple_main_layout">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/for_main_text">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Buy List"
android:id="#+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/do_new_list"
android:layout_marginLeft="153dp"
android:layout_marginRight="2dp"
android:background="#drawable/btn_yes" android:textColor="#804f29" android:textStyle="bold"/>/>
</LinearLayout>
<EditText
android:layout_width="305dp"
android:layout_height="wrap_content"
android:id="#+id/editText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="91dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Code:
private Button do_new_field;
private EditText[] text, many, cost;
private final int fields = 50;
private int last_field = 0;
private LinearLayout layoutik;
LinearLayout.LayoutParams layoutParams;
LinearLayout.LayoutParams layoutParams2;
#Override
public void onBackPressed() {
super.onBackPressed();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_buy);
do_new_field = (Button) findViewById(R.id.do_new_list);
layoutik = (LinearLayout) findViewById(R.id.for_main_text);
do_new_field.setOnClickListener(this);
text = new EditText[fields];
many = new EditText[fields];
cost = new EditText[fields];
layoutParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,0f);
layoutParams2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,0f);
}
#Override
public void onClick(View view) {
if(view == do_new_field)
{
if(last_field <= fields)
{
text[last_field] = new EditText(this);
many[last_field] = new EditText(this);
cost[last_field] = new EditText(this);
text[last_field].setWidth(305);
text[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
text[last_field].setMaxLines(3);
many[last_field].setWidth(91);
many[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
many[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
many[last_field].setMaxLines(1);
cost[last_field].setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
cost[last_field].setMaxLines(1);
text[last_field].setId(100 + last_field);
many[last_field].setId(200 + last_field);
cost[last_field].setId(300 + last_field);
text[last_field].setLayoutParams(layoutParams);
many[last_field].setLayoutParams(layoutParams);
cost[last_field].setLayoutParams(layoutParams);
layoutik.addView(text[last_field]);
layoutik.addView(many[last_field]);
layoutik.addView(cost[last_field]);
last_field++;
}
}
}
}
You might have to add "setId" to the editText and check.
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0f);
editText.setId(EDT_ID + sub_count + i);
editText.setTag(insertAdAl.get(i).getmApiAppend());
editText.setLayoutParams(params);
editText.setTextAppearance(NewAdverts.this,
android.R.style.TextAppearance_Small);
editText.setGravity(Gravity.LEFT | Gravity.CENTER);
editText.setPadding(4, 4, 4, 4);
editText.setTextAppearance(NewAdverts.this,
android.R.attr.textAppearanceSmall);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(R.drawable.notification_field);