i've created one searchEditBox and a button.Initially when the page loads.,the searchEditBox will be focussed with orange color mark automatically.when i use searchValue.setFocusable(false);.,its unfocussed but i couldnt able to type anything on that box(Box will be disabled).How could i recover this probs.?
java:
setContentView(R.layout.main);
edit=(EditText)findViewById(R.id.edit);
but=(Button)findViewById(R.id.but);
edit.setFocusable(false);
xml:
Hopefully this will solve your problem. Add the android:focusableInTouchMode="true" to your linearLayout that holds your EditText in your mail.xml file.
ie:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout"
android:focusableInTouchMode="true">
This worked for me. Very simple solution.
comment your edit.setFocusable(false);
use searchValue.requestFocus() to set focus on EditText. It will work for you.
Check the following code.
Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admob);
// Lookup R.layout.main
EditText edit = (EditText) findViewById(R.id.edit);
Button but=(Button)findViewById(R.id.but);
// edit.setFocusable(false);
edit.requestFocus();
}
admob.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<EditText android:id="#+id/edit"
android:layout_width="250px"
android:layout_height="wrap_content"
/>
<Button android:id="#+id/but"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="click"/>
</LinearLayout>
Donot forget to vote if my response is helpful for you.
Thanks
Deepak
Related
Trying to change a layout from an ImageButton but when i run it on my phone it does not switch instead it says program has stopped.
Java;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton ImageButton = (ImageButton) findViewById(R.id.image);
ImageButton.setBackgroundColor(1);
ImageButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
setContentView(R.id.aaa);
}
});
}
}
XML for first layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.bassammetwally.anew.MainActivity">
<RelativeLayout
android:layout_width="420dp"
android:layout_height="50dp"
android:background="#3955a1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Writer's Block"
android:textColor="#ffffff"
android:textSize="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="4dp"
android:id="#+id/Asd"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="40dp"
android:src="#drawable/pen"
android:layout_marginLeft="380dp"
android:layout_marginTop="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/image"/>
</RelativeLayout>
</LinearLayout>
Second layout to change to;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/aaa">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ans"/>
</LinearLayout>
I think it might have something to do with my syntax.
You need to use a layout reference instead of an id
setContentView(R.layout.view);
Where view.xml is the layout you want.
You may want to rethink your approach, though. Using Fragments is preferred over completely replacing the layout because your ImageButton is no longer usable and you'll need to recall findViewById for any new views that are displayed.
If you just want to load a layout file and add it to the current layout, see Inflating one layout
There are 3 xml files. Main.xml has a linear layout.
Button.xml just contains a button.
txt.xml just contains an edit text.
I inflate button.xml to Main.xml.
Looks fine.
Button has the same size as it was in Button.xml.
Next i inflate edit text to Main.xml which has an ems=10(android:ems="10").
When i run, the button changes its size to same as that of edit text.
How does that happen and how to overcome the problem. the code is pasted.
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:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
tools:context=".First" />
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
Button.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
txt.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
>
<requestFocus />
</EditText>
Main.java
public class First extends Activity {
LinearLayout lout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
final LayoutInflater inf=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Button btn=(Button)inf.inflate(R.layout.button, null);
lout=(LinearLayout)findViewById(R.id.ll);
lout.addView(btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(lout.getChildAt(5)==null){
EditText e=(EditText)inf.inflate(R.layout.txt, null);
lout.addView(e);
}
}
});
}
on 5 clicks of the button, 5 edit texts will show up. but the moment i click the button the size of the button changes in addition to the edit text formation.
Please paste your code here, only then anybody can help you. Still you can set a layout_height & layout_width of the button to something like 20sp(as per your need).
In your Main.xml file you need to fix the width of the layout as the layout is small at first because the textview is small. But when you click on the Button the layout increases as per the required width for the EditText and so the Button size also increases:
<LinearLayout android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="100sp"
android:layout_height="wrap_content" >
</LinearLayout>
In my layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/user_pswd_new_root" android:scrollbars="vertical"
android:soundEffectsEnabled="true">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/ScrollViewLogin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbarStyle="outsideInset" android:scrollbars="vertical|horizontal" android:visibility="visible">
<RelativeLayout android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_height="fill_parent">
<ImageView android:background="#drawable/logo_login" android:layout_height="wrap_content" android:id="#+id/imageView1"
android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"
android:padding="0dp" android:layout_margin="0dp"/>
...............
</RelativeLayout>
</ScrollView>
</RelativeLayout>
With the above code, I set in a Dialog and things are shown proeprly, but there is lot of unwanted space above the image which unnecessarily increases the height of the dialog. See the results :
Any idea why the top space is occupied. And how do I get rid of it. Where am I going wrong ?
It's the title of the Dialog, which is empty because you didn't specify a title (but the view is still there). You have to remove it, for example like this:
class MyDialog extends Dialog {
#Override
protected void onCreate(Bundle savedInstanceState) {
// make sure to call requestWindowFeature before setContentView
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_dialog_layout);
// other initialization code
}
// ...
}
But that depends on whether you are using a simple Dialog or an AlertDialog. If this doesn't work for you, post your dialog-creation code (Java) and I'll update my answer to show how to remove the title in your case.
I am having a very weird glitch that I cannot get my head around. Any help will be appreciated as this works on a phone running Android 2.3, tablet running 3.1 but not on a phone running Android 2.1.
I am trying to get the LinearLayout defined in the following XML with the ID of "overview_linear_layout".
overview_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="#layout/listing_view" android:layout_width="fill_parent" android:id="#+id/header_view" android:layout_height="wrap_content"></include>
<ScrollView android:id="#+id/scrollView1" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent" android:id="#+id/overview_linear_layout">
<TextView android:scrollbars="vertical" android:longClickable="false" android:layout_weight="1" android:clickable="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/main_description"></TextView>
</LinearLayout>
</ScrollView>
<include layout="#layout/listing_view" android:layout_width="fill_parent" android:id="#+id/footer_view" android:layout_height="wrap_content" android:layout_weight="0.0"></include>
</LinearLayout>
The code I am using to get the view is as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview_view);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.overview_linear_layout);
if(linearLayout!=null) {
System.out.println("Found linear layout");
}
else {
System.out.println("Did not find linear layout");
}
}
I have tried Project->Clean in Eclipse but that does not help either.
if eclipse don't show overview_linear_layout when you type click on the red cross and click create constant overview_linear_layout in R .
change this
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.overview_linear_layout);
to
LinearLayout linearLayout = (LinearLayout)findViewById(R.layout.overview_linear_layout);`
I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?
Java code is:
public class Test extends Activity {
List<ScrollView> sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical">
<ScrollView android:layout_height="wrap_content" android:id="#+id/scrollView1" android:layout_width="match_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ScrollView>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scrollView2">
<TextView android:text="TextView" android:layout_width="match_parent" android:id="#+id/textView2" android:layout_height="match_parent"></TextView>
</ScrollView>
</LinearLayout>
you haven't setContentview(yourLayout.xml)
it wont be able to findViewById.
do the findViewById AFTER the setContentView
First of you have to set layout file.
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
try this if you are getting any error then please post logcat.