I load url, for example mail.google.com, into webview in my app, and scrolling on web-page so slow! Tested on Samsung GalaxyTab 10.1, Motorola Droid X, Acer Liquid, etc.
Why?
Parameters that I try use:
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setRenderPriority(RenderPriority.HIGH);
add android:hardwareAccelerated="true" to the tag in your manifest.
Remove background image of the webview if you put it and also see if you used heavy size images in progress or loading or other images of heavy size reduce their sizes or remove it
This is a main.xml file
This code is a scrolling only, any where u can use
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a header"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:paddingTop="8dip"></TextView>
<TextView android:text="#+id/TextView02"
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.0"></TextView>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_weight="1.0"></Button>
<Button android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1.0"></Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is .java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ScrollViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.TextView02);
String s="";
for (int i=0; i < 200; i++) {
s += "Android Market ";
}
view.setText(s);
}
}
Related
I'm learning android and And I'm facing some troubles. I kindly request you to solve these problems.
I have 2 linear layouts inside a linear layout, both are placed horizontally. The left side section contains some buttons and if we click the button the respective layout must come on the right-side section. As of now, if I click the button the specific layout is appearing but not inside the second linear layout. And also, I want to set a default layout to appear on the right side layout. For ex, here I've added the "breakfastdishes.xml", which I want as a default right-side layout, and when I click on the buttons from the left-side layout, according to id the right-side layout must change. Can you please help me to achieve it?
Here is my code:
MenuSection.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuSection"
android:background="#drawable/gradient"
android:padding="10dp">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/breakfast"
android:layout_width="220dp"
android:layout_height="65dp"
android:text="#string/breakfast"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#AC8E0D"/>
<Button
android:id="#+id/lunch"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/lunch"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#0A5FAA"/>
<Button
android:id="#+id/eveningSnacks"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:text="#string/snacks"
android:textSize="20dp"
android:textStyle="bold"
app:backgroundTint="#DF5124" />
<Button
android:id="#+id/dinner"
android:layout_width="220dp"
android:layout_height="65dp"
android:layout_marginTop="60dp"
android:text="#string/dinner"
android:textSize="20dp"
android:background="#drawable/menu_category"
android:fontFamily="sans-serif"
android:textStyle="bold"
app:backgroundTint="#14A61A"/>
</LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:id="#+id/itemsDisplay">
<LinearLayout
android:id="#+id/menuDispArea"
android:layout_width="508dp"
android:layout_height="412dp">
<!-- <include-->
<!-- layout="#layout/breakfastdishes"-->
<!-- android:layout_width="508dp"-->
<!-- android:layout_height="412dp" />-->
</LinearLayout>
</ScrollView>
</LinearLayout>
BreakfastDishes.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:paddingStart="12dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingEnd="12dp"
android:paddingRight="10dp"
android:paddingBottom="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical">
<ImageView
android:id="#+id/burger"
android:layout_width="155dp"
android:layout_height="102dp"
android:layout_x="10dp"
android:layout_y="10dp"
app:srcCompat="#mipmap/burger_breakfast" />
<TextView
android:id="#+id/burgerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/burger"
android:layout_alignTop="#+id/burger"
android:layout_alignRight="#+id/burger"
android:layout_alignBottom="#+id/burger"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/burger"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:orientation="vertical">
<ImageView
android:id="#+id/eggOmlet"
android:layout_width="160dp"
android:layout_height="104dp"
android:layout_x="233dp"
android:layout_y="7dp"
app:srcCompat="#mipmap/egg_omlet" />
<TextView
android:id="#+id/eggOmletTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/eggOmlet"
android:layout_alignTop="#+id/eggOmlet"
android:layout_alignRight="#+id/eggOmlet"
android:layout_alignBottom="#+id/eggOmlet"
android:layout_gravity="center_horizontal"
android:layout_margin="1dp"
android:gravity="center"
android:text="#string/eggOmlet"
android:textColor="#FFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
MenuSection.java
package com.example.restaurant;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MenuSection extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_section);
Button breakfast = (Button) findViewById(R.id.breakfast);
Button lunch = (Button) findViewById(R.id.lunch);
breakfast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, BreakfastDishes.class);
startActivity(intent);
}
});
lunch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MenuSection.this, LunchDishes.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed(){
//super.onBackPressed();
}
}
Akshay, here are a few things you have to keep in mind ;
In the button click listener you are launching new activity instead of updating
the existing layout. When you use Intent it will launch new activity and
close the current activity. So in your case do not use startActivity
Use findViewById for the menuDispArea layout and add update
the content when user clicks on the button.
never use dp for text size, use sp
you can divide the layout using LinearLayout weights to split your layouts on
all the devices instead of hardcoding the height and width
https://developer.android.com/guide/topics/ui/layout/linear#Weight
edit: Added sample
LayoutInflater inflater = LayoutInflater.from(MenuSection.this);
LinearLayout parentLayout = findViewById(R.id.menuDispArea);
View menuLayout= inflater.inflate(R.layout.BreakfastDishes, parentLayout, false);
parentLayout.addView(menuLayout);
You can use this to inflate any XML in your layout dynamically. Make sure you are doing this inside button onClick for your use case.
I am attempting to show a list of items (each entry containing 3 textviews) followed by some buttons that will switch to other activities on click, where similar setups exist. While the ListView is populated perfectly when buttons aren't involved, as soon as I try to add buttons it disappears. I think this may be to do with my having set up the music_list.xml file as exclusively for the list, but from looking at other sources it should be possible to have a ListView followed by other Views. Currently the text button doesn't do anything; I'm trying to get it to show up with the ListView before writing any logic out.
My music_list.xml file, where there is a list and hopefully buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ListView
android:id="#+id/music_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="1.0"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="bottom|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="0"
android:text="Test 1"
android:layout_marginTop="32dp"
/>
</LinearLayout>
My music_item file, which holds the 3 TextViews that are used to populate the list;
<?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">
<TextView
android:id="#+id/artist_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="left"
tools:text="lutti" />
<TextView
android:id="#+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="center"
tools:text="one" />
<TextView
android:id="#+id/album_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="right"
tools:text="one" />
</LinearLayout>
And the current logic for an activity page, which populates the list but doesn't yet do anything to the buttons that are meant to be underneath it:
package com.example.android.musicapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by ~ Adam ~ on 1/2/2018.
*/
public class MyMusicActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_list);
ArrayList<Music> music = new ArrayList<Music>();
music.add(new Music("The Who", "Won't Get Fooled Again", "Who's Next"));
music.add(new Music("A Great Big World", "There's an Answer", "Is there Anybody Out There?"));
music.add(new Music("James Blake", "Retrograde", "Overgrown"));
music.add(new Music("Chopin", "Ballade No.3 in A Flat, Op.47", "Chopin: Ballades"));
music.add(new Music("Kendrick Lamar", "Loyalty (Feat. Rihanna)", "DAMN."));
music.add(new Music("Schel", "When the Dragon Came Down", "Schel"));
music.add(new Music("Man man", "Loot My Body", "On Oni Pond"));
music.add(new Music("Sarah Jarosz", "Dark Road", "Build Me Up From Bones"));
music.add(new Music("Jimmy Buffett", "Come Monday", "Songs You Know By Heart"));
MusicAdapter adapter = new MusicAdapter(this, music);
ListView listView = (ListView) findViewById(R.id.music_list);
listView.setAdapter(adapter);
}
}
When you add buttons in your first layout that layout pushes your listview layout to the left so that's why you are unable to see your list. Try using relativeLayout or put your linearLayout correctly. I have used RelativeLayout and it is working fine now:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ListView
android:id="#+id/music_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="bottom|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Test 1"
android:layout_marginTop="32dp"
/>
</RelativeLayout>
As the title shows, I'm trying to make a button in my Android app to change a TextView but it ain't working properly... Here's the code, I hope you guys see what's going wrong. FYI, I'm using NetBeans with the Android 2.3.3 emulator connected as I will be running on my phone (2.3.6) later.
Main java:
public class Rooster extends Activity
{
private Button buttonSearch;
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
this.buttonSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textRooster = (TextView)findViewById(R.id.textRooster);
textRooster.setText("some text");
}
});
}
Well here is my main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="42px"
android:layout_weight="1"
android:gravity="center_vertical" >
<EditText
android:id="#+id/editSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="2"
android:gravity="center"
android:hint="Zoekopdracht" />
<Button
android:id="#+id/buttonSearch"
android:layout_width="0dip"
android:layout_height="37px"
android:layout_weight="1"
android:gravity="center"
android:text="Zoek" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="39px"
android:layout_weight="1"
android:gravity="center" >
<Button
android:id="#+id/buttonChangeWijzigingen"
android:layout_width="fill_parent"
android:layout_height="37px"
android:gravity="center"
android:text="Zet wijzigingen aan" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="12" >
<TextView
android:id="#+id/textRooster"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Geen rooster opgezocht" />
</LinearLayout>
</LinearLayout>
I think the button that you think your clicking is actually getting drawn over. try changing
this.buttonSearch = (Button)this.findViewById(R.id.buttonSearch);
to
this.buttonSearch = (Button)this.findViewById(R.id.buttonChangeWijzigingen);
It doesnt actually fix the problem but just makes more clear what the issue is.
I am new to Android/java programming. I really have no clue what I am doing, and would like a little help. What I am trying to do is build a form very similar to the one below. I have already put in all of the layout, and strings information, but I am having trouble with the functions. The error that I am getting is View cannot be resolved to a type line 14. I am using eclipse to help me program this app. Here is my main.xml, and MadisonStudios.java file's contents as well.
http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-forms/
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome" />
<EditText
android:id="#+id/EditTextName"
android:layout_height="wrap_content"
android:hint="#string/name"
android:inputType="textPersonName"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<Spinner
android:id="#+id/SpinnerStatus"
android:layout_height="wrap_content"
android:prompt="#string/status"
android:layout_width="fill_parent"
android:entries="#array/statuslist">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/changebody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/ButtonSendChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendChange"
android:text="#string/changebutton">
</Button>
</LinearLayout>
MadisonStudios.java
package com.madisonstudios.supportapp;
import android.app.Activity;
import android.os.Bundle;
public class MadisonStudios extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendChange(View button) {
// Do click handling here
}
}
import android.view.View;
Add this line in import statements..
You should import it. If you are using Eclipse, press Ctrl+Shift+o.
I'm trying to change my checkbox dynamically.
for some reason the app throw my an exception.
what am I doing wrong?
this is my java code:
package com.school.commtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
public class OtherPlacesMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox c1 = (CheckBox)this.findViewById(R.id.schools);
c1.setChecked(true);
setContentView(R.layout.otherplacesmenu);
}
}
and this is the xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/table">
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow1">
<CheckBox android:text="School" android:id="#+id/schools" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow2">
<CheckBox android:text="Supermaker" android:layout_height="wrap_content" android:id="#+id/supermarkets" android:layout_width="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow3">
<CheckBox android:text="Gardens" android:layout_height="wrap_content" android:id="#+id/gardens" android:layout_width="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
I'm sure its a simple question, but I cannot find the answer anywhere.
p.s.
I can't set the value of the checkbox to be true at the xml because I would later want to change the checkbox from the user setting.
10x!
Usually you should post the exception message when asking a question like this, but it looks like your problem is that you're trying to findViewById before you setContentView. Your view doesn't exist to be found when you look for it.