I can't open a new Intent by a button but only in listview I followed a guide but it solved it listview and I need a normal click where is the problem in my code
The code only works with the listview...
buttonadd.setOnClickListener(new AdapterView.OnClickListener(){
#Override
public void onClick(AdapterView<?> parent, View view, int postition, long id){
String curentgruopname = parent.getItemAtPosition(postition).toString();
Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
groupchatintent.putExtra("groupname", curentgruopname);
startActivity(groupchatintent);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postition, long id) {
String curentgruopname = parent.getItemAtPosition(postition).toString();
Intent groupchatintent = new Intent(MainActivity.this , buttonaddactivity.class);
groupchatintent.putExtra("groupname", curentgruopname);
startActivity(groupchatintent);
}
});
Maybe it works when you initialize the OnClicklistener in the OnCreate Method.
Here is an Example:
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
button = findViewById(R.id.button);
button.setOnClickListener(buttonClick);
}
View.OnClickListener buttonClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(testActivity.this, nextActivity.class);
startActivity(intent);
}
};
Related
Been trying to add a favorites system to this notes app where I can tap and hold an item in the list view to add it to another activity with a list view. Here is the activity with the first list.
Items are added via the MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.savebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText editTextHeading = (EditText) findViewById(R.id.editTextTextPersonName);
EditText editTextContent = (EditText) findViewById(R.id.contentfield);
String heading = editTextHeading.getText().toString().trim();
String content = editTextContent.getText().toString().trim();
if (!heading.isEmpty()) {
if(!content.isEmpty()) {
try {
FileOutputStream fileOutputStream = openFileOutput(heading + ".txt", Context.MODE_PRIVATE); //heading will be the filename
fileOutputStream.write(content.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else {
editTextContent.setError("Content can't be empty!");
}
}else{
editTextHeading.setError("Heading can't be empty!");
}
editTextContent.setText("");
editTextHeading.setText("");
}
});
Button button2 = (Button) findViewById(R.id.btn_gotosaved);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, saved.class));
}
});
Button button3 = (Button) findViewById(R.id.btn_faves);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, favorites.class));
}
});
}
}
Items added will be viewed here
public class saved extends MainActivity {
public static final String EXTRA_MESSAGE = "com.example.notes.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved);
File files = getFilesDir();
String[] array = files.list();
ArrayList<String> arrayList = new ArrayList<>();
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
for (String filename : array) {
filename = filename.replace(".txt", "");
System.out.println(filename);
adapter.add(filename);
}
final ListView listView = (ListView) findViewById(R.id.lv_saved);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = listView.getItemAtPosition(position).toString();
Intent intent = new Intent(getApplicationContext(), Note.class);
intent.putExtra(EXTRA_MESSAGE, item);
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
String item = listView.getItemAtPosition(position).toString();
}
});
}
}
And tapping and holding an item from there should "favorite" it and copy it to this new activity with another listview
public class favorites extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorites);
ListView listView = (ListView) findViewById(R.id.lv_favorites);
}
}
How should I approach this?
With your implementation of creating an individual .txt file in the default directory for each note, this is how you could implement:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
String item = listView.getItemAtPosition(position).toString();
Boolean isItemFavorite = item.contains("_favorite");
if (!isItemFavorite){
File itemFile = new File(item + ".txt");
File favoriteItemFile = new File(item + "_favorite.txt");
itemFile.renameTo(favoriteItemFile);
}
}
});
Then in your "favorites" activity you could access all of your note .txt file the same as you do in your "saved" activity - just filtering out any items that don't contain "_favorite" in your "String[] array = files.list();"
Also, some tips: follow naming convention with your activities. "saved" should at least start with an uppercase letter and really should be named something like "SavedNotesListActivity". Also, you should use a room database to keep track of your notes. You should have a favorites table in your room database to keep track of all of your favorites.
I am trying to pass some data from one activity to another and I have made a toast in the other activity to see if data is being passed.When the toast shows up it is blank.I have done everything right and tried many different times with different methods I have also created another app but the problem still stays.It gives me null.
My java code in the first activity
public class titleandcuisine extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public static final String TITLE_GET = "title";
public static final String CUISINE_GET = "cuisine";
ImageView imageView;
Button button;
Spinner spinner;
EditText dish;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titleandcuisine);
dish = findViewById(R.id.editText);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.cuisines,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
button = findViewById(R.id.next);
imageView = findViewById(R.id.close);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), homepage.class));
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = dish.getText().toString();
String text2 = spinner.getSelectedItem().toString();
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(TITLE_GET,text);
data.putExtra(CUISINE_GET,text2);
startActivity(data);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Second activity java code
public class reviewactivity extends AppCompatActivity {
TextView cuisine,title;
ImageView displayimage,back;
Uri myUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reviewactivity);
Intent intent = getIntent();
String titleget = intent.getStringExtra(titleandcuisine.TITLE_GET);
String cuisineget = intent.getStringExtra(titleandcuisine.CUISINE_GET);
Toast.makeText(this, titleget + cuisineget, Toast.LENGTH_SHORT).show();
cuisine = findViewById(R.id.reviewcuisine);
title = findViewById(R.id.reviewtitle);
back = findViewById(R.id.backtopostvideo);
displayimage = findViewById(R.id.reviewdisplaimage);
// cuisine.setText(cuisineget);
// title.setText(titleget);
// myUri = Uri.parse(uri);
displayimage.setImageURI(myUri);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),videopost.class));
}
});
}
}
You need to read the text from the EditText when the button is clicked. Something like:
title = dish.getText().toString(); // Add this
Intent data = new Intent(getBaseContext(),imagepost.class);
data.putExtra(Intent.EXTRA_TEXT, title);
...
One more thing: You are trying to read the Intent.EXTRA_TEXT at the reviewactivity activity. However, nothing is starting that activity (at least within the piece of code that you shared).
I have made a list of titles but not attached the numbers to be given according to the titles.
I want to give the numbers and made call on them according to the user will call on select.
I am making a emergency number app and want to give the numbers with the titles in app, when the user will select a number according to need he will call ..
please guide me
This is my code:
MainActivity.class
public class MainActivity extends Activity {
static final int PICK_CONTACT_REQUEST = 1; // The request code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button EmergencyBtn = (Button) findViewById(R.id.button);
EmergencyBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent emerIntent = new Intent(MainActivity.this, EmergencyContacts.class);
startActivity(emerIntent);
}
});
EmergencyContacts.class
public class EmergencyContacts extends ListActivity {
static final String[] EmergencyNumbers = new String[]{
"Ambulance","Hilal-e-Ahmar","Edhi Trust","Bomb Disposal","Board of Secondary Education",
"Chambers of Commerce & Industry","Civil Defence","Civil Secretariat","Export Promotion ",
"Bureau","Fatmid Blood ","Transfusion ","Fire Brigade Center","General post office(GPO)",
"Govt. transport(GTS)","Hospital Civil(casualties)","Hospital services (Casualties)",
"Income Tax","Metropolitan corp.","News Agency(APP)","Police Emergency","Railway Station (City)",
"PIA Flight Enquiry","PIA Reservation","PIA Cargo","Passport Office","PTV ","Pakistan Tourism Dev. Corp.",
"PAF (Recrut)","Pakistan Army (Recruiting)","Pakistan Navy (Recruiting)","Radio Pakistan","Railway Enquiry",
"Railway Reservation (Cantt.)","Railway Reservation (city)","Sui Gas Complaints","Time Enquiry",
"Telephone Enquiry","Telephone Complaints","Trunk Overseas ","Booking","Trunk inland Enquiry","Phonogram",
"Overseas Booking","Overseas Enquiry","Telegraph Enquiry","Text Book Board","University","University Allama Iqbal",
"University of Engin and Tech","Weather (Enq)","Wapda (Enq)","PAKISTAN TOURISM DEV. CORP","PAKISTAN ARMY (RECRUITING)",
"PAKISTAN NAVY (RECRUITING)"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,EmergencyNumbers));
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView l, View v,int position,long id){
//TODO Auto generated method stub
super.onListItemClick(l,v,position,id);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Call to?"+"\n"+getListView().getItemAtPosition(position))
.setCancelable(false)
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// EmergencyContacts.this.finish();
Intent callTo = new Intent(EmergencyContacts.this,CallTo.class);
startActivity(callTo);
}
});
AlertDialog alert = builder.create();
alert.show();
}
callTo.class
public class CallTo extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_to);
Button button = (Button) findViewById(R.id.button6);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_DIAL);
String p = "tel:" + getString(R.string.phone_number);
i.setData(Uri.parse(p));
startActivity(i);
}
});
}
Use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call_to);
Button button = (Button) findViewById(R.id.button6);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
String number = getString(R.string.phone_number);
intent.setData(Uri.parse("tel:" + number));
startActivity(
Intent.createChooser(intent, "Choose a Call client :"));
}
});
}
Happy to help
I'm hoping this will be a fairly simple thing. In my parse database I have an column called seconds. I do not know the proper syntax to pull it as a Int, I can only pull it as a string. Is there a good way to do this? I've looked into conversion methods but none of them address actually taking an int from a position in an adapter. Here is my code. Alternatively if someone can tell me how to convert that string afterwards in a new activity that would be great.
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(Welcome.this,
MainActivity.class);
// Pass data "name" followed by the position
i.putExtra("Name", ob.get(position).getString("Name")
.toString());
i.putExtra("Time", ob.get(position).getString("Time")
.toString());
i.putExtra("Seconds", ob.get(position).getInt("Seconds")
);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Second Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textViewTime = (TextView)findViewById(R.id.textViewTime);
Timer = i.getStringExtra("Timer");
textViewTime.setText(Timer);
Name = i.getStringExtra("Name");
Seconds = i.getIntExtra("Seconds");
final CounterClass timer = new CounterClass(180000,1000);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.start();
}
});
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
You should use "getInt("Seconds")" without "toInt()".
I've been scouring youtube for hours and I cant seem to find one that works. So far I have this in my code but it wont let me start a new activity.
public class MainActivity extends ActionBarActivity {
ArrayList<PerMoney> PeepList = new ArrayList<PerMoney>();
int pos = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView people = (ListView)findViewById(R.id.peopleList);
setContentView(R.layout.activity_main);
if(PeepList.size() != 0)
UpdateList();
people.setOnItemClickListener(onListClick);
}
and then here is the onitemclick part
private AdapterView.OnClickListener onListClick=new AdapterView.OnItemClickListener()
{
ListView people = (ListView) findViewById(R.id.peopleList);
int res;
people.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
pos = position;
Intent intent = new Intent(this,SecondActivity.class);
startActivityForResult(intent, 0);
}
};
}
Shouldn't it be:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);