i have many buttons in main activity that each button link to another activity that contain listview that each item in listview link one html file that is in asset folder.
in my codes i'm using one webview activity for each activity.
how can i use one same webview activity for all activity.
i have eight different activities with different html files.
and i try to use one webview for others and always result is one html for all address.
one of my activities:
public class Dia_Page extends ListActivity {
public ListView lv;
public String number_of_keys;
public String[] values = new String[] {"dia 1",
"dia 2","dia 3"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dia__page);
setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1,values));
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
Intent myIntent = new Intent(Dia_Page.this,Dia_sec.class);
myIntent.putExtra("key",position);
startActivity(myIntent);
}
});
}
my webview activity:
web2.setWebViewClient(new myWebClient());
web2.getSettings().setJavaScriptEnabled(true);
int pos = getIntent().getIntExtra("key", 0);
if (pos == 0) {
web2.loadUrl("file:///android_asset/dia/diaone.html");
} else if (pos == 1) {
web2.loadUrl("file:///android_asset/dia/diatwo.html");
} else if (pos == 2) {
web2.loadUrl("file:///android_asset/dia/diathree.html");
}
how can i use same webview for show all activities html that are in different activities.
I'm posting this answer to my question to those friends that have a problem like mine.
in each activity that contains listview i put this codes:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), Dia_sec.class);
switch(position){
case 0 :
intent.setData(Uri.parse("file:///android_asset/can/canone.html"));
break;
case 1 :
intent.setData(Uri.parse("file:///android_asset/can/cantwo.html"));
break;
case 2 :
intent.setData(Uri.parse("file:///android_asset/can/canthree.html"));
break;
// etc
}
startActivity(intent);
and in my webview "dia_sec" i add this code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dia_sec);
web2.loadUrl(getIntent().getDataString());
I had about 8 different activities that contains their own listview with different html files.
and now i put code in activity and link them to my webview page.
problem solved and now i have one webview that i use it to show all html files from different source and different listviews.
Thanks alot every body to try help me in this way
Try changing the code in webview activity like this.
`
String pos="";
in = getIntent();
web2.setWebViewClient(new myWebClient());
web2.getSettings().setJavaScriptEnabled(true);
pos= in.getStringExtra("key");
if (pos.equals("0")) {
web2.loadUrl("file:///android_asset/dia/diaone.html");
} else if (pos.equals("1")) {
web2.loadUrl("file:///android_asset/dia/diatwo.html");
} else if (pos.equals("2")) {
web2.loadUrl("file:///android_asset/dia/diathree.html");
}`
Related
I'm very new to developing android apps and my programing experience is also lacking a bit.
But I hope some of you can still help me.
Let's get to my problem.
I created a list with images. If I click on one item, I get a Toast saying "you clicked xy". But I want to include a master detail flow. I looked up the internet for some help but all I could find were tutorials on how to integrate a website...
This is my onCreate method, I hope someone could help me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
}
});
There are two approaches - using fragments or using activities. Fragments are the preferred way, but harder. Seeing as you are new to android you should probably learn how to do it with activities first. Before you can do that you need to learn about intents.
What you need to do is use the intent to start the detail activity when your item is selected.
Master activity
protected void onCreate(Bundle savedInstanceState) {
...
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//you should make the key string a constant in your code
//im assuming for this example your item is something like a drawable resource id
intent.putExtra("ImageId", getItem(position));
startActivity(intent);
}
});
Detail activity
protected void onCreate(Bundle savedInstanceState) {
...
ImageView iv = (ImageView) findViewById(...);
int imageRes = getIntent().getIntExtra("ImageId", -1);
if (imgRes > 0){
iv.setImageResource(imageRes);
}
}
There is a AutoCompleteTextView and when user write for-example blue and select it from AutoCompleteTextView, the current page change to that particular page.
Here is the code:
public class MainActivity extends Activity {
private AutoCompleteTextView et_search_game;
private TextView TextViewForAutoComplete;
private static String[] GAMES_NAME = new String[] { "blue", "red", "black" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//AutoCompleteTextView Setting.
et_search_game = (AutoCompleteTextView)findViewById(R.id.actv_search_game_name_xml);
//TextView Setting.
TextViewForAutoComplete = (TextView)findViewById(R.id.tv_for_auto_complete_tv_xml);
ArrayAdapter<String> arrayAdapterForGamesName = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, GAMES_NAME);
et_search_game.setThreshold(1);
et_search_game.setAdapter(arrayAdapterForGamesName);
et_search_game.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
// TODO Auto-generated method stub
if(et_search_game.getText().toString().trim()=="blue"){
setContentView(R.xml.blue);
} else if (et_search_game.getText().toString().trim()=="red"){
setContentView(R.xml.red);
} else if (et_search_game.getText().toString().trim()=="black") {
setContentView(R.xml.black);
}
}
});
}
The red,black,blue pages are normal XML files in directory named 'xml'.
After running the code when i write b, the black row display and i click on it but nothing happen.
I don't want to go with activity(intent) method.
thanks.
Instead of == use .equals
Instead of taking value from "et_search_game.getText().toString().trim()" you can take from adapter and position "i"
It is always a best practise to place layout xmls in "layout" directory
I have got two activities.
My goal is: After longClick on any ListView position in activity 2, I want some String to be added to ListView in activity 1. From activity 2 I'm going back to activity 1 by pressing back button. Each ListView has got different adapter.
I tried with Bundle (put extra), but it makes app crash.
Fragment of code from activity 1:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_layout);
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() { //It adds text ect. to listView - It works so I cut out the rest of code
ar.add("anything");
ar.add(lv.try);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ar);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
});
}
Simply adding like ar.add("anything"); works. The lv.try contains by default text "First text". I tried to update this variable in activity 2 onItemLongClick fragment of code:
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//What to type here?
return false;
}
});
Putting simply try = "Second text"; will not update my try variable. I tried also with Bundle (extras) but without success. Any ideas?
I don't know if I understood the question. Anyway, have you tried to override onBackPressed?
In the second activity you create a boolean variabile and inizialize it as false, then change its value when you perform a long click:
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClick = true;
return false;
}
});
Remember, longClick has to be a class variable!
Outside onCreate:
#Override
public void onBackPressed() {
Intent intent = new Intent(SecondActivityName.this, FirstActivity.class);
intent.putExtra("longClick", longClick);
finish();
startActivity(setIntent);
}
And in the first Activity:
Intent intent = getIntent();
if (intent.getBooleanExtra("longClick", false)) {
addDrawerItems();
}
What does add position mean ?
In my opinion add position means add one element to a structure (array, list, etc).
So basically what I mean is that you can have an array in your CustomApplicatioClass or even a Static array(someone probably would not agree with me) and onItemLongClick() add the element you want in the structure.
Then when you load the Activity with the ListView you can do your ListView stuff using the updated structure
hope it helps
Hey guys I'm working on an app in android studio. I have a listview and when I make a selection i would like to add that selection to another listview in a different activity. What is the easiest/best way to do this? I've tried putExtra without any luck. Any examples or ideas would be great. Thank you guys.
Thanks for the examples everyone they've helped me understand a lot better the intent system. I've been trying the different examples everyone has posted and I've kind of gotten stuck. The goal is simply to have the items I select from the listview in the Walmart.java file to show up in the listview in GiftsSelected.java I have another place to open the activity so I don't need it to immediately open the new activity.
Here is my code:
This is Walmart.java
public class Walmart extends ActionBarActivity {
private String[]giftarray = {
"Apple" ,
"Bananas",
"Bed",
"Beef",
"Bottle",
"Bread",
"Broccoli",
"Carrots",
"Cat",
"Chicken",
"Chocolate",
"Computer",
"Cow",
"Crow",
"Dog",
"Dolphin",
"Dove",
"Drawer",
"Egg",
"Fish",
"Fork",
"Fridge",
"Giraffe",
};
Intent a = new Intent(Walmart.this,GiftsSelected.class);
private ListView giftListView;
private ArrayAdapter arrayAdapter;
ArrayList<String> list = new ArrayList<String>();
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){}
public boolean onContextItemSelected(MenuItem item){
return true;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_walmart);
getSupportActionBar().hide();
giftListView = (ListView) findViewById(R.id.gift_list1);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, giftarray );
giftListView.setAdapter(arrayAdapter);
giftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = "Item added to registry";
list.add(item2);
a.putStringArrayListExtra("list",list);
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
This is my GiftsSelected.java code:
public class GiftsSelected extends ActionBarActivity {
private ListView giftListView;
private ArrayAdapter arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> list = new ArrayList<String>();
//This makes my app crash which makes me think I did this wrong...
list = getIntent().getStringArrayListExtra("list");
String[] giftarray = new String[list.size()];
list.toArray(giftarray);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gifts_selected);
getSupportActionBar().hide();
giftListView = (ListView) findViewById(R.id.gift_list1);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_gallery_item, giftarray);
giftListView.setAdapter(arrayAdapter);
giftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = "Item added to your registry";
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
this is the list that i use
ArrayList<String> list = new ArrayList<String>();
add elements in it like this
list.add("something");
in first Activity
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
i.putStringArrayListExtra("list",list);
startActivity(i);
in the second activity in the onCreate
list = getIntent().getStringArrayListExtra("list");
Let's say the first Activity is X and X holds a listview that updates another listview in Activity Y.
If X is tightly related to Y, that is to say Y launches X, gets data then immediately returns to Y, then you should use startActivityForResult from Y.
class ActivityY {
public static final int REQUEST_CODE = 2;
...
Intent i = new Intent(this, ActivityX.class);
startActivityForResult(i, REQUEST_CODE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String returndata = data.getStringExtra("rowdata");
//update your listView, do notifyDataSetChanged() etc;
}
}
}
}
class ActivityX {
// in listview onItemClickListener or elsewhere that listens to row click
Intent intent = new Intent();
returnIntent.putExtra("rowdata", rowdata); // whatever data you need to transfer
setResult(RESULT_OK,intent);
finish();
}
If X and Y are loosely related, that is to say X is not necessarily launched from Y, but goes to Y then you should just use the usual startActivity(intent).
if X and Y are completely independent, that is to say X is not necessarily launched from Y, may not go to Y or wanders other Activities before arriving in Y then you should cache the data. If the data size is small then the best way is to store it in Preferences. When you Y Activity starts, get the data, update your ListView, then if needed remove the cached data from Preferences.
a)You can make the listview item object parcelable and send it between activities via intent extra.
b)You can save the selected listview item in a global variable.(Not recommended)
I've been able to pass a selected image from grid view to a new full screen activity. I am now trying to capture the EXIF data from the image and pass it into a new activity.
The first activity of passing the int from grid view seems to be working fine.
public class test extends Activity {
public static int pos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thumb);
GridView gridview = (GridView) findViewById(R.id.thumbgridview);
gridview.setAdapter(new tImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(test.this,test2.class);
pos=position;
intent.putExtra("pos", pos);
startActivity(intent);
finish();
}
});}
}
The second activity which displays the full image seems to be working fine.
public class test2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full);
Bundle bundle= getIntent().getExtras();
ImageView image = (ImageView) findViewById(R.id.imagefull);
int pos = bundle.getInt("pos");
bundle.getFloat(ExifInterface.TAG_MAKE);
tImageAdapter obj = new tImageAdapter(this);
image.setImageResource(obj.tThumbIds[pos]);
Button bDIR = (Button) findViewById(R.id.bDIR);
bDIR.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(test2.this,Direct.class);
intent.putExtra(ExifInterface.TAG_MAKE, 0);
startActivity(intent);
finish();
}
});
Now when I proceed to the final activity all I am seeing in the text view is the word Make.
public class Direct extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newthumb);
Bundle bundle= getIntent().getExtras();
bundle.getFloat(ExifInterface.TAG_MAKE);
TextView textview = (TextView) findViewById(R.id.dirtext);
textview.setText(ExifInterface.TAG_MAKE);
}
}
I am not getting any errors in debug and there hasn't been a single force close issue. Is there something I am missing? I've only been working with java for a couple weeks but this type of activity seems like it should be doable. (or I'm just an idiot)
Thanks!
bundle.getFloat(ExifInterface.TAG_MAKE); does not read anything. You are nowhere actually reading the Exif data from the image file. You simply are showing in the TextView the content of the static String named ExifInterface.TAG_MAKE.
The documentation is available: ExifInterface. You will need to do something like:
ExifInterface exifReader = new ExifInterface(filename);
textview.setText(exifReader.getAttribute(ExifInterface.TAG_MAKE));