Currently I am showing Google Map in Android phone using Java on the whole screen which is working fine for me.
public class MainActivity extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
Below is my XML file-
<?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">
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0vAX8Xe9xjo5gkFNEEIH7KdHkNZNJWNnsjUPKkQ"
/>
</RelativeLayout>
Problem Statement:-
I need to show the Google Map on the Top Half of the Android Screen instead of showing on the full screen. And on the Bottom Half of the Screen I need to show TextBox. Is it possible to do this?
Any thoughts will be appreciated.
Yeah its possible and can easily be implemented with LinearLayout. By assigning weight to MapView and TextView.
In this case
<?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" >
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:apiKey="0vAX8Xe9xjo5gkFNEEIH7KdHkNZNJWNnsjUPKkQ"
android:clickable="true"
android:enabled="true" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
Related
I very a newbie at android programming. I'm here because i want your help. Im designing an activity for my Thesis.
Heres the Image
I want to have an activity that has 2 Frames/Layout. The upper frame have buttons, the lower frame will display something when i click the button on the upper frame.
What would i use to make this happen?
Thank you in advance.
You can split the layout in half using LinearLayout and layout_weight param, like this:
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
layout_weight parameter functions as a priority in deciding who gets the space, given the other desciption would cause any of FrameLayouts to fill entire parent. You can find more in documentation.
Here is the complete solution:
step1:
create main_layout.xml file into layout folder:
<?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">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="New Text"
android:id="#+id/textView" />
</LinearLayout>
step2: create MainActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private View mBtn1;
private View mBtn2;
private TextView mTxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mBtn1 = findViewById(R.id.button1);
mBtn2 = findViewById(R.id.button2);
mTxt = (TextView)findViewById(R.id.textView);
mBtn1.setOnClickListener(mBtnClickListener);
mBtn2.setOnClickListener(mBtnClickListener);
}
View.OnClickListener mBtnClickListener = new View.OnClickListener() {
/**
* This is all sync because we want to have only one event at a time
* #param v
*/
#Override
public synchronized void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mTxt.setText("button1");
break;
case R.id.button2:
mTxt.setText("button2");
break;
}
}
} ;
}
step3: add the activty into your AndroidManifest.xml:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have a progress spinner displayed in the menu. I'd like to display a number inside the spinner, how would I go about doing this? Below are some of the code snippets for what I am doing to make the progress spinner work:
public void onCreateOptionsMenu(...) {
...
if (uploadCount > 0) {
MenuItem updatingMenuItem = menu.findItem(R.id.photo_capture_action_updating);
updatingMenuItem.setActionView(R.layout.action_progressbar);
updatingMenuItem.expandActionView();
} else {
menu.removeItem(R.id.photo_capture_action_updating);
}
}
And below is the action_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
Thank You,
You can add a textview along with this progressbar,with gravity center and wrap it inside a frame layout.
xml-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressBarLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="14" />
</FrameLayout>
Hope this solves your problem.
I am working on a Android project in which I need to show Google Map v2 on top half of the Android Screen and in the bottom half of the same android screen, I need to show a ListView.
Below is the code, I have so far which will be launched as soon as I launch my Application. I have setup properly the AndroidManifest.xml file for Google Map V2 key.
public class SampleActivity extends Activity {
#TargetApi(11)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().hide();
}
findViewById(R.id.sample_button).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
int width = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
.getDisplayMetrics());
SlideoutActivity.prepare(SampleActivity.this,
R.id.inner_content, width);
startActivity(new Intent(SampleActivity.this,
MenuActivity.class));
overridePendingTransition(0, 0);
}
});
}
}
And below is the sample.xml file which I need to modify so that I can add stuff which will have Google Map v2 in top half of the android screen and in the bottom half, I will have ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inner_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_android" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:background="#bb000000">
<Button style="#android:style/Widget.Button.Small"
android:id="#+id/sample_button"
android:layout_width="35dip"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text=">" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/sample_button"
android:layout_centerVertical="true"
android:textSize="19sp"
android:textColor="#ffffff"
android:text="Facebook-like slide-out nav"/>
</RelativeLayout>
</RelativeLayout>
Can anyone help me with this? I am done with all my settings related to setting up the Google Map API v2 key. I just need to plugin the code to start showing the google map v2 on the TOP half of the android screen and list view on the bottom half of the android screen.
Any help will be appreciated on this. Thanks
Update:-
Will this updated XML file will work to have Google Map v2 on top half of the android screen and ListView on bottom half of the android screen?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inner_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_android" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
android:background="#bb000000"
android:paddingLeft="2dip"
android:paddingRight="2dip" >
<Button
android:id="#+id/sample_button"
style="#android:style/Widget.Button.Small"
android:layout_width="35dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:text=">" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/sample_button"
android:text="Proximity Application"
android:textColor="#ffffff"
android:textSize="19sp" />
</LinearLayout>
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<ListView
android:id="#+id/mylist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</LinearLayout>
This layout should present you a map in the top half of the screen an a ListView in the bottom half:
<?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="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list">
</ListView>
</LinearLayout>
</LinearLayout>
So im using this awesome Cards library (CardsUI) and trying to figure out how to insert my own text into these views grammatically. Im my main activity im parsing JSON data, now i want to use data from the JSON and insert the text into the Cards. Here is some code i have so far.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_enlighten);
// the id title is referencing the title in my card layout below
system_id = (TextView) findViewById(R.id.title);
system_name = (TextView) findViewById(R.id.system_name);
CardUI mCardView = (CardUI) findViewById(R.id.cardUI1);
mCardView.setSwipeable(false);
// add AndroidViews Cards
mCardView.addCard(new MyCard("Get the CardsUI view"));
mCardView.addCardToLastStack(new MyCard("for Android at"));
MyCard androidViewsCard = new MyCard("www.androidviews.net");
mCardView.addCardToLastStack(androidViewsCard);
// draw cards
mCardView.refresh();
// Here is my JSON Parsing activity.
new ProgressTask(WelcomeYou.this).execute();
}
My Card Layout
<?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="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/title"
style="#style/CardTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="title" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="4dp"
android:background="#color/stroke" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selectable_background_cardbank"
android:gravity="center_vertical"
android:padding="4dp" >
<TextView
android:id="#+id/description"
style="#style/CardText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:ellipsize="end"
android:maxLines="4"
android:text="description" />
</LinearLayout>
</LinearLayout>
Im simply trying to put text into the here mCardView.addCard(new MyCard("PUT JSON STRING HERE"));
I know this question was posted a while ago, but I'm just now using the CardUI and I've got it to work. I don't know what R.id.activity_hello_enlighten contains, but if you look at the example provided with their source, the layout from your activity should be using a CardUI component.
Code from the example layout:
<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" >
<com.fima.cardsui.views.CardUI
android:id="#+id/cardsview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Otherwise there's nothing actually doing your card rendering to the screen.
Once you do this, get a pointer to the CardUI referenced by your layout (in the example code it's called cardsview) with the following code:
mCardView = (CardUI) findViewById(R.id.cardsview);
After that, it's as simple as adding a new card like this:
mCardView.addCardToLastStack(new MyCard("Your card's text!"));
I want to have two buttons top of a listview. If I scroll below the listview I want that the buttons stay at top. I searched through many solutions on stackoverflow however I could not find the exact one for my case.
My layout xml(mainlayout.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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The graphical layout produced is just what I wanted:
Here is my code to use this layout, in case I might fail to bind the Java code to layout;
public class MainActivity extends ListActivity{
Button latestButton;
Button trackedButton;
ListView lv = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
latestButton = (Button)findViewById(R.id.button1);
latestButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Latest Button!", Toast.LENGTH_LONG).show();
}
});
trackedButton = (Button)findViewById(R.id.button2);
trackedButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Tracked Button!", Toast.LENGTH_LONG).show();
}
});
this.lv = getListView();
String[] values = new String[]{"asdfa","qwerqwer","banbn"};
lv.setAdapter(new ArrayAdapter<String>(BilTrackerMainActivity.this, android.R.layout.simple_expandable_list_item_1, values));
}
however when this activity is called from my other activity my app unfortunately closed. LogCat says:
Your content must have a ListView whose id attribute is 'android.R.id.list'
But I try adding lv = (ListView)findViewById(R.id.mylist); instead of this.lv = getListView();. I could not solve this issue.
I am aware of ListView#addHeaderView but I want to solve my particular problem with using this kind of layout.
Just change your listview id to android:id="#android:id/list"
Use a RelativeLayout. Place the second button to the right of the first button using layout_toRightOf then place the listview beneath button one using layout_below. Something like this (I haven't tested it, apologies for any typos)
<?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="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_toRightOf="#id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
<ListView
android:id="#+id/mylist"
android:layout_below="#id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Here is the xml you are looking for
<?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:background="#FF394952">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="#android:style/ButtonBar">
<Button
android:id="#+id/btn_AddMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/newItem"
android:textColor="#FF394952"/>
</LinearLayout>
<ListView
android:id="#+id/game_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
</LinearLayout>
And when you design Activity for this dont use List activity directly use Activity and set adapter on the ListView.
ListView lv = (ListView) findViewById(R.id.game_list);
ArrayAdapter<String> listItem = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ALBUM_NAME);
lv.setAdapter(listItem);
lv.setTextFilterEnabled(true);
Use below xml file for that
<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mLlayout1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mLlayout1" >
</ListView>
</RelativeLayout>
Create FrameLayout with [your ListView] and [LinearLayout with your buttons].
Do once something like this within onCreate()
LinearLayout myButtons = ...;
myButtons.bringToFront();
So, your ContentView will have a FrameLayout with two child, one of those is in foreground.
That's all. Hope it helped))