I want to adjust editText input type such that first I want to show number only on keybord with a button for switching keyborad from numbers keypad to qwerty .I have find many similar question related to this but nothing has worked out.
I have tried to do this by setting ime action button.Don't know whether it is right or not but it is not working for me.Is there any other alternative for this?
Here is the code :
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.helloword.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText android:id="#+id/ed"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:imeOptions="actionGo|actionNext|actionSend|actionDone"
android:imeActionLabel="abc"
android:inputType="number"
android:imeActionId="#+id/my_ime"/>
</RelativeLayout>
Activity:
public class MainActivity extends ActionBarActivity {
EditText ed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText)findViewById(R.id.ed);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
ed.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg1==R.id.my_ime)
{
Log.e("inside", "inside");
ed.setInputType(InputType.TYPE_CLASS_TEXT);
}
return false;
}
});
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);
}
}
Related
My activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.myactionbar.actionbar.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<!-- Enable this layout on clicking the icon in actionbar -->
<RelativeLayout
android:id="#+id/rl_ListView1"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="#color/black"
android:visibility="invisible">
</RelativeLayout>
</RelativeLayout>
Main.xml - It shows an icon at actionbar
<item
android:id="#+id/show_info"
android:orderInCategory="100"
android:title="#string/search"
app:showAsAction="always"
android:icon="#drawable/ic_icon"
/>
On clicking the ic_icon icon in actionbar, i need to show the hidden Relativelayout with id=rl_ListView1.
I don't know how to find the layout with id on clicking an item in actionbar.
Please help me to do this.
You need to override onOptionsItemSelected method in our activity
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
//Code for showing layout
}
return super.onOptionsItemSelected(item);
}
=========
Updated
if (id == R.id.show_info) {
if(r1.getVisibility()== View.VISIBLE)
{
//Hide your layout
}
else
{
//Show your layout
}
Override onCreateOptionsMenu() and onOptionsItemSelected() in your activity . For e.g:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.your_menu, 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
switch (id) {
case R.id.show_info:
// Change visibilty of your RelativeLayout here
if(rl.getVisibility() == View.VISIBLE){
rl.setVisibility(View.INVISIBLE);
}else{
rl.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
try {
switch (item.getItemId()) {
case R.id.menu:
//do here
break;
default:
return super.onOptionsItemSelected(item);
}
} catch (Exception e) {
log(e);
}
}
I already have made a class that extends PreferenceFragment to display my apps preferences. It works, but I decided I should make the fragment appear in a new activity so that the user can back out of it. Right now, it just gets swapped into the main fragment and displayed. How can I simply make an activity that displays the entire PreferenceFragment? There is no layout defined for the Fragment since it's a PreferenceFragment.
Make a new activity that contains one fragment. Insert the preferences fragment in it. Here is a copy of what I use in my project.
public class SettingsActivity extends Activity{
public final static String SETTINGS_NATIVE_IGNORE = "pref_native_ignore";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(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();
switch(id)
{
case android.R.id.home:
onBackPressed();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
then the xml for it
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:baselineAligned="false"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="...SettingsActivity"
tools:ignore="MergeRootFrame" >
<fragment
android:id="#+id/preferences_fragment"
android:name="...fragment.PreferencesFragment"
tools:layout="#layout/fragment_preferences"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
And finally the preferences fragment
public class PreferencesFragment extends PreferenceFragment {
public PreferencesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
I'm new to Android and wonder how could I show a user a simple message after LongClick in one of the following buttons. The idea is to show the user the password he is using on each of the sites after a LongClick.
Any ideas?
public class MainMenu extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
setTitle("Main Menu");
}
#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, 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);
}
public void openWeb1(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.ljmu.ac.uk/")));
}
public void openWeb2(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.bbc.co.uk")));
}
public void openWeb3(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com")));
}
public void openWeb4(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.adidas.com")));
}
public void openWeb5(View v){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
}
public void addNew(View v){
}
}
The XML code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/GridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="net.androidbootcamp.myunimanager.MainMenu" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android1:id="#+id/btnLJMU"
android1:layout_width="290dp"
android1:layout_height="76dp"
android1:drawableRight="#drawable/profile"
android1:onClick="openWeb1"
android1:text="Liverpool JMU" />
<Button
android1:id="#+id/btnBBC"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="#drawable/contacts"
android1:onClick="openWeb2"
android1:text="BBC News" />
<Button
android1:id="#+id/btnFACEBOOK"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="#drawable/courses"
android1:onClick="openWeb3"
android1:text="facebook" />
<Button
android:id="#+id/btnADIDAS"
android:layout_width="112dp"
android:layout_height="0dp"
android:drawableRight="#drawable/calendar"
android1:onClick="openWeb4"
android1:text="adidas" />
<Button
android1:id="#+id/btnSTACKOVERFLOW"
android1:layout_width="match_parent"
android1:layout_height="76dp"
android1:drawableRight="#drawable/notes"
android1:onClick="openWeb5"
android1:text="StackOverFlow" />
<Button
android1:id="#+id/btnADDNEW"
android1:layout_width="match_parent"
android1:layout_height="78dp"
android1:onClick="addNew"
android1:drawableRight="#drawable/myuni"
android1:text="Add New" />
</LinearLayout>
</ScrollView>*
Have a LongClickListener for the button. Inside of it, you can execute the corresponding action by showing a pop up.
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Check the following to learn about how to create pop up messages.
Hope this helps!
I was just trying to get to know well how to change activities and i made a simple example and yet nothing.
The thing is, i dont know how to go back to the main activity after changing to the second activity.
here is MainActivity.java
public class MainActivity extends ActionBarActivity implements OnClickListener{
private String DEBUG_TAG1 = "tag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button pauseButton = (Button) findViewById(R.id.pause_button);
pauseButton.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
if (v.getId() == R.id.pause_button)
{
Log.d(DEBUG_TAG1,"pause");
Intent i = new Intent(this, SecondActivity.class);
Log.d(DEBUG_TAG1,"intent created");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
protected void onStop() {
super.onStop();
Log.d(DEBUG_TAG1,"on stop");
}
protected void onResume() {
super.onResume();
Log.d(DEBUG_TAG1,"on resume");
}
#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);
}
}
here is SecondActivity.java
public class SecondActivity extends ActionBarActivity implements OnClickListener{
private String DEBUG_TAG2 = "tag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button pauseButton2 = (Button) findViewById(R.id.pause_button2);
pauseButton2.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
if(v.getId() == R.id.pause_button2)
{
//here i have to get back to the MainActivity
}
}
#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 here are the XML files:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="net.iversoncru.changeact.MainActivity" >
<Button
android:id="#+id/pause_button"
android:layout_gravity="center"
android:text="||"
android:textColor="#ff0000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
</LinearLayout>
and activity_second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#000000"
tools:context="net.iversoncru.changeact.SecondActivity" >
<Button
android:id="#+id/pause_button2"
android:layout_gravity="center"
android:text="||"
android:textColor="#aa0000"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
</LinearLayout>
can anyone tell me how?
I noticed that, when SecondActivity starts, OnStop() is called on MainActivity.
How do i use OnResume()?
Also i read that maybe i have to save the state of MainActivity to be able to be back to it...
Im a little bit lost.
#Override
public void onClick(View v)
{
if(v.getId() == R.id.pause_button2)
{
//here i have to get back to the MainActivity
finish();
}
}
when you switch to the next activity OS pushes current activity on the stack and brings forward the requested activity.
finish() method from activity class will announce to the system that you are done with your work in current activity and restore my previous activity on the stack.
I am new to android development and I cannot understand this error, I am making a simple stop watch app, it has a Chronometer and 3 buttons (Start, stop, reset) I define all of these in the main java file correctly with their correct tags.
Its strange because I tested the app before and my app was working fine, but then I re-positioned the buttons in the GUI, and now it is saying that I am not casting the chronometer correctly.
06-06 19:11:07.488: E/AndroidRuntime(21511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shitstopwatch/com.example.shitstopwatch.ShittyClockActivity}: java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.Chronometer
here is the main java file:
public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, 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);
}public class ShittyClockActivity extends ActionBarActivity {
Button Start;
Button Stop;
Button Reset;
Chronometer mainChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shitty_clock);
mainChronometer = (Chronometer) this.findViewById(R.id.mainChronometer);
Start = (Button) this.findViewById(R.id.startButton);
Stop = (Button) this.findViewById(R.id.stopButton);
Reset = (Button) this.findViewById(R.id.resetButton);
addButtonListeners();
}
private void addButtonListeners()
{
Start.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
int stoppedMilliseconds = 0;
mainChronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
mainChronometer.start();
}
});
Stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.stop();
}
});
Reset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
mainChronometer.setBase(SystemClock.elapsedRealtime());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.shitty_clock, 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 here is the activity XML with the ID tags:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.shitstopwatch.ShittyClockActivity$PlaceholderFragment" >
<Chronometer
android:id="#+id/mainChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="#string/chronometer"
android:textSize="#dimen/BigText" />
<Button
android:id="#+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/mainChronometer"
android:layout_below="#+id/mainChronometer"
android:layout_marginTop="63dp"
android:text="#string/startButton" />
<Button
android:id="#+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/startButton"
android:layout_below="#+id/startButton"
android:layout_marginTop="16dp"
android:text="#string/resetButton" />
<Button
android:id="#+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/resetButton"
android:layout_below="#+id/resetButton"
android:layout_marginTop="20dp"
android:text="#string/stopButton" />
</RelativeLayout>
Clean your project (e.g., Project > Clean in Eclipse).
Sometimes, the R constants get out of sync with the resource entries in the APK file, causing this error.