You could create a group of objects (which are the options) and programmatically ensure that only one of them can be selected. selection could be presented as you wish it would be, I'd give a code example that imitates approximately the image you've attached. here's the structure:
XML file: CardViews with base of invisible FrameLayout (will be functioning as stroke), above the base is the cards' content
Java Class: setChecked(MaterialCardView selected) method that selects the chosen card (making it's 'stroke' visible) and checkedCard() that returns the selected card
so here's the code:
activity_main.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"
android:background="@color/parentBackground"
tools:context=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="@+id/public_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="@+id/public_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/selectedCardStroke"
android:visibility="visible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="@+id/card1Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="@mipmap/ic_public" />
<TextView
android:id="@+id/card1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/card1Icon"
android:text="Public Profile"
android:textAllCaps="false"
android:textColor="@color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="@+id/card1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/card1Label"
android:layout_alignStart="@id/card1Label"
android:text="Everyone can see your account. \nIncluding non followers."
android:textAllCaps="false"
android:textColor="@color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/friends_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/public_card"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="@+id/friends_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="@+id/card2Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="@mipmap/ic_friends" />
<TextView
android:id="@+id/card2Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/card2Icon"
android:text="Friends Only"
android:textAllCaps="false"
android:textColor="@color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="@+id/card2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/card2Label"
android:layout_alignStart="@id/card2Label"
android:text="Connections nly. Only your friends \nand friends of friends can see your account."
android:textAllCaps="false"
android:textColor="@color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/private_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/friends_card"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="@+id/private_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="@+id/card3Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="@mipmap/ic_private" />
<TextView
android:id="@+id/card3Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/card3Icon"
android:text="Private"
android:textAllCaps="false"
android:textColor="@color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="@+id/card3Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/card3Label"
android:layout_alignStart="@id/card3Label"
android:text="Only you can see your account."
android:textAllCaps="false"
android:textColor="@color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public MaterialCardView cPublic, cFriends, cPrivate;
public FrameLayout sPublic, sFriends, sPrivate;
public ArrayList<MaterialCardView> cards;
public ArrayList<FrameLayout> strokes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = new ArrayList<>();
strokes = new ArrayList<>();
cPublic = findViewById(R.id.public_card);
cFriends = findViewById(R.id.friends_card);
cPrivate = findViewById(R.id.private_card);
cards.add(cPublic);
cards.add(cFriends);
cards.add(cPrivate);
sPublic = findViewById(R.id.public_stroke);
sFriends = findViewById(R.id.friends_stroke);
sPrivate = findViewById(R.id.private_stroke);
strokes.add(sPublic);
strokes.add(sFriends);
strokes.add(sPrivate);
cPublic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setChecked(cPublic);
}
});
cFriends.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setChecked(cFriends);
}
});
cPrivate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setChecked(cPrivate);
}
});
}
public void setChecked(MaterialCardView selected) {
int index = cards.indexOf(selected);
FrameLayout stroke = strokes.get(index);
stroke.setVisibility(View.VISIBLE);
for (FrameLayout s : strokes) {
if (!s.equals(stroke)) {
s.setVisibility(View.INVISIBLE);
}
}
}
public MaterialCardView checkedCard() {
int index = 0;
for (FrameLayout s : strokes) {
if (s.getVisibility() == View.VISIBLE) {
index = strokes.indexOf(s);
}
}
return cards.get(index);
}
}
Hope it could help you (:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.