0

I have an activity with a fragment on the left side and when i click an item on the listview, i want to add a button on the right side.

Sorry for the very long post..

Fragment:

public class BoardFragment extends Fragment implements Button.OnClickListener{

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private Button bCreate;
private Button bViewMed;
private DatePicker datePicker;
private Button bSchemeSelection;
private TextView txt;
private ListView listView;
private OnFragmentInteractionListener mListener;
private ViewGroup viewGroup;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = null;
    view = inflater.inflate(R.layout.fragment_board, container, false);
    bCreate = (Button) view.findViewById(R.id.bCreateScheme);
    bCreate.setOnClickListener(this);

    bViewMed = (Button) view.findViewById(R.id.bSeeMessage);
    bViewMed.setOnClickListener(this);

    bSchemeSelection = (Button) view.findViewById(R.id.bSelection);
    bSchemeSelection.setOnClickListener(this);

    datePicker = (DatePicker) view.findViewById(R.id.datePicker);
    txt = (TextView) view.findViewById(R.id.createrHeader);
    listView = (ListView) view.findViewById(R.id.listActivities);
    viewGroup = (ViewGroup) view.findViewById(R.id.schemeContainer);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            //String value = listView.getSelectedItem().toString();
            Toast.makeText(adapterView.getContext(),"hej",Toast.LENGTH_SHORT).show();

            LinearLayout ll = new LinearLayout(adapterView.getContext());
            LinearLayout.LayoutParams ll_settings =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);
            ll.setLayoutParams(ll_settings);

            ll.setOrientation(LinearLayout.HORIZONTAL);

            Button bb = new Button(adapterView.getContext());
            bb.setText("hej");
            RelativeLayout.LayoutParams bb_settings = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            bb.setLayoutParams(bb_settings);
            ll.addView(bb);

            viewGroup.addView(ll);
        }
    });

    return view;
}

Activity

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class BoardCreator extends Activity
    implements BoardFragment.OnFragmentInteractionListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_board_creator);
    if(savedInstanceState ==null){
        getFragmentManager().beginTransaction()
                .add(R.id.toolbar,new BoardFragment())
                .commit();
    }
}

public void onFragmentInteraction(Uri uri){
    /*TextView txt = (TextView) findViewById(R.id.createrHeader);
    txt.setVisibility(View.VISIBLE);
    ListView listView = (ListView) findViewById(R.id.listActivities);
    listView.setVisibility(View.VISIBLE);*/
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.board_creator, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
    }
}

XML activity:

<LinearLayout 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="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.karljohan_schmidt.myapplication.BoardCreator">

<LinearLayout
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </FrameLayout>



</LinearLayout>

<FrameLayout
    android:id="@+id/schemeContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ScrollView>
</FrameLayout>


</LinearLayout>

XML fragment:

<LinearLayout 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:orientation="vertical"
tools:context="com.example.karljohan_schmidt.myapplication.BoardFragment">

<!-- TODO: Update blank fragment layout -->

<Button
    android:id="@+id/bCreateScheme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Opret skema"/>

<Button
    android:id="@+id/bSeeMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Se meddelelser"/>

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:calendarViewShown="false">
    </DatePicker>

<Button
    android:id="@+id/bSelection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Vis skema for valgte dag"
    />

<TextView
    android:id="@+id/createrHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Tryk på aktivitet for at tilføje:"
    android:visibility="invisible"/>
<ListView
    android:id="@+id/listActivities"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:entries="@array/activities"
    android:clickable="true"
    android:visibility="invisible"
    android:focusable="false">
</ListView>

</LinearLayout>

2 Answers2

0

Try this code

Button myButton=new Button(this);    
myButton.setId(btn);    
myButton.setBackgroundResource(R.drawable.image);    
myButton.setMinimumHeight(150);    
myButton.setMinimumWidth(150);      
Relativelayout.addView(myButton);     

Other attributes are optional. Also u can RelativeLayout to whatever layout u have choosen.

Refer this link for more info: How to add a button dynamically in Android?

Community
  • 1
  • 1
Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36
0

Add your button in the xml layout as usually, but set the visibility to gone

<Button
    android:id="@+id/myButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone">

Now your button is not taken into account when creating the layout at the beginning. While gone, the button is invisible, does not take up any space, and cannot be clicked/pressed. Afterwards you can change the buttons visibility dynamically (in your on click listener in the other button for example)

somewhere in activity or OnClickListener :

findViewById(R.id.myButton).setVisibility(View.VISIBLE);
RaINqer
  • 436
  • 1
  • 4
  • 11