Search code examples
androidandroid-layoutandroid-custom-view

Layout with custom view


Hi i have an activity class that has a layout, the layout itself has a custom view, this is the activity class:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class Minesweeper extends Activity {

public static final String KEY_DIFFICULTY = "minesweeper.difficulty";
public static final int DIFFICULTY_EASY = 1;
public static final double DIFFICULTY_MEDIUM = 1.5;
public static final int DIFFICULTY_HARD = 2;
private int diff;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
    diff++;
    Log.d("diff", String.valueOf(diff));
    Mine.setlevel(diff);
    setContentView(R.layout.minesweeper);
    Mine.clearMines();
}
} 

and here's the layout of the activity:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background" >

    <com.mine_sweeper.Table
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>`

everything's good on graphical view in eclipse on layout file but when i try to add a button to the layout file it only sticks to the left side of my custom view witch is a 9*9 grid, so my question is how to move it under my custom veiw? i have tried changing custom view's layout_width and layout_height already.


Solution

  • Try this. User vertical or horizontal to change positions in nested LinearLayout. android:orientation="vertical" or android:orientation="horizontal" `

     <?xml version="1.0" encoding="UTF-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
    
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <Button
            android:id="@+id/button"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Button" />
    
       <com.mine_sweeper.Table
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
    
    </LinearLayout>
    

    `

    You can also user other layouts like GridLayout etc. this is just one approach using LinearLayout