I build my own custom Dialogbut its not opening when i am clicking on ImageView. I also checked by putting break point Dialog is coming null how to rectify it. I want to shift TextView of id unread_count to the right of parent . How can I do this ?
If I set android:layout_alignParentRight="true" then I have the following picture :
<?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"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:minHeight="120dp"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:tint="#android:color/white"
app:srcCompat="#drawable/info" />
<TextView
android:id="#+id/Dialogtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Latest # LootBox"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/Dialogcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="All These Deals, Offers etc Are For Limited Period of Time And Can be Over at Any Time Without Any Prior Notice."
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
android:textColor="#666666" />
</LinearLayout>
<LinearLayout
android:id="#+id/lyt_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/bt_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_rounded_green"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Get Started"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
Java code
private ImageView dialogBox;
Dialog customDialog;
customDialog = new Dialog(getActivity());
dialogBox = (ImageView) view.findViewById(R.id.dialogBox);
dialogBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog();
}
});
private void showDialog() {
customDialog.setContentView(R.layout.dialog_info);
}
You can try this way, you have to find dialog views using dialog.findViewById
Dialog dialog = new Dialog(mActivity);
dialog.setContentView(R.layout.dialog_info);
//dialog.setCancelable(false); //set Cancelable
ImageView dialogBox = (ImageView) dialog.findViewById(R.id.imgViewID);
dialogBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
//for big size dialog
/* Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);*/
I check you dialog_info.xml code
You need to improve your design code, you don't need to take static height and weight
I update your layout code here
<?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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<ImageView
android:id="#+id/icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:tint="#android:color/white"
app:srcCompat="#drawable/ic_close" />
<TextView
android:id="#+id/Dialogtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Latest # LootBox"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|start"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="#+id/Dialogcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:text="All These Deals, Offers etc Are For Limited Period of Time And Can be Over at Any Time Without Any Prior Notice."
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
android:textColor="#666666" />
</LinearLayout>
<LinearLayout
android:id="#+id/lyt_action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="#+id/bt_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Get Started"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
You forgot to call show method on the custom dialog. Change your code to
private void showDialog() {
customDialog.setContentView(R.layout.dialog_info);
// Add this line
customDialog.show();
}
Related
[![enter image description here][1]][1]
Hi, How can achieve the above image. when user clicks on the profile icon then the pop up window will display. But here the problem i am getting is pop up window is covering the whole height from anchor view. I need to show that home and barcode images bottom navigation view.
My XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/menu">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu"
android:textSize="30sp"
android:layout_marginBottom="60dp"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:fontFamily="#font/robotobold"
android:layout_marginTop="20dp"
android:textColor="#color/colorBlack">
</TextView>
<TextView
android:id="#+id/agent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agent Agreement"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_gravity="start"
android:gravity="start"
android:fontFamily="#font/robotobold"
android:textColor="#color/colorBlack">
</TextView>
<TextView
android:id="#+id/privacypolicy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Privacy Policy"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_gravity="start"
android:gravity="start"
android:fontFamily="#font/robotobold"
android:textColor="#color/colorBlack">
</TextView>
<TextView
android:id="#+id/changesettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How to change your settings"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_gravity="start"
android:gravity="start"
android:fontFamily="#font/robotobold"
android:textColor="#color/colorBlack">
</TextView>
<TextView
android:id="#+id/bankinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Bank Account Info"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_gravity="start"
android:gravity="start"
android:fontFamily="#font/robotobold"
android:textColor="#color/colorBlack">
</TextView>
<TextView
android:id="#+id/profilepic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile Picture"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_gravity="start"
android:gravity="start"
android:fontFamily="#font/robotobold"
android:textColor="#color/colorBlack">
</TextView>
<TextView
android:id="#+id/logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
android:layout_margin="10dp"
android:textSize="14sp"
android:layout_gravity="start"
android:gravity="start"
android:fontFamily="#font/robotobold"
android:textColor="#color/colorBlack">
</TextView>
</LinearLayout>
</RelativeLayout>
and Java code for displaying pop window
draweeView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showPopup(view) ;
}
});
private void showPopup(View view) {
View popupView = getLayoutInflater().inflate(R.layout.menupopup, null);
popupWindow.setFocusable(true);
PopupWindow popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv = (TextView) popupView.findViewById(R.id.lesshigh);
popupWindow.showAsDropDown(view,-25, 10);
}
The result I am getting is:
Change the values of android:layout_height in your LinearLayout. For me the best what can you do will be look like this :
...
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
...
I'm using the default ProgressBar widget, for indeterminate progress, in a Linear Layout with Style: #android:style/Widget.DeviceDefault.Light.ProgressBar.Small
If I start the activity with the ProgressBar VISIBLE and never make it INVISIBLE or GONE it shows ok. But if I put progressBar.setVisibility(progressBar.GONE); or progressBar.setVisibility(progressBar.INVISIBLE); anywhere in my code, the space for the ProgressBar is on the UI, but I cant see it.
I thought it might be a leaky parallel thread and removed all multi-threading from my app, but it still won't appear if INVISIBLE or GONE is anywhere in my code.
It works fine on other Activities.
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="260dp">
<ScrollView
android:id="#+id/myTeamsScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal">
<LinearLayout
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="16dp"
android:orientation="vertical"
android:paddingTop="32dp">
<ImageView
android:id="#+id/menu1ImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:contentDescription="#string/userNavigationMenuButtonContent"
android:onClick="openDrawer"
app:srcCompat="#drawable/menu_icon" />
</LinearLayout>
</LinearLayout>
<EditText
android:id="#+id/enterNewTeamEditText"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/enterTeamNameEditTextcontent"
android:ems="10"
android:hint="#string/teamNameHint"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimary" />
<ProgressBar
android:id="#+id/myTeamsProgressBar"
style="#android:style/Widget.DeviceDefault.Light.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:alpha="1"
android:foregroundTint="#FFFFCC"
android:progressTint="#FFFFCC"
android:visibility="visible" />
<TextView
android:id="#+id/deckTypeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:contentDescription="#string/remotePokerlabelcontent"
android:enabled="false"
android:text="#string/newTeamDeckType"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/remotePokerTextView" />
<Spinner
android:id="#+id/newTeamDeckTypeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:background="#drawable/spinner"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterNewTeamEditText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/createTeamButton"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="#color/colorButton"
android:contentDescription="#string/createTeamButtoncontent"
android:fontFamily="#string/fontfamily"
android:onClick="createTeam"
android:text="Create Team"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingTop="16dp">
<Button
android:id="#+id/receivedJoinRequestsButton"
android:layout_width="147dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/colorButton"
android:contentDescription="#string/createTeamButtoncontent"
android:fontFamily="#string/fontfamily"
android:onClick="showJoinRequests"
android:text="#string/myTeamsJoinRequestsButton"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
android:layout_weight="1" />
<Switch
android:id="#+id/teamTypesSwitch"
android:layout_width="143dp"
android:layout_height="wrap_content"
android:checked="false"
android:fontFamily="#string/fontfamily"
android:text="#string/switchMemberTeams"
android:textColor="#color/colorPrimary"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:orientation="vertical">
<ListView
android:id="#+id/myTeamsListView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="32dp"
android:layout_weight="1"
android:alwaysDrawnWithCache="true"
android:background="#drawable/spinner"
android:contentDescription="#string/myTeamsListViewcontent"
android:isScrollContainer="true"
android:scrollbars="vertical"
android:scrollingCache="true"
android:smoothScrollbar="true"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:onClick="onSwitchChange"
app:headerLayout= "#layout/nav_header_main"
app:menu="#xml/drawer_view" />
</androidx.drawerlayout.widget.DrawerLayout>
In the Activity onCreate I call populateMemberTeams(); I added a CountDownTimer to check if the progressBar is on the screen and it is...Before the 2 seconds, there is a empty space, but I can't see the ProgressBar
When the timer expires the UI adjusts when the ProgressBar changes to GONE.
private void populateMemberTeams() {
ListView myTeamsListView = findViewById(R.id.myTeamsListView);
ProgressBar progressBar = findViewById(R.id.myTeamsProgressBar);
progressBar.setVisibility(progressBar.VISIBLE);
mAuth = FirebaseAuth.getInstance();
String userId = mAuth.getCurrentUser().getUid();
callTeamManagement.populateMemberTeams(TAG, passedActivity, context, userId);
new CountDownTimer(2000, 1000) {
public void onFinish() {
progressBar.setVisibility(progressBar.GONE);
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
}
I have tried:
Changing the ProgressBar style in case it was a colour issue;
INVISIBLE instead of GONE;
Putting the ProgressBar inside its own LinearLayout
progressBar.setVisibility(progressBar.VISIBLE);
progressBar.setVisibility(View.VISIBLE);
progressBar.setVisibility(ProgressBar.VISIBLE);
You should use runOnUiThread.
Helper method for running part of a method on the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
}
});
I'm trying to make a visualization of some data in Android using a vertical bar chart, but when I increase or decrease the height of a View programatically by clicking a button, it changes from top to bottom, as in the image below (default value is 300).
How do I change the height from bottom to top, ie keeping the base of the view fixed?
Expected result:
My code in Java:
public final class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LinearLayout layoutXAxis = (LinearLayout)this.findViewById(R.id.layoutX);
LinearLayout layoutYAxis = (LinearLayout)this.findViewById(R.id.layoutY);
LinearLayout layoutZAxis = (LinearLayout)this.findViewById(R.id.layoutZ);
TextView txtXAxis = (TextView)layoutXAxis.findViewById(R.id.txtXAxis);
TextView txtYAxis = (TextView)layoutYAxis.findViewById(R.id.txtYAxis);
TextView txtZAxis = (TextView)layoutZAxis.findViewById(R.id.txtZAxis);
View graphXAxis = (View)layoutXAxis.findViewById(R.id.XAxis);
View graphYAxis = (View)layoutYAxis.findViewById(R.id.YAxis);
View graphZAxis = (View)layoutZAxis.findViewById(R.id.ZAxis);
LinearLayout layoutBtnAdd = (LinearLayout)this.findViewById(R.id.layAdd);
Button btnAddX = (Button)layoutBtnAdd.findViewById(R.id.btnXAdd);
Button btnAddY = (Button)layoutBtnAdd.findViewById(R.id.btnYAdd);
Button btnAddZ = (Button)layoutBtnAdd.findViewById(R.id.btnZAdd);
LinearLayout layoutBtnSub = (LinearLayout)this.findViewById(R.id.laySub);
Button btnSubX = (Button)layoutBtnSub.findViewById(R.id.btnXSub);
Button btnSubY = (Button)layoutBtnSub.findViewById(R.id.btnYSub);
Button btnSubZ = (Button)layoutBtnSub.findViewById(R.id.btnZSub);
txtXAxis.setText(String.valueOf(graphXAxis.getLayoutParams().height));
txtYAxis.setText(String.valueOf(graphYAxis.getLayoutParams().height));
txtZAxis.setText(String.valueOf(graphZAxis.getLayoutParams().height));
btnAddX.setOnClickListener((OnClickListener)(new OnClickListener() {
public final void onClick(View it) {
View graph = graphXAxis;
LayoutParams params = graph.getLayoutParams();
params.height += 10;
graph = graphXAxis;
graph.setLayoutParams(params);
TextView text = txtXAxis;
View graph = graphXAxis;
text.setText(String.valueOf(graph.getLayoutParams().height));
}
}));
... // onClickListener of the other buttons
}
My code in XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/layoutY">
<TextView
android:id="#+id/txtYAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Y AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_blue_light"/>
...
<View android:id="#+id/YAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_blue_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutX"
android:layout_toLeftOf="#id/layoutY"
android:layout_toStartOf="#id/layoutY"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="#+id/txtXAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="X AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"/>
...
<View android:id="#+id/XAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutZ"
android:layout_toRightOf="#id/layoutY"
android:layout_toEndOf="#id/layoutY"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp">
<TextView
android:id="#+id/txtZAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Z AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_green_light"/>
...
<View android:id="#+id/ZAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
...
</RelativeLayout>
You might consider constructing your layout like the following.
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button_layout"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtYAxis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Y AXIS"
android:textColor="#android:color/holo_blue_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/YAxis"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_blue_dark"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutX"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtXAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_red_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/XAxis"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_red_dark" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutZ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtZAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Z AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_green_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/ZAxis"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is the output.
Hope that helps!
This question already has answers here:
Dynamically changing the linearlayout width or height on Android
(4 answers)
Closed 4 years ago.
In my application I want set dynamically weight to LinearLayout and for this I write below codes, but when running application show me Force close error!
Force close message in logCat :
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
My XML codes :
<?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"
android:background="#color/white"
tools:context=".activity.UpdateActivity">
<!--Header-->
<RelativeLayout
android:id="#+id/dialogEndCount_headerLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size120"
android:background="#color/tabHomeColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="#dimen/size40"
android:text="#string/updateDialogTitle"
android:textColor="#color/white"
android:textSize="#dimen/font16" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/dialogUpdate_animate"
android:layout_width="#dimen/size150"
android:layout_height="#dimen/size150"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/size20"
app:lottie_autoPlay="true"
app:lottie_colorFilter="#color/white"
app:lottie_fileName="update.json"
app:lottie_loop="true" />
</RelativeLayout>
<!--Content-->
<RelativeLayout
android:id="#+id/dialogEndCount_contentLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogEndCount_buttonsLay"
android:layout_below="#+id/dialogEndCount_headerLay"
android:layout_marginBottom="#dimen/size10"
android:layout_marginTop="#dimen/size10"
android:background="#drawable/bg_dialog_vip_white">
<!--Type-->
<TextView
android:id="#+id/dialogUpdate_infoTypeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/size15"
android:layout_marginRight="#dimen/size15"
android:layout_marginTop="#dimen/size10"
android:gravity="center_vertical"
android:text="نوع بروز رسانی : اجباری"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<!--Content-->
<ScrollView
android:id="#+id/dialogUpdate_infoContentScrollLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogUpdate_downloadProgress"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_below="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size20">
<RelativeLayout
android:id="#+id/dialogUpdate_infoContentLay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/dialogUpdate_infoContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="#string/updateContent"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<TextView
android:id="#+id/dialogUpdate_infoContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/dialogUpdate_infoContentTitle"
android:layout_marginTop="#dimen/size5"
android:gravity="center_vertical"
android:textColor="#color/favColorON"
android:textSize="#dimen/font14" />
</RelativeLayout>
</ScrollView>
<!--Download layout-->
<com.tellfa.colony.view.DownloadProgressView
android:id="#+id/dialogUpdate_downloadProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size10" />
</RelativeLayout>
<!--Buttons-->
<LinearLayout
android:id="#+id/dialogEndCount_buttonsLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size60"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="3">
<!--Cancel-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_cancelBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_cancelBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/ic_close"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_cancelBtnImg"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:text="#string/updateCancel"
android:textColor="#color/white"
android:textSize="#dimen/font14" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--Direct-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_directBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/green"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_directBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/appro_ic_download_circle"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_directBtnImg"
android:ellipsize="end"
android:gravity="center"
android:layout_marginRight="#dimen/size1"
android:singleLine="true"
android:text="#string/updateOK"
android:textColor="#color/white"
android:textSize="#dimen/font12" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
My JAVA codes:
public class UpdateActivity extends BaseActivity {
#BindView(R.id.dialogEndCount_buttonsLay)
LinearLayout dialogEndCount_buttonsLay;
private Window window;
private Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
//Initialize
ButterKnife.bind(this);
activity = this;
window = activity.getWindow();
//Set color to statusBar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.tabHomeColor));
}
//Set dynamically weight
dialogEndCount_buttonsLay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
How can I fix this problem?
If you want set dynamically weightSum just use this code :
dialogEndCount_buttonsLay.setWeightSum(3f);
i hope help you
You're mixing LinearLayout and RelativeLayout. Change your layout to use a LinearLayout then weight is an available property on your LayoutParams.
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
params.weight = 1f
I can make the dynamic UI without problems, but I don't know how to access the view with findviewbyid (R.id._____) I don't know what to put in the blank.
.java code
public class newList extends AppCompatActivity {
Button btnAddItem;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_list);
// allows the creation of dynamic rows
btnAddItem = (Button) findViewById(R.id.addItem);
btnAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = (ViewGroup) findViewById(R.id.linearVert);
inflater.inflate(R.layout.rows, parent);
}
});
}
}
xml that is associated with the java file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.shoppingbuddy.shoppingbuddy.newList">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="300dp"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearVert">
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Subtotal"
android:id="#+id/subtotal"
android:textColor="#ffc800"
android:layout_above="#+id/tax"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tax"
android:id="#+id/tax"
android:textColor="#ee00ff"
android:layout_above="#+id/total"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total"
android:id="#+id/total"
android:textColor="#ff0000"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/subtotalAmount"
android:layout_alignBottom="#+id/subtotal"
android:layout_alignEnd="#+id/scrollView"
android:textColor="#ffc800" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/taxAmount"
android:layout_above="#+id/total"
android:layout_alignParentEnd="true"
android:textColor="#ee00ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/totalAmount"
android:layout_alignBottom="#+id/total"
android:layout_alignEnd="#+id/taxAmount"
android:textColor="#ff0000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/addItem"
android:layout_above="#+id/subtotal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" />
</RelativeLayout>
xml used to create rows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="1"
android:id="#+id/linearHor">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/itemImage"
android:src="#drawable/box"
android:padding="5dp" />
<EditText
android:layout_width="109dp"
android:layout_height="wrap_content"
android:id="#+id/itemName"
android:hint="Name"
android:layout_weight="0.78" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/itemPrice"
android:hint="$ Price" />
</LinearLayout>
</LinearLayout>
There will be no unique R.id value at the activity level. For example, suppose the user taps the button 10 times, so you inflate the R.layout.rows layout 10 times. As a result, there will be 10 R.id.linearHor widgets, 10 R.id.itemPrice widgets, and so on.
Always call findViewById() on something that is guaranteed to give you a unique result. In this case — as with ListView, GridView, RecyclerView, etc. — you need to use other approaches to find the correct row View, then call findViewById() on that View to get at its child widgets.