xyz
xyz

Reputation: 71

How to make my simple activity as floating activity in android-studio?

This is my addbudget class

public class addbudget extends ActionBarActivity implements View.OnClickListener{


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.addbudget);

                btn66=(Button)findViewById(R.id.btn_addBudget);
                btn66.setOnClickListener(this);
                helper=new DBhelper(addbudget.this);
                txr=(TextView)findViewById(R.id.addbud);

                txtBudget=(EditText)findViewById(R.id.etBudget);

                list = (ListView) findViewById(R.id.list);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                        String h;
                        Cursor row = (Cursor) arg0.getItemAtPosition(position);
                        selected_did = row.getString(0);
                        h = row.getString(1);

                        txtBudget.setText(h);





                    }

                });


                Bundle data_from_list= getIntent().getExtras();
                value_in_tv= data_from_list.getString("passed data key");
                txr.setText(value_in_tv);

                fetchData2();
            }




            private void clearfield(){
                txtBudget.setText("");

            }


            public void onClick(View v) {

                    if (btn66 == v) {
                        checkIfRowPresent(txr.getText().toString());
                    }
                }

            public boolean checkIfRowPresent(String name) {
                SQLiteDatabase db = helper.getWritableDatabase();

                Cursor cursor =
                        db.query(DBhelper.TABLE2, null, DBhelper.Description + "='" + name + "'", null, null, null,
                                null, null);
                boolean ret = false;
                if (cursor.getCount() > 0) {

                    Toast.makeText(this, "Budget not add Successfully", Toast.LENGTH_LONG).show();// There is a row present in table1 with the given name
                }
                else{


                    ContentValues value = new ContentValues();
                    value.put(DBhelper.Amount, txtBudget.getText().toString());
                    value.put(DBhelper.Description, txr.getText().toString());


                    db = helper.getWritableDatabase();
                    db.insert(DBhelper.TABLE2, null, value);
                    db.close();
                    clearfield();
                    Toast.makeText(this, "Budget add Successfully", Toast.LENGTH_LONG).show();
                    fetchData2();
                    Intent i = new Intent(addbudget.this, MainActivity.class);
                    startActivity(i);
                }
                db.close();

                return ret;

            }


            private void fetchData2() {
                db = helper.getReadableDatabase();
                Cursor c = db.query(DBhelper.TABLE2, null, DBhelper.Description + "='" + value_in_tv+"'", null, null, null, null);
                adapter = new SimpleCursorAdapter(
                        this,
                        R.layout.addbudget,
                        c,
                        new String[]{DBhelper.Amount},
                        new int[]{R.id.lbl});
                list.setAdapter(adapter);
            }

        }

This is my addbudget.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="25dp"
        android:paddingBottom="@dimen/activity_vertical_margin"
      >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Category         "
            android:id="@+id/textView3"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Budget"
            android:layout_marginTop="20dp"
            android:id="@+id/textView4"
            android:layout_below="@+id/spinner_cat"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/spinner_cat"
            android:entries="@array/array_categories"
            android:layout_alignTop="@+id/textView3"
            android:layout_toRightOf="@+id/textView3"
            android:layout_toEndOf="@+id/textView3" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/etBudget"
            android:layout_below="@+id/spinner_cat"
            android:layout_alignLeft="@+id/spinner_cat"
            android:layout_alignStart="@+id/spinner_cat" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Budget"
            android:id="@+id/btn_addBudget"

            android:layout_marginTop="30dp"

            android:layout_below="@+id/etBudget"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignRight="@+id/etBudget"
            android:layout_alignEnd="@+id/etBudget" />
    </RelativeLayout>

In another xml layout ,I have one button,if I click that button,this activity needs to open as floating activity.

I Google this question,but the answers seems little tricky.I didnt understand that much,can anyone help me to change the above activity to floating activity?

Upvotes: 2

Views: 4805

Answers (1)

Tonespy
Tonespy

Reputation: 3387

This is a step by step implementation. I'll be copy pasting my code and explaining it too.

In your style.xml add this and you can modify it as you see fit

 <style name="PopupTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>

    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="colorAccent">@color/color_primary</item>
</style>

Create your Activity file and it Layout too.

In your Activity's .class file you can handle screen sizes in the onAttachWindow Override methods E.g:

 @Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

    switch (screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            this.getWindow().setLayout(900, 755);
            break;
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
            this.getWindow().setLayout(1080, 1000); //width x height
            break;
    }
}

And finally, in your AndroidManifest.xml file add this

<activity android:name=".MyFloatingActivity"
        android:enabled="true"
        android:theme="@style/PopupTheme"<--The theme in style
        android:label="@string/app_name"/>

Upvotes: 2

Related Questions