Settings button are not available with listview - java

Where is setting button (3 small points in right corner) ? And how to return it?
When I started to use ListView with another layout , my action bar dissapear.
May be reason is using another layout?
MainActivity.java
public class MainActivity extends ListActivity {
String[] values = new String[100];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.listView);
for (int i=0;i<=99;i++){
values[i]=Integer.toString(i+1);
}
MyCustomAdapter adapter = new MyCustomAdapter(this, values);
setListAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this,SettingActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_SHORT).show();
}
}

To use the default ActionBar:
Ensure your app's theme (in styles.xml has a Theme.AppCompat.* parent which also contains an ActionBar. For example:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/specify_action_bar_color_here</item>
<item name="colorPrimaryDark">#color/specify_status_bar_color_here</item>
<item name="colorAccent">#color/specify_widget_color_here</item>
</style>
Then, ensure your Activity class extends AppCompatActivity, rather than simply Activity. For example:
public class MainActivity extends AppCompatActivity {
//...
}

Related

How to pass String from One Activity to another Activity and use it in intent for Dialpad and Email

I have two one Adapter and another Activity . Adapter send the String Extra as per position in Firebase Data Structure into Next activity where data is displayed which is passed from Adapter.
It works pretty well. I am ablet to show the data in Textview. But When I user intent to Dial a phone or send Email , Then I'm not able to use Extras I"m receivng with but when I setText in Textview ..they show the exact Data. Please help
Here is the method in Adapter
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
database = FirebaseDatabase.getInstance();
dbreference = database.getReference("gender");
g = bookslist.get(position);
holder.teacher_quali.setText(g.getBqualifications());
holder.profile_details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), gender_details.class);
intent.putExtra(NEAR_LOCATION, g.getBlocation());
intent.putExtra(AVAILAIBILITY, g.getBavailaile());
intent.putExtra(MOBILE, g.getSellermobile());
intent.putExtra(EMAIL, g.getSelleremail());
v.getContext().startActivity(intent);
}
});
where I have defined MOBILE and EMAIL as
public static final String MOBILE = "other_mobile";
public static final String EMAIL= "other_email";
in same adapter view and my activity is
public class gender_details extends AppCompatActivity {
private TextView tutor_email,tutor_mobile;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details); // get the reference of Toolbar
toolbar.setTitle(getIntent().getStringExtra(KEY_NAME));
toolbar.setLogo(R.drawable.ic_person_black_24dp);
setSupportActionBar(toolbar);
String tutor_email_txt = "";
String tutor_mobile_txt = "";
tutor_email_txt = intent.getStringExtra(EMAIL);
tutor_mobile_txt = intent.getStringExtra(MOBILE);
// Setting values
TextView Email_Txt = (TextView) findViewById(R.id.tutor_email);
Email_Txt.setText(tutor_email_txt);
TextView Contact_Txt = (TextView) findViewById(R.id.tutor_contact);
Contact_Txt.setText(String tutor_mobile_txt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
// Activity's overrided method used to perform click events on menu items
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
// Display menu item's title by using a Toast.
if (id == R.id.action_call) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+tutor_mobile_txt));
startActivity(intent);
return true;
} else if (id == R.id.action_email) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, tutor_email_txt);
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
Intent intent = new Intent(gender_details.this, MainActivity.class);
startActivity(intent);
}
}
As you can see in Textview , information are shown correctl but when I use to Intent Action Dail or send email...I'm not been able to do so.
Please help
Try this:
Declare the variable Globally
public class gender_details extends AppCompatActivity {
private TextView tutor_email,tutor_mobile;
private ImageView img;
String tutor_email_txt;
String tutor_mobile_txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details); // get the reference of Toolbar
toolbar.setTitle(getIntent().getStringExtra(KEY_NAME));
toolbar.setLogo(R.drawable.ic_person_black_24dp);
setSupportActionBar(toolbar);
tutor_email_txt = intent.getStringExtra(EMAIL);
tutor_mobilee_txt = intent.getStringExtra(MOBILE);
// Setting values
TextView Email_Txt = (TextView) findViewById(R.id.tutor_email);
Email_Txt.setText(tutor_email_txt);
TextView Contact_Txt = (TextView) findViewById(R.id.tutor_contact);
Contact_Txt.setText(String tutor_mobile_txt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
// Activity's overrided method used to perform click events on menu items
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
// Display menu item's title by using a Toast.
if (id == R.id.action_call) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+tutor_mobile_txt));
startActivity(intent);
return true;
} else if (id == R.id.action_email) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, tutor_email_txt);
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TEXT, "mail body");
startActivity(Intent.createChooser(intent, ""));
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
Intent intent = new Intent(gender_details.this, MainActivity.class);
startActivity(intent);
}
}
Are You Declare Permission in Manifest If Not Then Declare it :
uses-permission android:name="android.permission.CALL_PHONE"
There are some serious naming problems in your code. First you need to make your tutor_mobile_txt variable global. Secondly inside onCreate your are initializing tutor_mobilee_txt instead of tutor_mobile_txt. Correct the spellings and then check.

error while programming for a button in initial level

I am new in android programming, having error in the given code which are: Multiple markers at this line
-Button1 cannot be resolved to a variable
-Syntax error on token "#", class expected
-id cannot be resolved to a variable
-Line breakpoint:MainActivity [line: 22] - onCreate(Bundle)
public class MainActivity extends Activity {
Button aButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aButton = (Button) this.findViewById(R.id.Button_1);
aButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
aButton.setText("Submitted");
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
1.u must have activity_main and it should include Button with Button_1
2.check .JavaClassName added in Manifest file

Android studio: An error has occured

I'm working on programming a android application. But as soon as I would like to test my application in my simulator, I get an instant error.
This is my code.
public class MainActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button toPech = (Button)findViewById(R.id.toPech);
toPech.setOnClickListener(this);
ImageButton toInfo = (ImageButton)findViewById(R.id.toInfo);
toInfo.setOnClickListener(this);
//this button is not in the same layoutactivity as the other 2.
ImageButton backPech = (ImageButton)findViewById(R.id.backPech);
backPech.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.toPech:
startActivity(new Intent(getApplicationContext(), LocationActivity.class));
break;
case R.id.toInfo:
startActivity(new Intent(getApplicationContext(), InfoActivity.class));
break;
case R.id.backPech:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
break;
}
}
I would like to know what is wrong so that I can.
From your commented line:
//this button is not in the same layoutactivity as the other 2.
I think you have a NullPointerException.
While the Button "backPech" isn't in activity_main.xml, so calling findViewById(R.id.backPech); will return a null object ( backPech will be null).
And calling setOnClickListener(this); to a null object will cause the NullPointerException.

what to do if backpressed

i have a code to change the text of textview in a layout according to the value of the edittext (et) in the previous layout >> somethig like this
I cannot comment, so i will help you editing. Use SharedPreferences to store the string from the edittext in order to get persistance. Then get the string from the SharedPreferences and do the same thing that you are doing(pass it through the intent)
there is MorningDrsGeneral :
public class MorningDrsGeneral extends ActionBarActivity {
Button button ;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morningdrs);
et = (EditText) findViewById(R.id.et);
addListenerOnButton1();
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, bookingKamal.class);
intent.putExtra("fn" , et.getText().toString());
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and there is bookingKamal.java :
public class bookingKamal extends ActionBarActivity {
Button button;
TextView textView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookingkamal);
textView3 = (TextView) findViewById(R.id.textView3);
String A = textView3.getText().toString();
String N = " ";
if (A.equals(N)){
Intent intent = getIntent();
String texx = intent.getStringExtra("fn");
textView3.setText(texx);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
So i have to keep the text in the bookingKamal activity .. it means when i go back from this layout and back to it the text should be the same as previous. And in this code it back to null :/ or
You have two options here:
Save the value into the internal file system so it can still will be loaded after the app has been unloaded
Save the value in the Activity which holds the button to this activity and save the text over there in a variable and when the bookingkamal activity starts, it could be read from somewhere (hint: Intent#putExtra("KEY", "VALUE"))
I am not describing how you should do it because there is enough documentation about it out there and that is not the point of this answer. This should give you an idea how to it.
You can do that by using sharedpreferences.
The above link will help you undderstand how to use it.
To add data to SharedPreferences use
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("text", mSaved.getText().toString());
editor.putInt("selection-start", mSaved.getSelectionStart());
editor.putInt("selection-end", mSaved.getSelectionEnd());
editor.apply();
And to retrieve
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null)
{
//mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
/*if (selectionStart != -1 && selectionEnd != -1)
{
mSaved.setSelection(selectionStart, selectionEnd);
}*/
}
Note : that SharedPreferences will save the value such that, even of you close the application and restart it. The values will still remain. So, make sure you clear the data when you done with it. You can read more about it here at the SharedPreferences docs.

Android action overflow drawn on top of ActionBar

In order to have the ActionBar in my application, my main activity extends from android.support.v7.app.ActionBarActivity. This is how it looks:
However, when I click in the overflow action button, the options show up over the ActionBar:
I wanted to show the overflow menu under the ActionBar, as it is supposed to. How can I solve this?
I'm running Android 4.4 (KITKAT). If you need any more details, just ask. Thanks.
Here is the code of my main activity:
public class SendMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_message);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_action_bar_icon);
getSupportActionBar().setDisplayUseLogoEnabled(true);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.activitySendMessage, new SendMessageFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_send_message, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.actionSettings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
if (id == R.id.actionShowHistory) {
startActivity(new Intent(this, SentMessagesHistoryActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}

Categories