How to save add button dynamically in android? - android

The code should add a button dynamically when the user needs it.
When an users click on addService this code takes (a name for button and a value to use in the intent for this button) from a second Activity, then adds the button dynamically in this Activity with the name and intent and the user can click it for service.
How to save the button dynamically added by the user?
Button btn = new Button(MainActivity.this);
btn.setText(buttonName);
layout.addView(btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String phone = serviceNum;
Intent e = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(e);
}
});

Include the save button in your layout, then hide it before the view loads. When the user performs the action, simply unhide the button. The layout should handle it automatically for you.

Related

i wanted to start a new activity after pressing the three buttons ...please let me know how can i achieve this?

Hi i am creating a simple educational game in which user has to press three images in order to proceed to the next level and in next level user has to press 5 images.
i have gone through the on click but i using onIntent intent = new Intent(this.getApplicationContext(), Activity.class);
this.startActivity(intent, 0); i am only able to start new activity on single button pressed but i wanted to start new activity when user has finished pressing three image Buttons.
Thanks in advanced.
you can use global int variable and increase it every button click and if its more than your button number open new activity
public int btn = 0;
MyButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(btn >= 2) {
//open your activity
}
else{
btn++;
}
}
});
Based on my understanding of your problem,I have provided a possible simple implementation.
Use a simple data structure like queue or stack. When a image is tapped, add information about the image to the data structure. After adding the information to data structure see if number of items in data structure is equal to 3 ? if yes check data structure has info about the required three images and not just info about the same image (happens if user taps on same image more than once). If condition is met then call startActivity(). Generalize this so that you can reuse the logic in different activities, irrespective of the number of images.
Hi i finally achieved my desired activity. now with this after checking two check box app will start new activity.
`private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
checkBoxListener =new OnClickListener() {
public void onClick(View v) {
if(chkIos.isChecked()&&chkAndroid.isChecked()) {
Intent i1=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
if(chkAndroid.isChecked()) {
Intent i2=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
}
}
}
};
chkIos.setOnClickListener(checkBoxListener);
chkAndroid.setOnClickListener(checkBoxListener);
}
}
`
thanks for all your answers.

android app displaying image on clicking button

Can someone help me with the code for
-if i click on button it should display a full screen image then again if i click on button on 2nd page it should do the same.
i want to know how to connect different pages/activities,when you click a button it should display some image and then again clickin on that page will display another image and so on,
images should be full screen
In your activity after setting content view use this code to start your image activity.
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(),
ImageActivity.class);
startActivity(intent);
}
});
Now put a ImageSwitcher or Image view and set a touch or click listener to change Images as per your logic.
Let's say you have two activities : MyActivity1 and My Activity2
In MyActivity1 class,create a button and on Button's click listener call MyActivity2.class using startActivity(new Intent(MyActivity1.this,MyActivity.class))
MyActivity2 class will contain one ImageView that will have its width and height as "fillparent" value in xml.
You can continue this process for as many activities you want.

TableLayout, Different Button actions depending on data in TableRow

I have the following problem:
I have a TableLayout along with several TableRows, which are dynamically created.
On the right side of every row I create a button, which should call another activity.
Now I want to pass some information with intent.putExtra(). In this case I want so pass the row number, which is also the first information in the row.
Here is a picture of the current state:
This is how I create the buttons during run-time (in a loop):
Button b1 = new Button (this, null, android.R.attr.buttonStyleSmall);
b1.setId(1000+grButtonId);
b1.setText("Request GR");
b1.setLayoutParams(params);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Some code, taken out for clarity
// See next code snippet
}
});
grButtonId++;
tr.addView(b1);
My idea so far is to use the id of the button (of course), and get the line number by the value of grButtonId.
Now comes my problem, let's have a detailed look at my onClickmethod:
#Override
public void onClick(View view) {
// finished is true, as soon as GRRequest has recieved the data
if(!finished & !dataRequested){
new GRRequest().execute(getIntent().getLongExtra("poNr", 0),(long)view.getId());
b1.setText("Show GR");
Log.d("DataList", detailList.toString());
dataRequested=true;
}
else{
if (dataRequested){
b1.setText("Show GR");
}
Intent intent = new Intent(DataTableCreater.this, GRTableCreater.class);
intent.putExtra("lineNr",view.getId());
intent.putExtra("dataList", detailList);
startActivity(intent);
}
}
When I request my data, the button I clicked on gets set to "Show GR" , as intended. The other buttons stay on "Request GR", this is also fine. But now I want these Buttons to Change to "Show GR" when tapped first and on second tap start the activity.
By now, the buttons change to "Show GR" and directly start the activity.
What would be a solution, to make this work?
Create a boolean Array clickedOnce[] = new boolean[grButtonId+1] one field for every Button.
Then have this
public void onClick(View view) {
if(!finished){
new GRRequest().execute(getIntent().getLongExtra("poNr", 0),(long)view.getId());
b1.setText("Show GR");
Log.d("DataList", detailList.toString());
clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]=true; //sets the clickedOnce for this button to true, substring(1,4) is needed to cancle the leading 1 from the id
}
else{
//Checks, if the button was clicked once
if (!clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]){
b1.setText("Show GR");
clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]=true;
}
else{
Intent intent = new Intent(DataTableCreater.this, GRTableCreater.class);
intent.putExtra("lineNr",view.getId());
intent.putExtra("dataList", detailList);
startActivity(intent);
}
}
}

How to create another button in android dynamically

As the title states, I am looking to find out how to create a button dynamically when another button in another activity is pressed. This is being done with the Android SDK.
Basically, I have two activities, MainActivity and SecondaryActivity, within the SecondaryActivity you enter some information, title, text, id, so on and so forth. When you click the "Save" button it sends some, information to MainActivity(not the issue). As well as sending the information, I need to create an entirely new button within the MainActivity.
Any suggestions on how this should be accomplished?
Thanks.
Edit 1
public void CreateNewButton(View view)
{
LinearLayout lineLayout = (LinearLayout)findViewById(R.id.linear_layout);
TextView newTextView = new TextView(this);
int id = rand.nextInt(100);
int newId;
newTextView.setVisibility(View.VISIBLE);
newTextView.setId( R.id.new_button + id );
newTextView.setText("New Item");
newTextView.setTextSize(35);
newTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(getBaseContext(), SecondActivity.class);
startActivity(intent);
}
});
lineLayout.addView(newTextView);
}
This code generates the new TextView( Decided to change it up ) but now the issue I have, is newTextView.setText(); needs to get the text from the other activity
newTextView.setText(i.getData().toString());
putting this in the CreateNewButton(View view) methods causes an error since technically there is no data in the field that it is trying to grab from.
The problem at hand is I need to create the new TextView field WITH the name of the new account that has yet to be created. If that makes any sense.
I'm going to assume you want to add this button to a LinearLayout:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
Button button = new Button(this);
button.setText("I'm a button!");
// add whatever other attributes you want to the button
linearLayout.addView(button);

In Android: How do you show layout twice using intent?

Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView

Categories

Resources