Bringing EditText to front of WebView Android - java

I have the following code:
public class MainActivity extends ActionBarActivity {
EditText userfield;
EditText passfield;
Button logbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userfield = (EditText)findViewById(R.id.editText);
passfield = (EditText)findViewById(R.id.editText2);
logbtn = (Button)findViewById(R.id.button);
GifWebView view = new GifWebView(this, "file:///android_asset/watg.gif");
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
setContentView(view);
userfield.bringToFront();
userfield.invalidate();
passfield.bringToFront();
passfield.invalidate();
logbtn.bringToFront();
logbtn.invalidate();
}
As you can see the GifWebView (extends WebView) is created programatically after the onCreate()
The button and EditTexts are made in the Activity.xml but are brought to front programatically in the Java file.
Why doesnt the above code work?
EDIT - Frame Layout Problem
My XML Code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.dencallanan.giftestapp.GifWebView android:id="#+id/GWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginBottom="153dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="USERNAME" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_marginTop="58dp"
android:layout_alignTop="#+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:hint="PASSWORD" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="#+id/button"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
</FrameLayout>
The Error:

You are calling setContentView(R.layout.activity_main) and again later setContentView(view). setContentView will overwrite the layout, this will in effect, replace it with a new layout. I suggest using a FrameLayout in XML file... Webview will be your bottom layer. On top of that Put a Linear/RelativeLayout with the Edittexts and Button... Simple and clean.
You can add your webview in XML like:
<com.package.GifWebView android:id="#+id/GWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
In code, access this as:
GifWebView gWView = (GifWebView) findViewById(R.id.GWebView);
gWView = new GifWebView(this, "file:///android_asset/watg.gif");

Related

TextView created programmatically not shown

I am trying to create a TextView programmatically which will be shown in another layout that doesn't belong to the same activity but the TextView isn't shown. Following is the code:
Activity:
public class LogActivity extends AppCompatActivity {
LinearLayout textLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
LayoutInflater layoutInflater=getLayoutInflater();
View textEntryLayout=layoutInflater.inflate(R.layout.layout_two, null);
textLayout=(LinearLayout) textEntryLayout.findViewById(R.id.layout);
textView=new TextView(this);
textView.setText("TextView");
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout.addView(textView);
}
Layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#color/editTextBG">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
That's because your container layout, has other children whose heights are set to match_parent. You won't be able to see TextView you add in Java code since the other children added in XML fill all the screen.
You need to add textEntryLayout to your layout. You are modifying that inflated view without adding it to (e.g.) a layout in your layout_one.
Try:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1);
layout.addView(textEntryLayout);

how to get a layout on click of imageview?

Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.
It should look like this after click event of ImageView. How can I get this??
Pleas help...
For perfect answer please add your xml file and java file source code.
Example
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linear1,linear2;
ImageView imgArrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
imgArrow = (ImageView) findViewById(R.id.imgArrow);
linear1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linear2.getVisibility() == View.GONE){
linear2.setVisibility(View.VISIBLE);
imgArrow.setImageResource(R.drawable.up);
}else{
linear2.setVisibility(View.GONE);
imgArrow.setImageResource(R.drawable.down);
}
}
});
}
}
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:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btnClick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="Additional Contact" />
<ImageView
android:id="#+id/imgArrow"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.2"
android:src="#drawable/down" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name here" />
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Phone number here" />
<Button
android:id="#+id/btnone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Click Here" />
</LinearLayout>
</LinearLayout>
Here down and up is image file for arrow.
so put that both arrow file in drawable folder.
You could just set an OnClickListener to the ImageView, which contains the arrow. Then assign a method, which handles the click and produces your different layout

Unable to use components of different layout in android main activity

In my activity I am displaying splash(splash.xml) screen for 5 sec and then I am changing content view to my actual layout of main activity(activity_main). But when I am trying to use the components in the my main activity layout(activity_main) I am getting null pointer exception.
Main Activity is
public class MainActivity extends ActionBarActivity {
ProgressBar progressBar2;
EditText userName,password;
static String url;
JSONObject reader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new CountDownTimer(5000,1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
progressBar2=(ProgressBar)findViewById(R.id.progressBar2);
progressBar2.setVisibility(View.GONE);
userName=(EditText)findViewById(R.id.userName);
password=(EditText)findViewById(R.id.password);
}
}
when I use any of this components I am getting null pointer exception.
My splash Screen layout file splash.xml
<RelativeLayout
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_web">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="111dp"
/>
</RelativeLayout>
and the main layout(activity_main.xml is)
<LinearLayout
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:hint="Username"
android:layout_height="wrap_content"
android:id="#+id/userName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password"
android:hint="Password"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me"
android:id="#+id/checkBox"
android:checked="false"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button"
android:onClick="login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:id="#+id/button3"
android:onClick="signUp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"/>
</LinearLayout>
</LinearLayout>
Because your
#Override
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
will be executed after 5 seconds.
Set your layout
setContentView(R.layout.activity_main);
before progressBar2=(ProgressBar)findViewById(R.id.progressBar2);

Android TextView should scroll vertically automatically

I need a TextView which scrolls automatically vertically. This is my layout code:
activity_info.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/iView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:contentDescription="#string/info"
android:src="#mipmap/infoImage" />
<TextView
android:id="#+id/tView"
android:layout_below="#+id/iView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:maxLines="500"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_below="#+id/tView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:id="#+id/buttonYes"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="YES" />
<Button
android:id="#+id/buttonNo"
android:layout_toEndOf="#id/buttonYes"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="NO" />
</RelativeLayout>
</RelativeLayout>
I read that I have to input this line:
tView.setMovementMethod(new ScrollingMovementMethod())
into my onCreate method in my activity:
InfoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
TextView tView = (TextView) findViewById(R.id.tView);
tView.setMovementMethod(new ScrollingMovementMethod());
}
If I do that, the TextView does not scroll automatically, if some new input (text) is inserted into the View. I know there are some other questions concerning the problem. But I've tried most of them.
How can I solve this problem?
Try to set the textView to android:gravity="bottom"
and use textView.append("\n"+"New Input Text."); for new input text;
You are almost there. Just add a few more things to achieve this,
First of all add those properties in your TextView
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
android:gravity="bottom"
Secondly as usual use,
tView.setMovementMethod(new ScrollingMovementMethod());
This should solve your problem.
I would suggest wrapping your TextView in a ScrollView
<ScrollView
android:id="#+id/sView"
android:layout_below="#+id/iView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/tView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="500"
android:scrollbars="vertical"
android:gravity="bottom" />
</ScrollView>
You can append new text by doing
public void appendAndScrollDown(String someText) {
tView.append("\n" + someText);
sView.fullScroll(View.FOCUS_DOWN);
}

EditText null after calling findViewById()

I've had this issue for the past couple days, and I can't seem to figure out what's wrong. I feel like I've written these exact lines of code a million times. I have two EditText components that I'm trying to get the text from when a button is clicked. But for some reason I'm getting a NullPointer exception for both the EditTexts. Calling findViewById() finds the buttons just fine, but doesn't seem to resolve the EditTexts.
When I run the application, the UI appears and I am able to click the "Connect" button. Logcat tells me the NullPointerException is coming from line 46 in "MainActivity.java", which is where the first EditText is. I've run tests and have verified that they are null. I can't seem to figure out why though, any insight on resolving the matter would be greatly appreciated. I'm running Android Studio v1.0.2.
MainActivity.java
public class MainActivity extends Activity{
private TextView _ipLabel;
private EditText _ipInput;
private EditText _portInput;
private Button _hostButton;
private Button _connectButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_ipLabel = (TextView) findViewById(R.id.ip_label);
_ipInput = (EditText) findViewById(R.id.ip_input);
_portInput = (EditText) findViewById(R.id.port_input);
_hostButton = (Button) findViewById(R.id.hostButton);
_connectButton = (Button) findViewById(R.id.connectButton);
_hostButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Host Button", "Clicked");
}
});
_connectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String connectionAddr = _ipInput.getText().toString() + ":";
connectionAddr+= _portInput.getText().toString();
Log.d("Host", connectionAddr);
}
});
}
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/ip_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-.-.-.-"/>
<Button
android:id="#+id/hostButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Host "/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP: "/>
<EditText
android:label="#+id/ip_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PORT: "/>
<EditText
android:label="#+id/port_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/connectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"/></LinearLayout>
<EditText
android:label="#+id/port_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Here it isn't android:label, it should be android:id.
Change this to,
android:id="#+id/port_input"
Also to the other edit text used.
Instead of:
<EditText
android:label="#+id/ip_input" // <-- this should be an id not a label
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
use:
<EditText
android:id="#+id/ip_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
try this..
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/ip_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-.-.-.-"/>
<Button
android:id="#+id/hostButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Host "/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP: "/>
<EditText
android:id="#+id/ip_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PORT: "/>
<EditText
android:id="#+id/port_input"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/connectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"/></LinearLayout>

Categories

Resources