removing a view from the activity - android

In my main activity java code file, I'm adding a new view with this method:
public void AddNewView(View view) {
Intent intent = new Intent(this, DisplayNewView.class);
startActivity(intent);
}
This uses a class called DisplayNewView which has this method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.activity_display_view, null);
TextView tv = (TextView) view.findViewById(R.id.myViewLabel);
tv.setTextSize(40);
addContentView(view,tlp);
}
So this sucessfully adds the view.
But now, in the same xml resource file that this view uses, I have a button that is supposed to remove this view. This button is also in the same DisplayNewView java code file.
It looks like this:
public void removeView(View view) {
ViewGroup vg = (ViewGroup)(view.getParent());
vg.removeView(view);
}
But when I click on the button, nothing happens. I also don't see any errors in the debugger console in Android Studio.
From my web searches, it looks like I'm trying to remove the view in the right way.
Is there anything I'm missing?
Thanks!

I've recreated your problem and i don't see any errors in presented code. From what i understand you should have something similar to this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonadd = (Button) findViewById(R.id.addButton);
buttonadd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddNewView(view);
}
});
}
public void AddNewView(View view) {
Intent intent = new Intent(this, DisplayNewView.class);
startActivity(intent);
}
}
public class DisplayNewView extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.activity_display_view, null);
TextView tv = (TextView) view.findViewById(R.id.myViewLabel);
tv.setTextSize(40);
Button buttondelete = (Button) view.findViewById(R.id.deleteButton);
buttondelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
removeView(view);
}
});
addContentView(view, tlp);
}
public void removeView(View view) {
ViewGroup vg = (ViewGroup) (view.getParent());
vg.removeView(view);
}
}
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.stackquest.MainActivity">
<Button
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add">
</Button>
</LinearLayout>
<?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:id="#+id/activity_display_new_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.stackquest.DisplayNewView">
<TextView
android:id="#+id/myViewLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="#+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete">
</Button>
</LinearLayout>
Clicking on delete button should delete the button view. If you wish to delete all views you can use vg.removeAllViews(); and as others have mentioned why create new intent for dynamically manipulating views?

Related

Android: Exit full screen and return to main activity

Main activity contains a spinner and a button. MainActivity.java code is as follows.
public class MainActivity extends AppCompatActivity implements LoadNew.VCallback {
public Spinner dropdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropdown = (Spinner) findViewById(R.id.spinner1);
//Create a list of items for the spinner.
String[] items = new String[]{"select topic", "topic1", "topic2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// TODO Auto-generated method stub
String sp1 = String.valueOf(dropdown.getSelectedItem());
if (sp1.contentEquals("select topic")) {
loadFun("");
}
if (sp1.contentEquals("topic1")) {
loadFun("topic1");
}
if (sp1.contentEquals("topic2")) {
loadFun("topic2");
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView){
// TODO Auto-generated method stub
}
});
Button x = (Button) findViewById(R.id.full_screen);
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadFun("topic2", 0);
}
});
}
public void loadFun(String topicName, int i) {
LoadNew st = new LoadNew(this);
st.display(topicName, i);
}
}
The xml layout for main activity is as follows (activity_main.xml).
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/full_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Screen"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
<LinearLayout
android:id="#+id/full_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
There is another java class LoadNew.java as follows.
public class LoadNew {
public interface VCallback {
//To access methods of MainActivity class
}
public Activity activity;
public LoadNew(Activity _activity) {
this.activity = _activity;
VCallback callerActivity = (VCallback) activity;
}
//Display PDF
public void display(String topicName, int i) {
LinearLayout fullScreen = (LinearLayout) this.activity.findViewById(R.id.full_view);
fullScreen.removeAllViews();//Clear field
LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_load, null);
if(i ==0) {
this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.activity.setContentView(view);
}
TextView tv= (TextView) this.activity.findViewById(R.id.tv1);
tv.setText(topicName);
tv.setTextSize(100);
}
}
The activity_load.xml file is as follows.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_below="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
When an item is selected in the spinner, the corresponding text is displayed in the textview. Upon pressing the button, the textview appears in full screen mode.
Now I want to return to the previous view (by pressing back button or by single tab on screen), i.e., the view before the button was clicked. How can I achieve this? Thanks in advance for the patience and the help.
Simplest would be to have the spinner and the non-fullscreen TextView on one activity and open a second activity with the fullscreen mode on button click.

Create popup window for each gridview item - Android

I'm developing an Android application for a restaurant for an food ordering system.
In my project,I have a Gridview ,When click on a button on the item in gridview, I want to get a popup window with some textfields,buttons etc.
I got help from this document example
the above document example is working alone as single application.it's just an example for creating basic pop-up window.but I need it on Gridview.
my work -->
instead of above doc's main.xml and PopUpWinndowDemoActivity.java (it's main activity),
I use my grid_single.xml and it's implementation file Grid_single.java.
But it doesn't work for me.When I click on the each item's button,not showing the pop-up window as i hope. So, Please help me to solve this or give me a working example for pop-up window on grid view items.
I have attached my files with this.
waiting for your help,
Thank you!
Grid_single.java
Button btnClosePopup;
Button btnCreatePopup;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_single);
btnCreatePopup = (Button) findViewById(R.id.addToCart);
btnCreatePopup.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initiatePopupWindow();
}
});
btnCreatePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
initiatePopupWindow();
}
});
}
private PopupWindow pwindo;
private void initiatePopupWindow() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) Grid_Single_Popup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup, (ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 300, 370, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
};
grid_single.xml
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:padding="5dp"
android:id="#+id/grid_single_back"
>
<ImageView
android:id="#+id/grid_image"
android:layout_width="150dp"
android:layout_height="150dp">
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="24dp" >
</TextView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Cutomize & Add"
android:id="#+id/addToCart"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:textSize="18dp"
android:padding="5sp"/>
</LinearLayout>
Thanks in advance
This is working for me, check once
In grid adapter:
public CustomGrid(String[] web, int[] Imageid, Grid_single activity) {
this.Imageid = Imageid;
this.web = web;
this.activity = activity;
}
In getView method of your adapter, keep like this:
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
Button btnPopup = (Button) grid.findViewById(R.id.btnPopup);
btnPopup.setOnClickListener(activity);
Grid_single Should implement OnClickListener, and keep like this:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnPopup:
initiatePopupWindow(web[grid.getPositionForView(v)]);
break;
default:
break;
}
}

Android - Adding Buttons into an existing layout programatically

I am building a soundboard app, where I have some predefined buttons in a tablelayout, which is nested inside a relative layout. The layout is an own fragment (I use multiple tabs for different categories).
Sound and tabs are working fine, now I want to implement the function, that you generate a new button, once you click the "+" button and that the "+" button moves one row beneath the new button.
Is there a way to generate a new tablelayout, with the same attributes as the already existing without having to use blanks inside the xml-file?
try this simple example and modify as per your requirement
public class MainActivity extends Activity {
Context context;
ScrollView scrollView;
LinearLayout container;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.LinearAdd);
}
public void addButton(View view) {
Button button = new Button(MainActivity.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
button.setLayoutParams(lp);
button.setText("ADDED");
layout.addView(button);
}
}
And this your xml
<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=".MainActivity" >
<LinearLayout
android:id="#+id/LinearAdd"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addButton"
android:text="click to add" />
</LinearLayout>
</LinearLayout>
Here is my layout fragment code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="#+id/tableLayout">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_column="1">
<Button
android:background="#drawable/button"
style="#style/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tab01sound01"
android:layout_column="0"
android:text="#string/buttonAlan" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_below="#+id/tableLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/thirdTable">
</TableLayout>
</RelativeLayout>
And this is the code from my first fragment tab
public class Tab1 extends Fragment {
private int buttonAmount = MainActivity.getButtonAmountTab1();
private Button[] button = new Button[buttonAmount + 1];
private Sound sound;
private String packageName = MainActivity.getStringPackageName();
private Button addButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
sound = MainActivity.getSound();
View rootView = inflater.inflate(R.layout.fragment_fun, container, false);
//This generates the Audio functionality for each button
for (int i = 1; i < buttonAmount + 1; i++) {
String buttonID = "tab01sound0" + i;
int resID = getResources().getIdentifier(buttonID, "id", packageName);
final int audioID = getResources().getIdentifier(buttonID, "raw", packageName);
button[i] = (Button) rootView.findViewById(resID);
registerForContextMenu(button[i]);
button[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound.playSound(audioID);
}
});
}
//functionality for the "+" button
addButton = (Button) rootView.findViewById(R.id.addButton);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CreateDialog newDialog = new CreateDialog();
newDialog.AlertBox(getActivity());
}
});
return rootView;
}
}

Custom Android Menu from an ImageView

I want my Android app to be able to show something like menu button would, but the menu would pop out from an imageview.
You can use PopupWindow:
Activity Class:
public class MainActivity extends Activity {
ImageView mOptionsImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOptionsImage = (ImageView) findViewById(R.id.imageView1);
mOptionsImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(mButton, 0, v.getHeight());
ListView list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// SOME YOUR CODE
popupWindow.dismiss();
}
});
}
});
}
activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="34dp"
android:text="Button" />
</RelativeLayout>
and popupWindow layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
Plese note: you will see no result, until your list is empty, add some buttons, textviews, or fill list with items before test this code.;)
ImageView imV = (ImageView) findViewById(R.id.imV); // if declared in XML
imV.setOnClickListener(imVListener);
View.OnClickListener imVListener = new View.OnClickListener() {
public void onClick(View v) {
//you can use QuickAction menu to pop up
}
};
And the link to QuickAction menu.

Add onclick at more then one object

I create a button for each name in my database, then i should add at this button a onclick function, but i don't know what id use. i use ??? in the code for indicate the position
public class Game extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
ListView playersList=(ListView)findViewById(R.id.playersList);
MyDb db=new MyDb(getApplicationContext());
db.open();
Cursor c=db.fetchAllUsers();
startManagingCursor(c);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(
this,
R.layout.user,
c,
new String[]{MyDb.UsersMetaData.USERS_NAME},
new int[]{R.id.namePlayer}
);
playersList.setAdapter(adapter);
db.close();
Button newUser;
newUser = (Button)findViewById(R.id.buttonNew);
newUser.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MyDb db=new MyDb(getApplicationContext());
db.open();
EditText inserted = (EditText)findViewById(R.id.editName);
String nome = inserted.getText().toString();
db.insertUser(nome);
db.close();
}
});
Button chosen;
chosen = (Button)findViewById(R.id.???);
chosen.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Game2.class);
startActivityForResult(myIntent, 0);
finish();
}
});
}
}
user interface
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/sfondo"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#drawable/sfondo">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<EditText
android:id="#+id/editName"
android:text="#string/editName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:lines="1"
android:width="180px" />
<Button
android:text="#string/newPlayer"
android:id="#+id/buttonNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="70px" />
</LinearLayout>
<ListView
android:id="#+id/playersList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</FrameLayout>
xml for the db elements
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical"
>
<Button
android:id="#+id/namePlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:minWidth="250px" />
</LinearLayout>
thanks
So the button is in R.layout.user? If so I think you may need to extend SimpleCursorAdapter and in the getView() method you can find the view and add the listener. Something like this:
public class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(...) {
super(...);
}
public void getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Button button = (Button) view.findViewById(R.id.???);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
}
}
That way your 'find' is scoped to one row in the list. You can't add the listener where you have the code currently because there will be multiple Views with the same ID.
to define onClick to more than one object , your class should implement the interface OnClickListener , and override the method onClick(View v) , like this :
public class MyActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstance){
....
//add listeners to your buttons
btn1.setOnClikListener(this);
btn2.setOnClickListener(this);
....
}
#Override
public void onClick(View v ) {
if(v==btn1)
// treatement 1
if(v== btn2)
//treatment 2
...etc
}

Categories

Resources