I Am Making A Note Saver Application, For The Main Body of Note I Am Using A Multiline EditText Looks Like This:-
Screenshot
Code For EditText:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mnotes.view.TextField
android:id="#+id/mNoteEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#null"
android:gravity="top|start"
android:inputType="textMultiLine"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:scrollHorizontally="false"
android:hint="Start Writing For Here...."
android:padding="8dp"
android:clickable="true"
android:focusable="true"
android:fontFamily="sans-serif-smallcaps"
android:longClickable="true"
android:textIsSelectable="true"/>
</LinearLayout>
When I Scroll This EditText It Gains Focus Which Makes Softkey Popup, This Behaviour Makes The Scrolling Experience Very Bad.So I Want To Disable Focus On EditText While Scrolling, Please Help.
I found a solution (only works on short texts). You can use the ScrollView onScrollChange event.
This is how I did it:
ScrollView scroll = findViewById(R.id.scrollView1);
scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3) {
et.clearFocus();
}
});
The variable et is an EditText object.
Related
Ok this is a weird one I hope someone can explain to me.
I have a custom button layout which creates a button with a circular progress bar in the middle of the button. My XML code is below. What I can't work out however is that the ProgressBar seems to be appearing behind the button. If I set the button background to anything other than transparent the progressbar cannot be seen. With the button background as transparent I can then see the ProgressBar but it still appears behind the button text. I was under the understanding that views appeared in the order they are added. I have even tried setting the view to be on top (view.bringToFront();) and I've tried removing the view and recreating it.
Why does the progressbar appear behind the button and what can I do to solve it?
Many thanks
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/holo_blue_bright"
android:padding="2dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:gravity="center"
android:textColor="#android:color/white"
android:singleLine="true"
android:clickable="false">
</Button>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:visibility="visible"
/>
</RelativeLayout>
Code using the above layout
private void setupTableLayout(int NumberOfRows, int NumberOfButtons){
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(0, android.widget.TableRow.LayoutParams.MATCH_PARENT, 3f);
TableLayout tableLayout = (TableLayout) findViewById(R.id.thetablelayout);
tableLayout.removeAllViews();
for (int i = 0; i < NumberOfRows; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(tableParams);
RelativeLayout btnOneLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
RelativeLayout btnTwoLayout = (RelativeLayout)getLayoutInflater().inflate(R.layout.custom_button, null);
ProgressBar btnOneProgressBar = (ProgressBar)btnOneLayout.findViewById(R.id.progressBar);
ProgressBar btnTwoProgressBar = (ProgressBar)btnTwoLayout.findViewById(R.id.progressBar);
btnOneLayout.setLayoutParams(rowParams);
btnTwoLayout.setLayoutParams(rowParams);
Button btnOne = (Button)btnOneLayout.findViewById(R.id.button);
btnOne.setText("Btn 1, Row " + i);
btnOne.setId(1001 + i);
Button btnTwo = (Button)btnTwoLayout.findViewById(R.id.button);
btnTwo.setText("Btn 2, Row " + i);
btnTwo.setId(2001 + i);
setButtonClickListener(btnOneLayout, btnOneProgressBar);
setButtonLongClickListener(btnOneLayout, btnOneProgressBar);
tableRow.addView(btnOneLayout); //Add layout, instead of just Button
View adivider = new View(this);
adivider.setLayoutParams(new TableRow.LayoutParams(20, TableRow.LayoutParams.MATCH_PARENT));
adivider.setBackgroundColor(Color.TRANSPARENT);
// This bit of code deals with odd/even numbers of buttons.
if (((i + 1) * 2) < NumberOfButtons + 1) {
tableRow.addView(adivider);
tableRow.addView(btnTwoLayout);
} else {
tableRow.addView(adivider);
btnTwoLayout.setBackgroundResource(android.R.color.transparent);
tableRow.addView(btnTwoLayout);
}
tableLayout.addView(tableRow);
}
}
You are propably running this on android >= 5.0. In 5.0 they added elevation field for views. Elevation defines z-order of views in ViewGroup.
In that case button have non-zero elevation value and progress bar have zero value elevation.
Set elevation of progress bar to e.g. 10dp
<ProgressBar
...
android:elevation="10dp"/>
Put your button into another layout (best choice for this case is probably FrameLayout).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
... />
</FrameLayout>
<ProgressBar
android:id="#+id/progressBar"
... />
</RelativeLayout>
I can't tell you why exactly you get that effect, but I suppose that is a bug. Notice that if you replace Button with other view, for example TextView that problem doesn't exits. But when you change RelativeLayout to any other (tested with FrameLayout) this bug still appears. I guess it's going about background property and order of drawing or measurement in any layout.
try using FrameLayout like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#android:color/holo_blue_bright"
android:padding="2dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="Button"
android:gravity="center"
android:textColor="#android:color/white"
android:singleLine="true"
android:clickable="false">
</Button>
<ProgressBar
android:layout_gravity="center"
android:id="#+id/progressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
android:visibility="visible"
/>
</FrameLayout>
See this link
Generally, FrameLayout should be used to hold a single child view,
because it can be difficult to organize child views in a way that's
scalable to different screen sizes without the children overlapping
each other. You can, however, add multiple children to a FrameLayout
and control their position within the FrameLayout by assigning gravity
to each child, using the android:layout_gravity attribute.
Child views are drawn in a stack, with the most recently added child on top.
By adding marginTop you can do that.. otherwise you can change the structure of button and progress bar...
<linearLayout android:orientation="horizontal" ... >
<ImageView
android:id="#+id/thumbnail"
android:layout_weight="0.8"
android:layout_width="0dip"
android:layout_height="fill_parent"
>
</ImageView>
<TextView
android:id="#+id/description"
android:layout_marginTop="-20dip"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="wrap_content"
>
</TextView>
this code is working fine for me :D
I want to show simple round progress bar after I clicked on a button, but it's doesn't work. Look on my XML
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/progress_layout">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_gravity="center"
android:visibility="invisible" />
</FrameLayout>
And my java code
postPet = (Button) myFragmentView.findViewById(R.id.post_pet);
postPet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
}
});
There is no error, nothing happening after click.
P.S. I initialize variable progressBar in fragment's onCreate method
LayoutInflater infl = getActivity().getLayoutInflater();
ViewGroup rootGroup = (ViewGroup) getActivity().findViewById(R.id.progress_layout);
View root = infl.inflate(R.layout.fragment_new_pet_form, rootGroup);
progressBar = (ProgressBar) root.findViewById(R.id.progressBar);
You should add your pb over a button in xml like:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/progress_layout">
<Button
android:id="#+id/post_pet"
your button description here />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
this way it has to work and you would not need to inflate the pb additionally.
Try adding one of the associated styles: http://developer.android.com/reference/android/widget/ProgressBar.html
You can also set the background color of its parent to some value, and see if any part is opaque: if not, the progress bar has no width and height or is not visible.
Finally you can use hierarchyviewer with the emulator to inspect the layout and verify what's happening.
Also if the frame layout has no other children, get rid of it and use layout_gravity to position the progress bar in its parent: no need for the extra viewgroup!
I have an activity inside my android app that lists some information inside ListView , in the top left corner of each Item there is a more information icon, when user touches this icon application should change the content of this list item to some useful information , and also the icon should change to another icon , for example a close icon , again when user touchs close icon application should show list item contents.
For changing the Icon i used this code, but first time that i click icon nothing happens , second time it changes, and sometimes when i click one of the icons the icon of another list item changes.
ListActivity.java
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View viewCliced, int position, long id)
{
img = (ImageView)viewCliced.findViewById(R.id.img_icon);
img.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
img.setImageResource(R.drawable.setting);
}
});
}
});
this code changes the icon but user should touch icon twice and sometimes change another list item's icon :|
this is the XML of ListView Item
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/item_select"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="8" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="top|left"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:src="#drawable/bookabout" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:gravity="right|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="نام فصل"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_explanation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:text="توضیحات فصل درسی"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
How i should develop this feature ?
toggling between two kind of contents for a ListView item when user touches an Icon inside the ListView Item
I read a lot of article about list views but i couldn't find a way to do that,
Thank you very much for answering
Best Regards
"For changing the Icon i used this code, but first time that i click icon nothing happens , second time it changes, and sometimes when i click one of the icons the icon of another list item changes."
because you declare the listener inside the list listener so for the first time you click the icon your listener set (because you click list item ) and for the second time the icon responses. pull out listener of img and put it in onCreate method. Inside your adapter modify the getView method like this:
#Override
public View getView(int position, View row, ViewGroup parent) {
final int listItemPosition = position;
ImageView img = (ImageView)row.findViewById(R.id.image1);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// Change icon here
// If you'd like to delete item use the code below
// List.remove(listItemPosition);
// Adapter.this.notifyDataSetChanged();
}
});
}
I have a grid view with 7 images, and now when I inflate it I get it like this
Here you can see the images are aligned as per normal view
But I dont want it like that I want it like this
The bottom images are aligned to form kind of a pyramid or triangle type
How can i achieve this in gridview in android ???
If you try to set the padding then you get the view to be shrinked like this
if(position == 4){
view.setPadding(50, 0, 0, 0);
}
So I would suggest something like this
Create 2 gridviews in xml as this such that they have their alignments centered
<TextView
android:id="#+id/txtControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<GridView
android:id="#+id/gridView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/txtControl"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:numColumns="4" >
</GridView>
<GridView
android:id="#+id/gridView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtControl"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:numColumns="3" >
</GridView>
Now In your main activity create 2 String values for each
public class MainActivityWrapped extends Activity {
static final String[] MOBILE_OS1 = new String[] {
"Android", "iOS", "Windows", "Blackberry"};
static final String[] MOBILE_OS2 = new String[] {
"Android", "iOS", "Windows"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridviewlayout);
GridView gridView1 = (GridView) findViewById(R.id.gridView1);
gridView1.setAdapter(new ImageAdapterWrapped(this, MOBILE_OS1,gridView1));
GridView gridView2 = (GridView) findViewById(R.id.gridView2);
gridView2.setAdapter(new ImageAdapterWrapped(this, MOBILE_OS2,gridView1));
}
}
Either you can create 2 adapter or control the same 1st adapter to be reused
The output would be like this
As Takendarkk comments, the layout you want is not a grid
You should be able to achieve what you want with nested linearlayouts:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- first 4 image views here -->
</LinearLayout>
<LinearLayout
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- last 3 image views here -->
</LinearLayout>
</LinearLayout>
This is do-able, but the solution is messy.
If you know how many images are going to be in the row, you can calculate how much padding you'll need on the left and right images of your row.
Then, in your getView() of your adapter, you can set this padding bearing in mind that due to the way views are recycled in adapters, you'll need to unset the padding for those that do not need it.
Let me know in a comment if you need some code to better illustrate what I'm proposing.
I am just starting writing Android apps. I have done some beginner's tutorials, but I'm having trouble with the next step for which I could not find any helpful answer.
I want to write an app that computes the value of a complex function which depends on a three parameters. This works if I put everything on a single view:
For each parameter one TextField (with the parameter name) plus one EditText field to enter the value. At the bottom there is one Button, and when this is clicked, the result is displayed in another TextField.
But now I want improve the layout (plus I want to learn how to deal with more complex structures):
Two of the parameters are usually just set once and then kept at their values. So I decided to move these two parameters to a different view.
No I'm looking for recommendations (and hopefully links example code):
Which structure should I use? It seems that I do not need to use different activities, but one activity with different views should do the job.
ViewSwitcher sounds reasonable, since I only require two different views in this case. On the other hand ViewFlipper may be preferable, since I can reuse this later for other projects with multiple views. I also read that ViewPager allows to swipe between different views.
How can I read an EditField if this is on a different view?
I tried to use ViewSwitcher, and it worked when I added an additional button to switch to a second view. But when I moved the TextField and EditField for parameters one and two
to the second view, the app does not run on the emulator, just stating "error in app" (while Eclipse shows no errors). From this, I guess that I have to do some additional work to pass data in EditText between different views.
I have not found any examples for this - can anybody give me some advice/examples?
Any help is appreciated
I figured that my initial description was maybe not too helpful without any code.
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/viewSwitcher1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/pg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1st view" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value1" />
<EditText android:id="#+id/val1"
android:text="2.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/v2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value2" />
<EditText android:id="#+id/val2"
android:text="3.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:layout_width="wrap_content"
android:id="#+id/calc"
android:layout_height="wrap_content"
android:text="2*Val1 + Val2 =" />
<TextView android:id="#+id/res"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch view 1" />
</LinearLayout>
</LinearLayout>
<!-- next view -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/pg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View 2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch view 2" />
</LinearLayout>
</LinearLayout>
</ViewSwitcher>
and here is my main activity:
package com.example.testas2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity {
ViewSwitcher switcher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1);
initControls();
}
private void initControls()
{
Button calculate=(Button)findViewById(R.id.calc);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick (View v) { calculate(); }});
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new Button.OnClickListener()
{public void onClick (View v) { switcher.showNext(); }});
Button b2=(Button)findViewById(R.id.b2);
b2.setOnClickListener(new Button.OnClickListener()
{public void onClick (View v) { switcher.showPrevious(); }});
calculate();
}
private void calculate()
{
EditText val1=(EditText)findViewById(R.id.val1);
EditText val2=(EditText)findViewById(R.id.val2);
TextView res=(TextView)findViewById(R.id.res);
Double Val1=Double.parseDouble(val1.getText().toString());
Double Val2=Double.parseDouble(val2.getText().toString());
Double result = 2*Val1+Val2;
res.setText( String.format( "%.3f", result ) );
}
#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;
}
}
This version is working:
- when pushing the first button, it computes: result = 2 * value1 + value2)
- when pushing the button "switch view 1", it goes to the second view
- on the second view, when pushing the button "switch view 2", it goes back to view 1
But I can't get the following two pieces to work:
1) I would like to move the "switch view 1" button to the top of the first view, but this gives an error in the emulator ("Unfortunately testas2 has stopped").
2) I would like to move the input of the second value to the second view, but this gives the same error in the emulator.
What am I doing wrong?
Why does the order of the elements in the layout matter, and how can I modify my code to make it work?
Example code -
TextView tField= (add cast here)findViewById(R.id.editText)
tfield.getText();
The above answer can be improved if you post some code of yours because findViewById at times depends on the container view and the above code will not work then as it will look for the textfield in the default view.
I just did "Project > Clean", restarted the app in the emulator, and now it works!
In other words, the solution for the original question is trivial (in fact there was no real problem). One can simply move elements in the layout file (TextView, EditText, Button) within a view or between views. The posted activity works always and the data (from the different TextView and EditView fields) is accessible everywhere in the code. Therefore, no explicit "transfer" of data between views is required.
Only after moving elements in the layout file one should do "Project > Clean".
In your current Activity, create a new Intent:
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
Then in the new Activity, retrieve those values:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
For more info follow this link