user6412889
user6412889

Reputation: 39

(Android) Put tablelayout inside alertdialog popup

This is my tablelayout:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    android:id="@+id/dialog_table_results"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:weightSum="1"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow
        android:id="@+id/headerrow"

        >



        <TextView
            android:text=""
            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />
        <TextView
            android:text="@string/matching"

            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:textAlignment="center"
            android:layout_height="wrap_content"/>

        <TextView
            android:text="@string/counting"
            android:layout_width="0dp"
            android:layout_height="wrap_content"  android:textAlignment="center"
            android:layout_weight="0.25"/>

        <TextView
            android:text="@string/choosing"
            android:layout_width="0dp"  android:textAlignment="center"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"/>

    </TableRow>

    <TableRow
        android:id="@+id/subheaderrow"
        >



        <TextView
            android:text=""
            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/wronganswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/wronganswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/wronganswer" android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>


    </TableRow>





</TableLayout>

This is table row for adding in activity, i added tablelayout row which is still in layout, it is not important i guess.

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/table_row"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        >

        <TextView
            android:text=""
            android:layout_weight="0.25"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/object_name"
            />



        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp" android:id="@+id/true_choosing"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

    <TextView
        android:text="@string/wronganswer" android:layout_width="0dp" android:id="@+id/half_choosing"
        android:layout_height="wrap_content"
        android:layout_weight="0.0833"/>

        <TextView

            android:text="@string/halfanswer" android:layout_width="0dp" android:id="@+id/false_choosing"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>



        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp" android:id="@+id/true_matching"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

    <TextView
        android:text="@string/wronganswer" android:layout_width="0dp" android:id="@+id/half_matching"
        android:layout_height="wrap_content"
        android:layout_weight="0.0833"/>

        <TextView
            android:text="@string/halfanswer" android:layout_width="0dp" android:id="@+id/false_matching"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>



        <TextView
            android:text="@string/trueanswer" android:layout_width="0dp" android:id="@+id/true_counting"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>

    <TextView
        android:text="@string/wronganswer" android:layout_width="0dp" android:id="@+id/half_counting"
        android:layout_height="wrap_content"
        android:layout_weight="0.0833"/>

    <TextView
            android:text="@string/halfanswer" android:layout_width="0dp" android:id="@+id/false_counting"
            android:layout_height="wrap_content"
            android:layout_weight="0.0833"/>



    </TableRow>

I look from here How to add rows dynamically into table layout

This is in my activity:

TableLayout resultsTable;//global

 resultsTable = (TableLayout) MainActivity.this.findViewById(R.id.dialog_table_results);//oncreate


  TableRow row = (TableRow)LayoutInflater.from(MainActivity.this).inflate(R.layout.table_row, null);//inmethod
    ((TextView)row.findViewById(R.id.object_name)).setText("s");

    resultsTable.addView(row);

this is full activity:

 protected void onCreate(Bundle savedInstanceState) {
            Log.d(TAG, "MAINACTIVITY oncreate");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = getIntent().getExtras().getString("username");
        wasDbExisted = getIntent().getExtras().getBoolean("wasDbExisted");

            mMediaPlayer = new MediaPlayer();

            textMain = (TextView) findViewById(R.id.text_main);
            textHalfCompletedGames = (TextView) findViewById(R.id.text_half_completed_trainings);
            textUnCompletedGames = (TextView) findViewById(R.id.text_uncompleted_trainings);
            //  textMatch = (TextView) findViewById(R.id.text_match);
            dbHandler = new DatabaseHandler(MainActivity.this);

            trainingList = new ArrayList<Training>();
            trainingHalfList = new ArrayList<Training>();

            rdioGrpUncompleted = new RadioGroup(this);
            rdioGrpHalfCompleted = new RadioGroup(this);

    //dbHandler.getAllTrainingResponse();//// TODO: 5/25/2016 sil
    //dbHandler.getAllTraining("t");//// TODO: 5/25/2016 sil
            //  dbHandler.getAllObjectObject();     //// TODO: 5/25/2016 sil
            // dbHandler.getAllTrainingObject();     //// TODO: 5/25/2016 sil
            // Log.d(TAG,"LEVEL:"+ dbHandler.getCurrentLevel(86));

            textMain.setText("Hoşgeldin " + username);


            backgroundImg = new ImageView(this);
            backgroundImg = (ImageView) findViewById(R.id.background_img);

            //  GlideDrawableImageViewTarget ivTarget = new GlideDrawableImageViewTarget(ivImg);
            Glide.with(this)
                    .load(R.raw.gif_mainactivity) // The image you want to load
                    .crossFade()
                    .placeholder(R.drawable.img_mainactivity_background_placeholderimg) // The placeholder GIF.
                    .into(backgroundImg);

            //resultsTable = (TableLayout) findViewById(R.id.dialog_table_results);

            TableLayout rresultsTable = (TableLayout) MainActivity.this.findViewById(R.id.dialog_table_results);
            TableRow tr = new TableRow(this);
            tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

            //Create text views to be added to the row.
            TextView name = new TextView(this);
            name.setText("f");
            name.setTextSize(16);
            name.setTypeface(Typeface.DEFAULT_BOLD);
            TableRow.LayoutParams nameParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.6f);
            nameParams.setMargins(7, 5, 0, 0);

            TextView quantity = new TextView(this);
            quantity.setText("g");
            quantity.setTextSize(16);
            quantity.setTypeface(Typeface.DEFAULT_BOLD);
            TableRow.LayoutParams quantityParams = new TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.2f);
            quantityParams.setMargins(0, 5, 0, 0);

            TextView totalPrice = new TextView(this);
            totalPrice.setTextSize(16);
            totalPrice.setTypeface(Typeface.DEFAULT_BOLD);
            TableRow.LayoutParams totalPriceParams = new TableRow.LayoutParams( TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 0.2f);
            totalPriceParams.setMargins(0, 5, 0, 0);

            tr.addView(name,nameParams);
            tr.addView(quantity,quantityParams);
            tr.addView(totalPrice,totalPriceParams);

            rresultsTable.addView(tr);

 settingsMenuPopup = new ImageView(this);
        settingsMenuPopup = (ImageView) findViewById(R.id.settings_imgbtn);


        settingsMenuPopup.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {

                popup = new PopupMenu(MainActivity.this, settingsMenuPopup);

                //Inflating the Popup using xml file
                popup.getMenuInflater()
                        .inflate(R.menu.popupmenu_main, popup.getMenu());

  //registering popup with OnMenuItemClickListener
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        //Log.d(TAG, "you clciked " + item.getTitle() + " id=" + item.getItemId()+"   item.getGroupId():"+  item.getGroupId());//// TODO: 6/2/2016 sil buralari
                        Log.d(TAG, "you clciked " + item.getTitle() + " id=" + item.getItemId() + " item:" + item.toString());

                        if (item.getTitle().toString().contains("onuc")) {//sonuclarinizi gorunse
                            Log.d(TAG, "you clciked sonuc gör");

                            showPopupDialog();

                        } 

private void showPopupDialog() {
//1 match,3 secme, 2 count

        List<String> objectNameList = new ArrayList<String>();

        objectNameList = dbHandler.getAllObjectObjectName(username);

//objectname
for(int j=0; j<objectNameList.size() ; j++) {

    TableRow table_row = new TableRow(this);
    table_row = (TableRow) findViewById(R.id.table_row);


    String objectName = objectNameList.get(j);

    ((TextView)table_row.findViewById(R.id.object_name)).setText(objectName);

    TextView [] textViewArray = new TextView[9];

    TextView t1 = new TextView(this);
    TextView t2 = new TextView(this);
    TextView t3 = new TextView(this);
    TextView t4 = new TextView(this);
    TextView t5 = new TextView(this);
    TextView t6 = new TextView(this);
    TextView t7 = new TextView(this);
    TextView t8 = new TextView(this);
    TextView t9 = new TextView(this);

    t1 = (TextView) findViewById(R.id.true_matching);
    t2 = (TextView) findViewById(R.id.half_matching);
    t3 = (TextView) findViewById(R.id.false_matching);

    t4 = (TextView) findViewById(R.id.true_counting);
    t5 = (TextView) findViewById(R.id.half_counting);
    t6 = (TextView) findViewById(R.id.false_counting);

    t7 = (TextView) findViewById(R.id.true_choosing);
    t8 = (TextView) findViewById(R.id.half_choosing);
    t9 = (TextView) findViewById(R.id.false_choosing);

    textViewArray[1] = t1;
            textViewArray[2] = t2;
    textViewArray[3] = t3;
            textViewArray[4] = t4;
    textViewArray[5] = t5;
            textViewArray[6] = t6;
    textViewArray[7] = t7;
            textViewArray[8] = t8;
    textViewArray[9] = t9;


    //behavor
    for (int i = 1 ; i < 4; i++) {


        //response
        for(int k=1;k<4;k++){

           textViewArray[i*3+k-3].setText(dbHandler.getCountForTable(i,k,objectName,username));

        }



    }
    resultsTable.addView(table_row);
    resultsTable.requestLayout();
}
*/

        builder.setView(inflater.inflate(R.layout.activity_login, null));
//this is temporary, i will use tablelayout
        builder.create();
        builder.show();

}

I need that alertdialog when suerclicks popup menu. How can i manage without using fragment?

there is a view of alertdialog. I tried to add layout as view there but i cant manage. I am really new and dont know about those.

How to place a table layout inside the custom dialog?

There are only a few answers about that and most of them are comments or they suggest fragments.

Upvotes: 0

Views: 1259

Answers (1)

ugali soft
ugali soft

Reputation: 2789

This is how i do to inflate view to my popup alert dialog

final AlertDialog.Builder alert = new AlertDialog.Builder(context);

View your_view = LayoutInflater.from(cntx).inflate(R.layout.name_of_layout, null);

alert.setView(your_view);

Upvotes: 1

Related Questions