opening new content view - java

In my Main.class I am creating a dynamic ScrollView and starting it using setContentView(sv) it displays a Spinner and a Button.
The user selects an option from the Spinner and then clicks the Button. I have set a setOnClickListener to the Button.
In the onClick method I am trying to start up a new ContentView. Calling a new class file with the same type of layout as Main.class. Not sure how to go about doing this. I added finish() to the onClick method and it closes the original window but not sure how to open the new one.
Here is the Main.class code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.messages = new Messages(this);
this.datasource = new FacilitiesDataSource(this);
this.datasource.open();
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
Spinner s = new Spinner(this);
s.setId(SPINNER_FACILITIES);
s.setLayoutParams(new Spinner.LayoutParams(-2,-1));
final List<SpinnerObject> list = this.datasource.getFacilitiesList();
final ArrayAdapter<SpinnerObject> adapter = new ArrayAdapter<SpinnerObject>(this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
ll.addView(s);
Button b = new Button(this);
b.setText("Submit");
b.setLayoutParams(new LayoutParams(-2,-1));
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Spinner spin = (Spinner)findViewById(SPINNER_FACILITIES);
Log.v("option picked", Integer.toString(( (SpinnerObject) spin.getSelectedItem () ).getId () ));
/*
* NEED HELP HERE
* NEED HELP HERE
* NEED HELP HERE
*/
//finish();
}
});
ll.addView(b);
setContentView(sv);
}
Here is my Vehicles.class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("Started onCreate", "Vehicle");
ScrollView sv = new ScrollView(this);
setContentView(sv);
}

I assume you mean you want to start a new Activity (from Main to Vehicles). In that case you can use the following code:
Intent intent = new Intent(Main.this, Vehicle.class);
startActivity(intent);
Make sure all Activity classes are declared in your Android Manifest file, inside the <application> tag, like this:
<activity android:name=".Vehicles" />

Related

Adding items in ListView

I have problems with my list . I had an Activity I get some Data from.
The data must accessed in variables in my class :
Mylist.java class
public String Job,Ship,Location,Shift,Date;
public String workingHour;
Mylist(String job,String loc,String shp,String shft,String dat,String wh){
Job=job;
Location=loc;
Ship=shp;
Shift=shft;
Date=dat;
workingHour=wh;
}
and by pressing Save Button The list must add an item.My problem is when I add another items the list have only one item How to fix this ?
Adding Class :
AddOredit.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_oredit);
final ArrayList<Mylist>list =new ArrayList<>();
final ArrayList<String> Job=new ArrayList<>();
String selecteditem1, selecteditem2,getlocation,getship,getdate,gethours;
Spinner type=(Spinner)findViewById(R.id.s1);
Spinner shift=(Spinner)findViewById(R.id.s2);
EditText location=(EditText)findViewById(R.id.loc);
EditText ship=(EditText)findViewById(R.id.ship);
EditText date=(EditText)findViewById(R.id.date);
EditText workinghours=(EditText)findViewById(R.id.hours);
Button Save = (Button) findViewById(R.id.editbtn);
Button cancel = (Button) findViewById(R.id.cancelbtn);
selecteditem1 = type.getSelectedItem().toString(); //for first spinner
selecteditem2 = shift.getSelectedItem().toString();//for second spinner
getlocation=location.getText().toString();
getship=ship.getText().toString();
getdate=date.getText().toString();
gethours=workinghours.getText().toString();
list.add(new Mylist(selecteditem1,getlocation,getship,selecteditem2,getdate,gethours));
Log.e(TAG, String.valueOf(list.size()));
for(int i=0;i<list.size();i++){
Job.add("Job "+(i+1));
}
//When pressing save Button
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//list.add(new Mylist(selecteditem1, getlocation, getship, selecteditem2, getdate, gethours));
Intent intent = new Intent(AddOredit.this, MainActivity.class);
intent.putStringArrayListExtra("MyList", Job);
startActivity(intent);
}
});
}
MainActivity.java this class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv= (ListView) findViewById(R.id.listView);
Button ADD=(Button)findViewById(R.id.addbtn);
Button DELETE=(Button)findViewById(R.id.deletebtn);
Button PROFILE=(Button)findViewById(R.id.profilebtn);
Intent intent=getIntent();
ArrayList<String> Job ;
Job=intent.getStringArrayListExtra("MyList");
if(Job!=null){
ListAdapter adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Job);
lv.setAdapter(adapter);
//when user click on add button
}
ADD.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//go to AddOreditpage
Intent i = new Intent(MainActivity.this, AddOredit.class);
startActivity(i);
}
});
**Note :- ** Add Button is for Adding items in the list and Save Button is for saving data entered at AddOredit class.
**Note :- ** By clicking Add Button , getting to the AddOredit Activity .
By Clicking Save Button, getting back to the main activity with the item added
You are assigning a new List for list object on your second activity created. Try to assign the list with an existing object and add your new object.
EDIT:
Try to Declare this globally as,
private static ArrayList list =new ArrayList<>();
and with in onCreate() remove it. Let me know if it still do not work.

clickable image button in java without xml in android

I want to make image button in java class without using XML(dynamic UI).
I can make this but I can't connect to onClick void.
Can anybody help me?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LayoutParams params =
new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TableRow table = new TableRow(this);
table.setOrientation(TableRow.VERTICAL);
ImageButton ib = new ImageButton(this);
ib.setImageResource(R.drawable.one);
ib.setLayoutParams(params);
table.addView(ib);
TableRow.LayoutParams layoutParams=
new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT );
this.addContentView(table, layoutParams);
}
public void onClick(View v){
Toast.makeText(getBaseContext(),"This button clicked",Toast.LENGTH_SHORT).show();
}
you missed ib.setOnClickListener(...);. If your activity implements View.OnClickListener then it would be
ib.setOnClickListener(this);
You need to assign OnClickListener before using it
If you are implementing OnClickListener in the activity then use (I believe you are because you have onClick method defined)
ib.setOnClickListener(this);
If not then do this
ib.setOnClickListener(new View.OnClickListener()
{
public void onClick(){
// Do as required
}
});

How to access views by id from tabHost?

I have tabHost widget with dynamic created tabs.
In each of newly created tab, there is relative layout with some views (textView, listView etc).
My question is: how to access to these views and how to append new content there? I tried with setting ids or tags, but i didn't work.
public class Main extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("buttontab");
spec.setContent(R.id.form);
spec.setIndicator("Search-form");
tabs.addTab(spec);
Button btn=(Button)tabs.getCurrentView().findViewById(R.id.buttontab);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag)
{
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(Main.this);
// Defining the RelativeLayout layout parameters.
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView t = new TextView(Main.this);
// t.setId(12); //This dosen't work
t.setText("Searching in progress. Please wait...");
ListView resultsList = new ListView(Main.this);
//Here: array adapter etc
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(t);
relativeLayout.addView(resultsList);
return relativeLayout;
}
});
spec.setIndicator("Tab with results");
tabs.addTab(spec);
new GetData(Main.this).execute();
}
});
}
public void appendResultsToViews(String result)
{
//Here i need to access TextView from tab with specific tag and append results to TextView, which was created dynamically earlier
}
Thank you for your help.
set tag of child view and use findViewWithTag(tag)

Android: Create EditText on Runtime

I'm trying to create a view where the user can click a "plus" button, and have additional EditTexts be created. The goal is to have a base of 2 EditTexts, and each time the user clicks the button, add another 2 EditTexts.
How can I do this? I can add EditTexts from Java, but I can't figure out how to add and handle a list of them dynamically.
I was hoping to take however many pairs of EditTexts, and push it into a key/value HashMap or something.
Any ideas of how to do this? Thanks!
public class MyActivity extends Activity {
private LinearLayout main;
private int id = 0;
private List<EditText> editTexts = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
Button addButton = new Button(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addEditText();
}
});
Button submit = new Button(this);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for (EditText editText : editTexts) {
editText.getText().toString();
// whatever u want to do with the strings
}
}
});
main.addView(addButton);
main.addView(submit);
setContentView(main);
}
private void addEditText() {
LinearLayout editTextLayout = new LinearLayout(this);
editTextLayout.setOrientation(LinearLayout.VERTICAL);
main.addView(editTextLayout);
EditText editText1 = new EditText(this);
editText1.setId(id++);
editTextLayout.addView(editText1);
editTexts.add(editText1);
EditText editText2 = new EditText(this);
editText2.setId(id++);
editTextLayout.addView(editText2);
editTexts.add(editText2);
}
Do it in a ListView.
Then you can just add them to a ListAdapter.
And then use adapter.notifyDatasetChanged()
May be I am not clear but Instead of adding Individual edit text you can add as Group View like Linear layout here you can use any flag values to add dynamic name conversions also.
That view you can update into List View like inflating rows in the List View....

Creating a spinner dynamically

I was struggling to create a spinner dynamically. I had it on the page but every time I tried to select an option it would blow up. My original code is at the bottom. I fixed it by moving the addSpinner() function outside of the inner class and changing
Spinner newSpinner = new Spinner(getApplicationContext());
to
Spinner newSpinner = new Spinner(this);
It's fixed but I have no idea what it didn't work initially. Can anyone explain? (apologies if it's a noob question - I am new to both Java and android)
public class SpotManageActivity extends Activity
{
private SimpleCursorAdapter mSpots;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_manage_activity);
Button add_new_button = (Button) findViewById(R.id.add_new_spot_button);
add_new_button.setOnClickListener(new ButtonOnClickListener());
}
public class ButtonOnClickListener implements View.OnClickListener
{
#Override
public void onClick(View v)
{
addSpinner();
}
private void addSpinner()
{
LinearLayout layoutHolder =
(LinearLayout) findViewById(R.id.layout_holder);
LinearLayout spinnerHolder = new LinearLayout(getApplicationContext());
spinnerHolder.setOrientation(LinearLayout.HORIZONTAL);
spinnerHolder.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
Spinner newSpinner = new Spinner(getApplicationContext());
newSpinner.setLayoutParams(
new Spinner.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
newSpinner.setAdapter(mSpots);
layoutHolder.addView(spinnerHolder);
spinnerHolder.addView(newSpinner);
// A non-internal spot was selected
}
}
}
I am not sure at all, but if in the stack trace you're getting something about a wrong context, it's probably because a Spinner, when clicked, opens a Dialog, and a Dialog needs an Activity context.
For more info:
Android - what's the difference between the various methods to get a Context?
Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

Categories