Search code examples
androidbuttononcreate

No changes being applied to my Activity


I am trying to just test if some of these approaches work, but they don't seem to be changing anything. The code in question is below.

public class Matching extends AppCompatActivity{

Button button1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_flipmodule);

    button1 = (Button) findViewById(R.id.button1);

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    button1.setHeight((int)(height*.3));
    button1.setWidth((int)(width*.3));

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}
}

First off, I wanted to get rid of the bar across the top of the screen that says the name of the activity class it is being called from, so I just used the line this.requestWindowFeature(Window.FEATURE_NO_TITLE); before I set the content view, but that doesn't seem to do anything.

Also, I was getting extremely frustrated trying to get my xml buttons to scale with the screen (which I still don't think is possible through xml, correct me if I am wrong). So I decided to just do it in my onCreate for the Intent. I got the width and height but for some reason when I tried to adjust the width and height via setHeight and setWidth, it doesn't change the button size.

What simple thing am I overlooking with these two problems? Any help is appreciated, thanks.

EDIT:

My plan here is to add buttons to the table and then resize them later, sorry its a bit sloppy with all the table rows in there

<?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:layout_width="match_parent"
    android:layout_height="55dp"
    android:orientation="horizontal">
</LinearLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp">

        <Button
            android:id="@+id/button1"
            android:layout_width="@dimen/box_size"
            android:layout_height="@dimen/box_size"
            android:text="Button" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</TableLayout>
</LinearLayout>

Solution

  • Update

    To remove the title bar across the top of the screen, make sure your Activity's theme has these:

        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    

    Original

    To make buttons that scale with screen size, you can use ConstraintLayout. Add some Guidelines at 33% and 67% and position your buttons relative to them. Here's an example with just one button:

    <?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">
    
        <android.support.constraint.Guideline
            android:id="@+id/leftRail"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.33"/>
    
        <android.support.constraint.Guideline
            android:id="@+id/rightRail"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.67"/>
    
        <android.support.constraint.Guideline
            android:id="@+id/topRail"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.33"/>
    
        <android.support.constraint.Guideline
            android:id="@+id/bottomRail"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.67"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="scales with screen size"
            app:layout_constraintTop_toTopOf="@id/topRail"
            app:layout_constraintLeft_toLeftOf="@id/leftRail"
            app:layout_constraintRight_toRightOf="@id/rightRail"
            app:layout_constraintBottom_toBottomOf="@id/bottomRail"/>
    
    
    </android.support.constraint.ConstraintLayout>
    

    enter image description here