I am trying to replicate this simple use case in ConstraintLayout. Basically it is 2 views left aligned to each other, but centered within parent.
Without CL, it would be as follows
FrameLayout
match_parent
match_parent
LinearLayout
orientation vertical
width wrap_content
height wrap_content
layout_gravity center_horizontal
gravity left
View
width wrap_content
View
width wrap_content
With CL im struggling how to center them both as a group, I can center them each, with constraints to left parent, right parent, however I want the group to be centered, and withing that group, left gravity. Ive tried using barriers to left and right of the two views, but it doesn center the chain horizontally. Basically I need a horizontal chain out of barriers, I think.
Edit:
I----AAA ----I
I----BBBBB----I
I-------------I
I-------------I
You can do it programmatically:
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:background="#303F9F"
android:text="Short Text"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
android:gravity="start" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="24dp"
android:background="#303F9F"
android:text="This is a very long text"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:layout_marginRight="8dp"
android:gravity="start" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>
The background color is the see the change.
In code get width of the larger TextView and set it to the smaller one.
textView1 = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
textView1.measure(0,0);
textView2.measure(0,0);
int len1 = textView1.getMeasuredWidth();
int len2 = textView2.getMeasuredWidth();
if (len1 > len2) {
textView2.getLayoutParams().width = len1;
} else {
textView1.getLayoutParams().width = len2;
}
And you don't always have to use ConstraintLayout.
Maybe below code will address your problem
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text2 Test2 Test2"
app:layout_constraintTop_toBottomOf="#id/centered"
app:layout_constraintStart_toStartOf="#id/text1" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/centered"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/centered"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.50" />
Assuming you have two views (view1 being the shorter but at the top and view2 being the longer but at the bottom).
You can do something like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:apps="http://schemas.android.com/apk/res-auto">
<View
android:id="#+id/view1"
android:layout_width="120dp"
android:layout_height="30dp"
android:background="#android:color/holo_blue_bright"
apps:layout_constraintLeft_toLeftOf="#+id/view2"
apps:layout_constraintTop_toTopOf="parent"
apps:layout_constraintBottom_toTopOf="#+id/view2"
apps:layout_constraintVertical_chainStyle="packed"
/>
<View
android:id="#+id/view2"
android:layout_width="240dp"
android:layout_height="30dp"
android:background="#android:color/holo_blue_dark"
apps:layout_constraintLeft_toLeftOf="parent"
apps:layout_constraintRight_toRightOf="parent"
apps:layout_constraintTop_toBottomOf="#+id/view1"
apps:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
Do note layout_constraintVertical_chainStyle set to packed being used that would have both views pushed together.
Also, observe the constraints between both the views to enable it to be laid out the way you want. View2, in a way, is dictating to view1 on how it is centred. A good thing about using ConstraintLayout here is that you don't need to define an extra group (ViewGroup) just to lump the two views together, just define the constraints well enough and it will do its magic.
This will produce something like this:
I'm not sure if this is what you are looking for?
Related
I am centering an image on the screen as follows:
<androidx.constraintlayout.motion.widget.ConstraintLayout
android:id="#+id/dashboardConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/logo"
android:layout_width="155dp"
android:layout_height="20dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/ic_logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.motion.widget.ConstraintLayout>
What I would like to do is have the image offset downward by 50dp from its center position. Adding a marginTop does not work. All that does is adds a margin at the top of the screen where the image is constrained. The image still remains in the center. But I want it shifted down by 50dp.
I tried it and it works fine and I have highlighted the code that I have changed
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".Third">
<androidx.appcompat.widget.AppCompatImageView //it is normar imageView in centered
android:id="#+id/logo"
android:layout_width="155dp"
android:layout_height="20dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatImageView //in this imageView i used marginTop and it works perfectly
android:id="#+id/logo1"
android:layout_width="155dp"
android:layout_height="20dp"
android:scaleType="centerCrop"
android:src="#drawable/two"
android:layout_marginTop="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatImageView //in this imageView i used vertical_bias and it work perfectly.
android:id="#+id/logo2"
android:layout_width="155dp"
android:layout_height="20dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher_background"
app:layout_constraintVertical_bias="0.60" //there is 0.0=Top, 0.50=center, 1.0=Bottom and so on.
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Output is:-
Suppose i have a horizontal GuideLine at 30% of parent and a View (suppose a Button) how can i make that view be centered on the guideline?
Like this:
Update:
The view i'm using here has height based on ratio, and soltuion on answers doesnt works.
Here is current layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<ImageView
android:id="#+id/imgLogo"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#id/guideline_30"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="#+id/guidelineV_75"
app:layout_constraintStart_toEndOf="#+id/guidelineV_25"
app:layout_constraintTop_toTopOf="#+id/guideline_30"
app:srcCompat="#drawable/ic_launcher_background" />
<android.support.constraint.Guideline
android:id="#+id/guideline_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<android.support.constraint.Guideline
android:id="#+id/guidelineV_25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<android.support.constraint.Guideline
android:id="#+id/guidelineV_75"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
</android.support.constraint.ConstraintLayout>
Using fix width or height
Need to use same Guideline for both top and bottom constraint like:
app:layout_constraintTop_toTopOf="#id/guideline_30"
app:layout_constraintBottom_toBottomOf="#id/guideline_30"
xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgLogo"
android:layout_width="150dp"
android:layout_height="150dp"
app:layout_constraintTop_toTopOf="#id/guideline_30"
app:layout_constraintBottom_toBottomOf="#id/guideline_30"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:src="#drawable/splash_logo" />
<android.support.constraint.Guideline
android:id="#+id/guideline_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.30" />
</android.support.constraint.ConstraintLayout>
output:
Using width and height based on the ratio
As you are using both width and height based on ratio so that it's not centered.
But to get your desired result you can also set Guideline = 0.15 (0.3 / 2) and modify constraintTop and constraintBotttom
xml:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imgLogo"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toStartOf="#+id/guidelineV_75"
app:layout_constraintStart_toEndOf="#+id/guidelineV_25"
app:layout_constraintTop_toTopOf="#+id/guideline_15"
app:srcCompat="#drawable/ic_launcher_background" />
<android.support.constraint.Guideline
android:id="#+id/guideline_15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.15" />
<android.support.constraint.Guideline
android:id="#+id/guidelineV_25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.25" />
<android.support.constraint.Guideline
android:id="#+id/guidelineV_75"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
</android.support.constraint.ConstraintLayout>
output:
You need to constraint both the top and the bottom of the View to the guideline, like so:
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="#id/guideline"
app:layout_constraintBottom_toBottomOf="#id/guideline" />
In ConstraintLayout, for centering the View over Vertical axis of any other View Component (e.g. Guidelines, TextViews, ImageViews, layouts, etc.), you can simply follow these two steps, in XML:-
1) app:layout_constraintTop_toTopOf="#id/componentId"
2) app:layout_constraintBottom_toBottomOf="#id/componentId"
This will Equally distribute the component's View in the Vertical center.
BONUS:-
For centering the View in the Horizontal axis, use
1) app:layout_constraintLeft_toLeftOf="#id/componentId"
or
app:layout_constraintStart_toStartOf="#id/componentId"
2) app:layout_constraintRight_toRightOf="#id/componentId"
or
app:layout_constraintEnd_toEndeOf="#id/componentId"
I am adding a couple of Textviews and a Seekbar in a Constraint Layout. I have defined textview height according to the screen height and want the seekbar height to go from one textview to the other. The code is
<TextView
android:id="#+id/detailTaskHighTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45"
android:text="High"/>
<TextView
android:id="#+id/detailTaskLowTextId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.75"
android:text="Low"/>
<SeekBar
android:id="#+id/detailTaskPriorityId"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="3"
android:progress="3"
android:rotation="270"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#id/detailTaskLowTextId"
app:layout_constraintTop_toTopOf="#id/detailTaskHighTextId"
app:layout_constraintLeft_toRightOf="#id/detailTaskHighTextId"/>
I don't want to hardcode the seekbar width. The seekbar width is really small here. Using the Top_toTopOf and Bottom_toBottomOf just brings seekbar in the middle of the two text views. I tried Start_toStart and End_toEnd as well. What am I missing? This is what I am seeing Link to image
You need to change height (using MATCH_CONSTRAINT) of seek bar like this:
<SeekBar
android:id="#+id/detailTaskPriorityId"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="wrap_content"
android:layout_height="0dp" <- MATCH_CONSTRAINT
android:max="3"
android:progress="3"
android:rotation="270"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#id/detailTaskLowTextId"
app:layout_constraintTop_toTopOf="#id/detailTaskHighTextId"
app:layout_constraintLeft_toRightOf="#id/detailTaskHighTextId"/>
I want to display the list view in half of the screen and the other half contains some static content. When i tried to put the listview, the whole screen is repeated in the loop. I want only that particular part the screen in listview. Can anyone help?
This is my MainActivity.java
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<6;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("text", menu[i]);
hm.put("icon", Integer.toString(image[i]) );
aList.add(hm);
}
String[] from = { "icon","text" };
int[] to = { R.id.icon,R.id.text};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.activity_test_main, from, to);
ListView listView = ( ListView ) findViewById(R.id.list);
listView.setAdapter(adapter);
This my activity_test_main.xml
<?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"
android:background="#drawable/new_background"
>
<ImageView
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginTop="30dip"
android:layout_gravity="center_horizontal"
android:src="#drawable/katrina" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Katrina"
android:textColor="#FFFFFF"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="katrina#gmail.com"
android:textColor="#FFFFFF"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"/>
<RelativeLayout
android:id="#+id/single_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:textColor="#FFFFFF"/>
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
If I'm getting this right you want to display the ListView below the arrow ImageView?
You can simply put the ListView below the ImageView inside the RelativeLayout, then set the android:layout_below param to the id of the ImageView...arrow and finally make sure your ListView's id is set to list
I'm trying to mimic an action in the Twitter android app. When you long press/swipe a tweet (list item) it looks like this
This makes it easy for the user to take actions on the list item without leaving the screen. How can I do something similar in my app?
For now I've added a context menu which looks like this (notice that it fades out rest of the app).
I achieved this by registerForContextMenu(listView)
Currently, my list is like this:
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, myList,
R.layout.list_items, new String[] {LI_ID,
LI_NAME, LI_COUNT}, new int[] {
R.id.li_id, R.id.li_name, R.id.li_count});
setListAdapter(adapter);
and my layout is simply like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/li_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/li_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="10dip"
android:textStyle="bold"/>
<TextView android:id="#+id/li_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#9ed321"
android:paddingRight="3dip"
android:paddingLeft="3dip"/>
</RelativeLayout>
Have you looked tried an expandable list view ?
http://developer.android.com/reference/android/widget/ExpandableListView.html
http://www.youtube.com/watch?v=mwE61B56pVQ
You could just nest another layout in each individual ListView item that's initially invisible, then just toggle the visibility of the view on a long click event.
For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true" >
<TextView
android:id="#+id/main_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:padding="16dp"
android:layout_gravity="center" />
<LinearLayout
android:id="#+id/hidden_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
Then in your activity, just set an onItemLongClickListener on your ListView, and toggle the
visibility of the hidden_view:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
LinearLayout mView = view.findViewById(R.id.hidden_view);
switch(mView.getVisibility()){
case View.VISIBLE:
mView.setVisibility(View.GONE);
break;
case View.GONE:
mView.setVisibility(View.VISIBLE);
break;
}
}
}