You have many possibilities to create this kind of layout:
- A parent
RelativeLayout
, a LinearLayout
with 4 dots at the top and for the bottom display your views with the attributes as toLeftOf, toRightOf, etc. - make an onClickListener
method to change their state and save the number.
- Multiple
LinearLayout
s with weight
attributes to fill the entire space and each of the views (rounded number) fill the space (see the example below to understand the weight attribute) - or several RelativeLayout
s - or even a TableLayout
.
- Or
LinearLayout
and GridView
below with an ItemClickListener
method..
According to the comments below your question, there is a lot of possibilities. I choose one of them with Linear and RelativeLayout. It might be something like this:
<!-- named activity_main.xml -->
<RelativeLayout
... >
<LinearLayout
android:id="@+id/contain"
... android:layout_width="250dip"
android:orientation="horizontal"
android:weightSum="4" >
<!-- weightSum to 4 = whatever the screen, display
my children views in 4 sections -->
<View
... android:layout_weight="1"
android:background="@drawable/green_dots" />
<!-- weight to 1 = this takes one section -->
<View
... android:layout_weight="1"
android:background="@drawable/green_dots" />
<!-- weight to 1 = this takes one section -->
<View
... android:layout_weight="1"
android:background="@drawable/green_dots" />
<View
... android:layout_weight="1"
android:background="@drawable/green_dots" />
</LinearLayout>
<RelativeLayout
android:layout_below="@id/contain" ... >
... Here display your buttons (or textviews) with
custom drawable background for each one
</RelativeLayout>
And in your Activity
class, it implements OnClickListener
like this:
public class MyActivity extends Activity implements OnClickListener { }
Then in your methods inside this Activity:
// init your buttons var
Button one, two, three, four, five ...;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the layout above
setContentView(R.layout.activity_main);
// init your buttons
one = (Button) findViewById(R.id.button1);
two = (Button) findViewById(R.id.button2);
three = (Button) findViewById(R.id.button3);
... etc.
// set them to your implementation
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
... etc.
}
// call this function when one button is pressed
public void onClick(View view) {
// retrieves the id of clicked button
switch(view.getId()) {
case R.id.button1:
methodToSaveNumber(int);
break;
case R.id.button2:
methodToSaveNumber(int);
break;
case R.id.button3:
methodToSaveNumber(int);
break;
... etc.
}
}
Then in your methodToSaveNumber
method:
// finally, your method to save the number of the password
public void methodToSaveNumber(int i) {
... do something.
... change the state of the buttons, the dots, whatever you want
}
And just to show you how it works, the drawable green_dots
might be like this:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- radius -->
<corners
android:radius="20dp" />
<!-- border -->
<stroke
android:color="@android:color/green"
android:width="2dp" />
<!-- background (transparent) -->
<solid
android:color="#00000000" />
</shape>
You must to inform you about the layouts and how it works, the click event and its listener, the drawables and their states (focused, pressed, disabled, ...), and finally, I hope you will get what you want.
Happy coding!